forked from JunStar/swoole-chess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.php
86 lines (71 loc) · 1.91 KB
/
cli.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
use Phalcon\Di\FactoryDefault\Cli as CliDI,
Phalcon\Cli\Console as ConsoleApp;
use Phalcon\Db\Adapter\Pdo\Mysql as DbAdapter;
define('VERSION', '1.0.0');
// 使用CLI工厂类作为默认的服务容器
$di = new CliDI();
// 定义应用目录路径
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__)));
/**
* 注册类自动加载器
*/
$loader = new \Phalcon\Loader();
$loader->registerDirs(
array(
APPLICATION_PATH . '/app/cli',
APPLICATION_PATH . '/app/models',
APPLICATION_PATH . '/app/lib',
)
);
$loader->register();
// 加载配置文件(如果存在)
if (is_readable(APPLICATION_PATH . '/config/config.php')) {
$config = include APPLICATION_PATH . '/config/config.php';
$di->set('config', $config);
}
$di->set('common', function(){
$common = new common();
return $common;
});
// Setup the database service
$di->set('db', function () {
return new DbAdapter(array(
"host" => "localhost",
"username" => "root",
"password" => "123456",
"dbname" => "chess",
"charset" => "utf8"
));
});
$di->set('PlayIdFdUserIdRel', function(){
$PlayIdFdUserIdRel = new PlayIdFdUserIdRel();
return $PlayIdFdUserIdRel;
});
// 创建console应用
$console = new ConsoleApp();
$console->setDI($di);
/**
* 处理console应用参数
*/
$arguments = array();
foreach ($argv as $k => $arg) {
if ($k == 1) {
$arguments['task'] = $arg;
} elseif ($k == 2) {
$arguments['action'] = $arg;
} elseif ($k >= 3) {
$arguments['params'][] = $arg;
}
}
// 定义全局的参数, 设定当前任务及动作
define('CURRENT_TASK', (isset($argv[1]) ? $argv[1] : null));
define('CURRENT_ACTION', (isset($argv[2]) ? $argv[2] : null));
try {
// 处理参数
$console->handle($arguments);
} catch (\Phalcon\Exception $e) {
echo $e->getMessage();
exit(255);
}