Skip to content

Commit cc82deb

Browse files
committed
feat(router): add job router strategy ✨
1 parent 76efbdb commit cc82deb

File tree

7 files changed

+194
-15
lines changed

7 files changed

+194
-15
lines changed

README-CN.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -688,11 +688,10 @@ cp ./.git-hooks/* ./git/hooks
688688

689689
- 提供更友善的开发api帮助
690690
- 模块支持数据库nosql自定义配置
691-
- 支持mysql主从配置
692691
- ORM提供更多链式操作api
693692
- 框架log行为进行级别分类
694693
- 想办法解决上线部署是配置文件问题
695-
- 基于phar文件和git webhook自动化打包部署
694+
- 基于phar文件打包部署
696695
- ...
697696

698697
# DONE
@@ -707,3 +706,7 @@ cp ./.git-hooks/* ./git/hooks
707706
- 提供更友善的开发api帮助
708707
+ 请求参数校验:require/length/number
709708
- 支持mysql主从配置
709+
710+
- v0.7.0(2017/06/18)
711+
- 集成travis-ci实现持续集成部署
712+
- 增加脚本目录

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -681,11 +681,10 @@ project address: [https://github.com/TIGERB/easy-php](https://github.com/TIGERB/
681681

682682
- Provide much friendly help for user
683683
- Module's config support module-defined mysql and nosql configuration
684-
- Support master-salve mysql configuration
685684
- ORM provide more apis
686685
- Make different rank for log
687686
- Resolve config problem when publish our project
688-
- implement auto deploy by used phar and git webhook
687+
- implement auto deploy by used phar
689688
- ...
690689

691690

@@ -696,7 +695,12 @@ project address: [https://github.com/TIGERB/easy-php](https://github.com/TIGERB/
696695
- The performance test and optimize
697696
- Use the lazy load thought to optimize the framework
698697
- Change Helper's method to the framework's function
698+
699699
- v0.6.9(2017/05/22)
700700
- more friendly for api develop process
701701
+ request param check:require/length/number
702702
- support master-salve config for db
703+
704+
- v0.7.0(2017/06/18)
705+
- implement ci by travis-ci
706+
- add jobs script folder

cli

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,24 @@
1515
*
1616
* 在php cli模式下运行框架运行框架
1717
*/
18-
18+
// php cli --env=<environment> --<method|job>=<value> --<arguments>=<value> ...
19+
// \033[33m env \033[0m: Script run environment
20+
//
1921
/**
2022
* 命令行参数list
2123
*/
2224
$help = function () {
2325
$string = <<<HELP
2426
\033[36m Cli Usage \033[0m:
25-
php cli --env=<environment> --method=<value> --<arguments>=<value> ...
27+
php cli --<method||job>=<value> --<arguments>=<value> ...
2628
2729
\033[36m Example \033[0m:
2830
php cli --method=demo.index.get --username=test
31+
php cli --job=demo.demo.test --username=test
2932
3033
\033[36m Params mean \033[0m:
31-
\033[33m env \033[0m: Script run environment
3234
\033[33m method \033[0m: The function will running, value format <module.controller.action>
35+
\033[33m job \033[0m: The function will running, value format <module.job.action>
3336
\033[33m arguments \033[0m: Input arguments
3437
\033[33m help \033[0m: Display command list
3538
@@ -39,7 +42,7 @@ $help = function () {
3942
4043
For example, php -S 'localhost:666', after that open website 'http://localhost:666/'
4144
42-
\033[32mEasy PHP: A lightweight PHP framework for studying. Version 0.6.6\033[0m
45+
\033[32mEasy PHP: A Faster Lightweight Full-Stack PHP Framework. Version 0.7.0\033[0m
4346
4447
\033[32mAuthor : TIGERB <https://github.com/TIGERB>\033[0m
4548
\n
@@ -70,12 +73,23 @@ foreach ($argv as $v) {
7073

7174
// 设置cli模式
7275
putenv('IS_CLI=yes');
73-
$method = $input['method'];
74-
$method = explode('.', $method);
75-
$input['module'] = $method[0];
76-
$input['controller'] = $method[1];
77-
$input['action'] = $method[2];
78-
unset($input['method']);
76+
if (isset($input['method'])) {
77+
$method = $input['method'];
78+
$method = explode('.', $method);
79+
$input['module'] = $method[0];
80+
$input['controller'] = $method[1];
81+
$input['action'] = $method[2];
82+
$input['router_mode']= 'controller';
83+
unset($input['method']);
84+
} else {
85+
$job = $input['job'];
86+
$job = explode('.', $job);
87+
$input['module'] = $job[0];
88+
$input['job'] = $job[1];
89+
$input['action'] = $job[2];
90+
$input['router_mode']= 'job';
91+
}
92+
7993
$_REQUEST['argv'] = $input;
8094

8195
// 载入框架运行文件

framework/handles/RouterHandle.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Framework\Exceptions\CoreHttpException;
1616
use ReflectionClass;
1717
use Closure;
18+
use Framework\Handles\Router\Job;
1819

1920
/**
2021
* 路由处理机制.
@@ -208,7 +209,8 @@ public function register(App $app)
208209
// 注入当前对象到容器中
209210
$app::$container->setSingle('router', $this);
210211
// request uri
211-
$this->requestUri = $app::$container->getSingle('request')->server('REQUEST_URI');
212+
$request = $app::$container->getSingle('request');
213+
$this->requestUri = $request->server('REQUEST_URI');
212214
// App
213215
$this->app = $app;
214216
// 获取配置
@@ -221,6 +223,11 @@ public function register(App $app)
221223
$this->actionName = $this->config->config['route']['default_action'];
222224

223225
/* 路由策略 */
226+
if ($app->isCli === 'yes' && $request->get('router_mode') === 'job') {
227+
$job = new Job();
228+
$job->register($app);
229+
return;
230+
}
224231
$this->routeStrategy = 'pathinfo';
225232
if (strpos($this->requestUri, 'index.php') || $app->isCli === 'yes') {
226233
$this->routeStrategy = 'general';

framework/handles/router/Controller.php

Whitespace-only changes.

framework/handles/router/Job.php

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?php
2+
/********************************************
3+
* Easy PHP *
4+
* *
5+
* A lightweight PHP framework for studying *
6+
* *
7+
* TIERGB *
8+
* <https://github.com/TIGERB> *
9+
* *
10+
********************************************/
11+
12+
namespace Framework\Handles\Router;
13+
14+
use Framework\App;
15+
use Framework\Exceptions\CoreHttpException;
16+
use ReflectionClass;
17+
use Closure;
18+
19+
/**
20+
* job路由处理机制.
21+
*
22+
* @author TIERGB <https://github.com/TIGERB>
23+
*/
24+
class Job
25+
{
26+
/**
27+
* 框架实例.
28+
*
29+
* @var App
30+
*/
31+
private $app;
32+
33+
/**
34+
* 配置实例
35+
*
36+
* @var
37+
*/
38+
private $config;
39+
40+
/**
41+
* 注册路由处理机制.
42+
*
43+
* @param App $app 框架实例
44+
* @param void
45+
*/
46+
public function register(App $app)
47+
{
48+
// App
49+
$this->app = $app;
50+
$this->app->notOutput = true;
51+
52+
// 获取配置
53+
$this->config = $app::$container->getSingle('config');
54+
$this->routeStrategy = 'general';
55+
56+
// 开启路由
57+
$this->route();
58+
}
59+
60+
/**
61+
* 路由机制
62+
*
63+
* @param void
64+
*/
65+
public function route()
66+
{
67+
// 路由策略
68+
$strategy = $this->routeStrategy;
69+
$this->$strategy();
70+
71+
// 判断模块存不存在
72+
if (! in_array(strtolower($this->moduleName), $this->config->config['module'])) {
73+
throw new CoreHttpException(404, 'Module:'.$this->moduleName);
74+
}
75+
76+
// 获job类
77+
$jobName = ucfirst($this->jobName);
78+
$jobPath = "Jobs\\{$this->moduleName}\\{$jobName}";
79+
80+
// 判断控制器存不存在
81+
if (! class_exists($jobPath)) {
82+
throw new CoreHttpException(404, 'Job:'.$jobName);
83+
}
84+
85+
// 反射解析当前控制器类 判断是否有当前操作方法
86+
$reflaction = new ReflectionClass($jobPath);
87+
if (!$reflaction->hasMethod($this->actionName)) {
88+
throw new CoreHttpException(404, 'Action:'.$this->actionName);
89+
}
90+
// 实例化当前控制器
91+
$job = new $jobPath();
92+
// 调用操作
93+
$actionName = $this->actionName;
94+
// 获取返回值
95+
$this->app->responseData = $job->$actionName();
96+
}
97+
98+
/**
99+
* 普通路由 url路径
100+
*
101+
* @param void
102+
*/
103+
public function general()
104+
{
105+
$app = $this->app;
106+
$request = $app::$container->getSingle('request');
107+
$moduleName = $request->request('module');
108+
$jobName = $request->request('job');
109+
$actionName = $request->request('action');
110+
111+
$this->moduleName = $moduleName;
112+
$this->jobName = $jobName;
113+
$this->actionName = $actionName;
114+
}
115+
}

jobs/demo/Demo.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
/**
3+
* Easy PHP
4+
*
5+
* A Faster Lightweight Full-Stack PHP Framework
6+
*
7+
* author: TIERGB <https://github.com/TIGERB>
8+
*/
9+
10+
namespace Jobs\Demo;
11+
12+
/**
13+
* Demo Jobs
14+
*
15+
* @author TIERGB <https://github.com/TIGERB>
16+
*/
17+
class Demo
18+
{
19+
/**
20+
* 构造函数
21+
*/
22+
public function __construct()
23+
{
24+
# code...
25+
}
26+
27+
/**
28+
* job
29+
*
30+
* @example php cli --jobs=demo.demo.test
31+
*/
32+
public function test()
33+
{
34+
echo 'Hello Easy PHP Jobs';
35+
}
36+
}

0 commit comments

Comments
 (0)