Skip to content

Commit 8cef4a1

Browse files
author
leiguoguo
committed
add demo code
1 parent f61ab37 commit 8cef4a1

File tree

1 file changed

+60
-55
lines changed

1 file changed

+60
-55
lines changed

demo/demo.php

Lines changed: 60 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,72 @@
11
<?php
22
require_once __DIR__ . '/vendor/autoload.php';
33

4-
$config = array(
5-
'compilePath' => __DIR__ . '/var/ckcr_compiled/',
6-
'preDefined' => array(
7-
'user' => 'aarr|include:name;id{
8-
name: str,
9-
id: int,
10-
}',
11-
'show' => 'aarr|include:id;url{
12-
id: int,
13-
url: str,
14-
}',
15-
),
16-
);
4+
############################# Env本身定义了一个抽象概念, 需要根据不同的运营环境定义不同的实现. 比如, 对于客户端App
5+
class AppEnv extends \ClientAdapter\Env {
6+
public $osName;
7+
public $appVersion;
8+
}
179

18-
$data = array(
19-
'user' => array(
20-
'id' => 1,
21-
'name' => 'goosman-lei',
22-
'age' => 30,
23-
),
24-
'shows' => array(
10+
############################ 客户端适配器, 本身提供了一种规则校验, 下面以我们的Feature应用场景示例
11+
class Feature {
12+
protected static $features = array();
13+
14+
public static function init($featuresConfig) {
15+
foreach ($featuresConfig as $featureName => $envDesc) {
16+
if (\ClientAdapter\Env::checkEnv($envDesc)) {
17+
self::$features[$featureName] = TRUE;
18+
}
19+
}
20+
}
21+
22+
public static function isEnable($featureName) {
23+
return array_key_exists($featureName, self::$features) && self::$features[$featureName] === TRUE;
24+
}
25+
}
26+
27+
28+
########################## OK. 下面是应用中使用Feature的应用场景
29+
30+
echo "app start\n\n";
31+
32+
echo "prePare Env\n\n";
33+
$appEnv = new \AppEnv();
34+
$appEnv->sessUid = rand(0, 10000);
35+
$appEnv->clientIp = long2ip(rand(0, pow(2, 32)));
36+
$appEnv->latitude = rand(0, 1000000) / 1000;
37+
$appEnv->longitude = rand(0, 1000000) / 1000;
38+
$appEnv->osName = rand(0, 1) ? 'ios' : 'android';
39+
$appEnv->appVersion = rand(0, 100) / 10;
40+
41+
\ClientAdapter\Env::setCurrEnv($appEnv);
42+
43+
echo "Features init\n\n";
44+
$featuresConfig = array(
45+
'support-hail' => array( # ios 5.2及以上版本; android 5.3及以上版本. 支持打招呼功能
2546
array(
26-
'id' => 1,
27-
'url' => 'http://img.oneniceapp.com/1.jpg',
47+
'osName eq ios',
48+
'appVersion v>= 5.2',
2849
),
2950
array(
30-
'id' => 2,
31-
'url' => 'http://img.oneniceapp.com/2.jpg',
51+
'osName eq android',
52+
'appVersion v>= 5.3',
3253
),
3354
),
55+
'support-emotion' => array( # ios 5.3及以上版本, 10%小流量用户开启表情功能
56+
'osName eq ios',
57+
'appVersion v>= 5.3',
58+
'sessUid <=% 100:10',
59+
),
3460
);
61+
Feature::init($featuresConfig);
3562

63+
echo "Business code is\n";
64+
if (Feature::isEnable('support-hail')) {
65+
echo "\tHere is hail code\n";
66+
}
67+
if (Feature::isEnable('support-emotion')) {
68+
echo "\tHere is emotion code\n";
69+
}
3670

37-
$ckcrHandler = new \Ckcr\Handler($config);
38-
39-
40-
41-
echo '==================直接的应用' . chr(10);
42-
$ckcr = 'aarr{
43-
user: aarr|include:name;id{
44-
name: str,
45-
id: int,
46-
},
47-
shows: iarr{
48-
*: aarr|include:url{
49-
url: str,
50-
}
51-
}
52-
}';
53-
$ckcrProxy = $ckcrHandler->getProxy($ckcr);
54-
$ckcrProxy->ckcr($data);
55-
echo json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) . chr(10);
56-
57-
58-
echo '==================带预处理的例子' . chr(10);
59-
$ckcr = 'aarr{
60-
user: {@user@},
61-
shows: iarr{
62-
*: {@show@},
63-
}
64-
}';
65-
$ckcrProxy = $ckcrHandler->getProxy($ckcr);
66-
$ckcrProxy->ckcr($data);
67-
echo json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) . chr(10);
71+
echo "\nAppEnv is:\n";
72+
echo json_encode((array)$appEnv, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) . chr(10);

0 commit comments

Comments
 (0)