diff --git a/.git-hooks/pre-commit b/.git-hooks/pre-commit index e96b8b1..74e5dff 100644 --- a/.git-hooks/pre-commit +++ b/.git-hooks/pre-commit @@ -8,7 +8,7 @@ PHPCS_BIN=./vendor/bin/phpcs PHPCS_CODING_STANDARD=PSR1,PSR2 -PHPCS_IGNORE=./vendor,./.git-hooks,./.env,./.gitignore,./composer.json,./composer.lock,./README.md,/frontend,./docs,./config,./.babelrc,./LICENSE,./yarn.lock,./webpack.config.js,./package.json,.travis.yml,phpunit.xml +PHPCS_IGNORE=./vendor,./.git-hooks,./.env,./.gitignore,./composer.json,./composer.lock,./README.md,/frontend,./docs,./config,./.babelrc,./LICENSE,./yarn.lock,./webpack.config.js,./package.json,.travis.yml,phpunit.xml,README-CN.md,cli,.tmp_staging TMP_STAGING=".tmp_staging" # parse config diff --git a/README-CN.md b/README-CN.md index b4dbfc9..f01f8db 100644 --- a/README-CN.md +++ b/README-CN.md @@ -78,6 +78,14 @@ framework [Easy PHP核心框架目录] │ ├── Model.php [数据模型基类] │ └── db [数据库类目录] │ └── Mysql.php [mysql实体类] +├── router [路由策略] +│ ├── RouterInterface.php [路由策略接口] +│ ├── General.php [普通路由] +│ ├── Pathinfo.php [pathinfo路由] +│ ├── Userdefined.php [自定义路由] +│ ├── Micromonomer.php [微单体路由] +│ ├── Job.php [脚本任务路由] +│ └── EasyRouter.php [路由策略入口类] ├── nosql [nosql类目录] │ ├── Memcahed.php [Memcahed类文件] │ ├── MongoDB.php [MongoDB类文件] @@ -225,6 +233,17 @@ $request->check('code', 'number'); ## 路由模块 +``` +├── router [路由策略] +│ ├── RouterInterface.php [路由策略接口] +│ ├── General.php [普通路由] +│ ├── Pathinfo.php [pathinfo路由] +│ ├── Userdefined.php [自定义路由] +│ ├── Micromonomer.php [微单体路由] +│ ├── Job.php [脚本任务路由] +│ └── EasyRouter.php [路由策略入口类] +``` + 通过用户访问的url信息,通过路由规则执行目标控制器类的的成员方法。我在这里把路由大致分成了四类: **传统路由** @@ -725,7 +744,7 @@ cp ./.git-hooks/* ./git/hooks # TODO -- 重构路由,简化jobs路由流程 +- 集成swoole - 增加数据库变更辅助 - 集成swagger - 提供更友善的开发api帮助 @@ -738,6 +757,9 @@ cp ./.git-hooks/* ./git/hooks # DONE +- v0.7.1(2017/08/29) + - 重构路由模块 + - v0.7.0(2017/06/18) - 集成travis-ci实现持续集成部署 - 增加脚本目录 diff --git a/README.md b/README.md index 4b608b6..5b5f8dc 100644 --- a/README.md +++ b/README.md @@ -77,6 +77,14 @@ framework [easy-php framework directory] │ ├── Model.php [data model] │ └── db [db type directory] │ └── Mysql.php [mysql class file] +├── router [router strategy] +│ ├── RouterInterface.php [router strategy interface] +│ ├── General.php [general strategy class] +│ ├── Pathinfo.php [pathinfo strategy class] +│ ├── Userdefined.php [userdefined strategy class] +│ ├── Micromonomer.php [micromonomer strategy class] +│ ├── Job.php [job strategy class] +│ └── EasyRouter.php [router strategy entrance class] ├── nosql [nosql directory] │ ├── Memcahed.php [memcahed class file] │ ├── MongoDB.php [mongoDB class file] @@ -222,6 +230,17 @@ $request->check('code', 'number'); ## Route Handle Module +``` +├── router [datebase object relation map class directory] +│ ├── RouterInterface.php [router strategy interface] +│ ├── General.php [general strategy class] +│ ├── Pathinfo.php [pathinfo strategy class] +│ ├── Userdefined.php [userdefined strategy class] +│ ├── Micromonomer.php [micromonomer strategy class] +│ ├── Job.php [job strategy class] +│ └── EasyRouter.php [router strategy entrance class] +``` + Execute the target controller's function by the router parse the url information.Is composed of four types of: **tradition router** @@ -719,7 +738,7 @@ project address: [https://github.com/TIGERB/easy-php](https://github.com/TIGERB/ # TODO -- Refactor router handle and make the jobs route more simple +- Use swoole - Add database sql helper - Integrate swagger - Provide much friendly help for user @@ -733,6 +752,9 @@ project address: [https://github.com/TIGERB/easy-php](https://github.com/TIGERB/ # DONE +- v0.7.1(2017/08/29) + - refactor router by the strategy design pattern + - v0.7.0(2017/06/18) - implement ci by travis-ci - add jobs script folder diff --git a/cli b/cli index f0ccb18..4ad7a6b 100644 --- a/cli +++ b/cli @@ -1,15 +1,14 @@ -#!/usr/bin/php * - * * - ********************************************/ - +/************************************************* + * Easy PHP * + * * + * A Faster Lightweight Full-Stack PHP Framework * + * * + * TIERGB * + * * + * * + *************************************************/ + /** * cli模式 * @@ -41,7 +40,7 @@ $help = function () { \033[36m Quick start demo \033[0m: php cli --run - \033[32mEasy PHP: A Faster Lightweight Full-Stack PHP Framework. Version 0.7.0\033[0m + \033[32mEasy PHP: A Faster Lightweight Full-Stack PHP Framework. Version 0.7.1\033[0m \033[32mAuthor : TIGERB \033[0m \n diff --git a/composer.json b/composer.json index 710ab59..92f5be5 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "tigerb/easy-php", "description": "A Faster Lightweight Full-Stack PHP Framework", - "version": "0.7.0", + "version": "0.7.1", "type": "framework", "homepage": "http://php.tiegrb.cn/", "license": "MIT", diff --git a/config/common.php b/config/common.php index 2c1225a..658e8f9 100644 --- a/config/common.php +++ b/config/common.php @@ -12,6 +12,12 @@ use Framework\Helper; return [ + /* 应用目录名称 */ + 'application_folder_name' => 'app', + + /* 脚本目录名称 */ + 'jobs_folder_name' => 'jobs', + /* 默认模块 */ 'module' => [ 'demo' diff --git a/framework/App.php b/framework/App.php index 82077d5..d8b3148 100644 --- a/framework/App.php +++ b/framework/App.php @@ -1,13 +1,13 @@ * - * * - ********************************************/ +/************************************************* + * Easy PHP * + * * + * A Faster Lightweight Full-Stack PHP Framework * + * * + * TIERGB * + * * + * * + *************************************************/ namespace Framework; @@ -222,7 +222,7 @@ public function callSelf($method = '', $uri = '', $argus = array()) $router->moduleName = $requestUri[0]; $router->controllerName = $requestUri[1]; $router->actionName = $requestUri[2]; - $router->routeStrategy = 'microMonomer'; + $router->routeStrategy = 'micromonomer'; $router->route(); return $this->responseData; } diff --git a/framework/handles/RouterHandle.php b/framework/handles/RouterHandle.php index 57d28a8..6c7e313 100644 --- a/framework/handles/RouterHandle.php +++ b/framework/handles/RouterHandle.php @@ -1,13 +1,13 @@ * - * * - ********************************************/ +/************************************************* + * Easy PHP * + * * + * A Faster Lightweight Full-Stack PHP Framework * + * * + * TIERGB * + * * + * * + *************************************************/ namespace Framework\Handles; @@ -15,7 +15,8 @@ use Framework\Exceptions\CoreHttpException; use ReflectionClass; use Closure; -use Framework\Handles\Router\Job; +use Framework\Router\Job; +use Framework\Router\EasyRouter; /** * 路由处理机制. @@ -24,180 +25,6 @@ */ class RouterHandle implements Handle { - /** - * 框架实例. - * - * @var App - */ - private $app; - - /** - * 配置实例 - * - * @var - */ - private $config; - - /** - * 默认模块. - * - * @var string - */ - private $moduleName = ''; - - /** - * 默认控制器. - * - * @var string - */ - private $controllerName = ''; - - /** - * 默认操作. - * - * @var string - */ - private $actionName = ''; - - /** - * 默认操作. - * - * @var string - */ - private $routeStrategy = ''; - - /** - * 请求uri. - * - * @var string - */ - private $requestUri = ''; - - /** - * 自定义路由规则 - * - * get请求 - * - * 查询数据 - * - * @var array - */ - private $getMap = []; - - /** - * 自定义路由规则 - * - * post请求 - * - * 新增数据 - * - * @var array - */ - private $postMap = []; - - /** - * 自定义路由规则 - * - * put请求 - * - * 更新数据 - * - * @var array - */ - private $putMap = []; - - /** - * 自定义路由规则 - * - * delete请求 - * - * 删除数据 - * - * @var array - */ - private $deleteMap = []; - - /** - * 构造函数. - */ - public function __construct() - { - # code... - } - - /** - * 魔法函数__get. - * - * @param string $name 属性名称 - * - * @return mixed - */ - public function __get($name = '') - { - return $this->$name; - } - - /** - * 魔法函数__set. - * - * @param string $name 属性名称 - * @param mixed $value 属性值 - * - * @return mixed - */ - public function __set($name = '', $value = '') - { - $this->$name = $value; - } - - /** - * 自定义get请求路由 - * - * @param string $uri 请求uri - * @param mixed $function 匿名函数或者控制器方法标示 - * @return void - */ - public function get($uri = '', $function = '') - { - $this->getMap[$uri] = $function; - } - - /** - * 自定义post请求路由 - * - * @param string $uri 请求uri - * @param mixed $function 匿名函数或者控制器方法标示 - * @return void - */ - public function post($uri = '', $function = '') - { - $this->postMap[$uri] = $function; - } - - /** - * 自定义put请求路由 - * - * @param string $uri 请求uri - * @param mixed $function 匿名函数或者控制器方法标示 - * @return void - */ - public function put($uri = '', $function = '') - { - $this->putMap[$uri] = $function; - } - - /** - * 自定义delete请求路由 - * - * @param string $uri 请求uri - * @param mixed $function 匿名函数或者控制器方法标示 - * @return void - */ - public function delete($uri = '', $function = '') - { - $this->deleteMap[$uri] = $function; - } - /** * 注册路由处理机制. * @@ -206,214 +33,7 @@ public function delete($uri = '', $function = '') */ public function register(App $app) { - // 注入当前对象到容器中 - $app::$container->setSingle('router', $this); - // request uri - $request = $app::$container->getSingle('request'); - $this->requestUri = $request->server('REQUEST_URI'); - // App - $this->app = $app; - // 获取配置 - $this->config = $app::$container->getSingle('config'); - // 设置默认模块 - $this->moduleName = $this->config->config['route']['default_module']; - // 设置默认控制器 - $this->controllerName = $this->config->config['route']['default_controller']; - // 设置默认操作 - $this->actionName = $this->config->config['route']['default_action']; - - /* 路由策略 */ - if ($app->isCli === 'yes' && $request->get('router_mode') === 'job') { - $job = new Job(); - $job->register($app); - return; - } - $this->routeStrategy = 'pathinfo'; - if (strpos($this->requestUri, 'index.php') || $app->isCli === 'yes') { - $this->routeStrategy = 'general'; - } - - // 开启路由 - $this->route(); - } - - /** - * 路由机制 - * - * @param void - */ - public function route() - { - // 路由策略 - $strategy = $this->routeStrategy; - $this->$strategy(); - - // 自定义路由判断 - if ($this->userDefined()) { - return; - } - - // 判断模块存不存在 - if (! in_array(strtolower($this->moduleName), $this->config->config['module'])) { - throw new CoreHttpException(404, 'Module:'.$this->moduleName); - } - - // 获取控制器类 - $controllerName = ucfirst($this->controllerName); - $controllerPath = "App\\{$this->moduleName}\\Controllers\\{$controllerName}"; - - // 判断控制器存不存在 - if (! class_exists($controllerPath)) { - throw new CoreHttpException(404, 'Controller:'.$controllerName); - } - - // 反射解析当前控制器类 判断是否有当前操作方法 - $reflaction = new ReflectionClass($controllerPath); - if (!$reflaction->hasMethod($this->actionName)) { - throw new CoreHttpException(404, 'Action:'.$this->actionName); - } - - // 实例化当前控制器 - $controller = new $controllerPath(); - // if (! method_exists($controller, $this->actionName)) { - // throw new CoreHttpException(404, 'Action:'.$this->actionName); - // } - - // 调用操作 - $actionName = $this->actionName; - // 获取返回值 - $this->app->responseData = $controller->$actionName(); - } - - /** - * 普通路由 url路径 - * - * @param void - */ - public function general() - { - $app = $this->app; - $request = $app::$container->getSingle('request'); - $moduleName = $request->request('module'); - $controllerName = $request->request('controller'); - $actionName = $request->request('action'); - if (! empty($moduleName)) { - $this->moduleName = $moduleName; - } - if (! empty($controllerName)) { - $this->controllerName = $controllerName; - } - if (! empty($actionName)) { - $this->actionName = $actionName; - } - - // CLI 模式不输出 - if (empty($actionName) && $this->app->isCli === 'yes') { - $this->app->notOutput = true; - } - } - - /** - * pathinfo url路径. - */ - public function pathinfo() - { - /* 匹配出uri */ - if (strpos($this->requestUri, '?')) { - preg_match_all('/^\/(.*)\?/', $this->requestUri, $uri); - } else { - preg_match_all('/^\/(.*)/', $this->requestUri, $uri); - } - - // 使用默认模块/控制器/操作逻辑 - if (!isset($uri[1][0]) || empty($uri[1][0])) { - // CLI 模式不输出 - if ($this->app->isCli === 'yes') { - $this->app->notOutput = true; - } - return; - } - $uri = $uri[1][0]; - - /* 自定义路由判断 */ - - $uri = explode('/', $uri); - switch (count($uri)) { - case 3: - $this->moduleName = $uri['0']; - $this->controllerName = $uri['1']; - $this->actionName = $uri['2']; - break; - - case 2: - /* - * 使用默认模块 - */ - $this->controllerName = $uri['0']; - $this->actionName = $uri['1']; - break; - case 1: - /* - * 使用默认模块/控制器 - */ - $this->actionName = $uri['0']; - break; - - default: - /* - * 使用默认模块/控制器/操作逻辑 - */ - break; - } - } - - /** - * 自定义路由 - * - * @return void - */ - private function userDefined() - { - $module = $this->config->config['module']; - foreach ($module as $v) { - // 加载自定义路由配置文件 - $routeFile = "{$this->app->rootPath}/config/{$v}/route.php"; - if (file_exists($routeFile)) { - require($routeFile); - } - } - - // 路由匹配 - $uri = "{$this->moduleName}/{$this->controllerName}/{$this->actionName}"; - if (! array_key_exists($uri, $this->getMap)) { - return false; - } - - // 执行自定义路由匿名函数 - $app = $this->app; - $request = $app::$container->getSingle('request'); - $method = $request->method . 'Map'; - if (! isset($this->$method)) { - throw new CoreHttpException( - 404, - 'Http Method:'. $request->method - ); - } - $map = $this->$method; - $this->app->responseData = $map[$uri]($app); - if ($this->app->isCli === 'yes') { - $this->app->notOutput = false; - } - return true; - } - - /** - * APP内部调用 可构建微单体架构 - * - * @return void - */ - public function microMonomer() - { - # do nothing... + // 初始化路由模块入口类 + (new EasyRouter())->init($app); } } diff --git a/framework/handles/router/Controller.php b/framework/handles/router/Controller.php deleted file mode 100644 index e69de29..0000000 diff --git a/framework/handles/router/Job.php b/framework/handles/router/Job.php deleted file mode 100644 index 0e85031..0000000 --- a/framework/handles/router/Job.php +++ /dev/null @@ -1,115 +0,0 @@ - * - * * - ********************************************/ - -namespace Framework\Handles\Router; - -use Framework\App; -use Framework\Exceptions\CoreHttpException; -use ReflectionClass; -use Closure; - -/** - * job路由处理机制. - * - * @author TIERGB - */ -class Job -{ - /** - * 框架实例. - * - * @var App - */ - private $app; - - /** - * 配置实例 - * - * @var - */ - private $config; - - /** - * 注册路由处理机制. - * - * @param App $app 框架实例 - * @param void - */ - public function register(App $app) - { - // App - $this->app = $app; - $this->app->notOutput = true; - - // 获取配置 - $this->config = $app::$container->getSingle('config'); - $this->routeStrategy = 'general'; - - // 开启路由 - $this->route(); - } - - /** - * 路由机制 - * - * @param void - */ - public function route() - { - // 路由策略 - $strategy = $this->routeStrategy; - $this->$strategy(); - - // 判断模块存不存在 - if (! in_array(strtolower($this->moduleName), $this->config->config['module'])) { - throw new CoreHttpException(404, 'Module:'.$this->moduleName); - } - - // 获job类 - $jobName = ucfirst($this->jobName); - $jobPath = "Jobs\\{$this->moduleName}\\{$jobName}"; - - // 判断控制器存不存在 - if (! class_exists($jobPath)) { - throw new CoreHttpException(404, 'Job:'.$jobName); - } - - // 反射解析当前控制器类 判断是否有当前操作方法 - $reflaction = new ReflectionClass($jobPath); - if (!$reflaction->hasMethod($this->actionName)) { - throw new CoreHttpException(404, 'Action:'.$this->actionName); - } - // 实例化当前控制器 - $job = new $jobPath(); - // 调用操作 - $actionName = $this->actionName; - // 获取返回值 - $this->app->responseData = $job->$actionName(); - } - - /** - * 普通路由 url路径 - * - * @param void - */ - public function general() - { - $app = $this->app; - $request = $app::$container->getSingle('request'); - $moduleName = $request->request('module'); - $jobName = $request->request('job'); - $actionName = $request->request('action'); - - $this->moduleName = $moduleName; - $this->jobName = $jobName; - $this->actionName = $actionName; - } -} diff --git a/framework/router/EasyRouter.php b/framework/router/EasyRouter.php new file mode 100644 index 0000000..cff263e --- /dev/null +++ b/framework/router/EasyRouter.php @@ -0,0 +1,257 @@ + * + * * + *************************************************/ + +namespace Framework\Router; + +use Framework\App; +use Framework\Exceptions\CoreHttpException; +// use ReflectionClass; +use Closure; + +/** + * 路由入口. + * + * @author TIERGB + */ +class EasyRouter +{ + /** + * 框架实例. + * + * @var App + */ + private $app; + + /** + * 配置实例 + * + * @var + */ + private $config; + + /** + * 默认模块. + * + * @var string + */ + private $moduleName = ''; + + /** + * 默认控制器. + * + * @var string + */ + private $controllerName = ''; + + /** + * 默认操作. + * + * @var string + */ + private $actionName = ''; + + /** + * 类文件路径. + * + * @var string + */ + private $classPath = ''; + + /** + * 类文件路径. + * + * @var string + */ + private $executeType = ''; + + /** + * 请求uri. + * + * @var string + */ + private $requestUri = ''; + + /** + * 路由策略. + * + * @var string + */ + private $routeStrategy = ''; + + /** + * 路由策略映射 + * + * @var array + */ + private $routeStrategyMap = [ + 'general' => 'Framework\Router\General', + 'pathinfo' => 'Framework\Router\Pathinfo', + 'user-defined' => 'Framework\Router\Userdefined', + 'micromonemer' => 'Framework\Router\Micromonomer', + 'job' => 'Framework\Router\Job' + ]; + + /** + * 魔法函数__get. + * + * @param string $name 属性名称 + * + * @return mixed + */ + public function __get($name = '') + { + return $this->$name; + } + + /** + * 魔法函数__set. + * + * @param string $name 属性名称 + * @param mixed $value 属性值 + * + * @return mixed + */ + public function __set($name = '', $value = '') + { + $this->$name = $value; + } + + /** + * 注册路由处理机制. + * + * @param App $app 框架实例 + * @param void + */ + public function init(App $app) + { + // 注入当前对象到容器中 + $app::$container->setSingle('router', $this); + // request uri + $this->request = $app::$container->getSingle('request'); + $this->requestUri = $this->request->server('REQUEST_URI'); + // App + $this->app = $app; + // 获取配置 + $this->config = $app::$container->getSingle('config'); + // 设置默认模块 + $this->moduleName = $this->config->config['route']['default_module']; + // 设置默认控制器 + $this->controllerName = $this->config->config['route']['default_controller']; + // 设置默认操作 + $this->actionName = $this->config->config['route']['default_action']; + + // 路由决策 + $this->strategyJudge(); + + // 路由策略 + (new $this->routeStrategyMap[$this->routeStrategy])->route($this); + + // 判断是app还是job + $this->isAppOrJob($this); + + // 自定义路由判断 + if ((new $this->routeStrategyMap['user-defined'])->route($this)) { + return; + } + + // 启动路由 + $this->start(); + } + + /** + * 判断是app还是job + * + * @return void + */ + public function isAppOrJob() + { + // 任务类 + if ($this->routeStrategy === 'job') { + $className = $this->request->request('job'); + $actionName = $this->request->request('action'); + $folderName = ucfirst($this->config->config['jobs_folder_name']); + $this->classPath = "{$folderName}\\{$this->moduleName}\\{$className}"; + $this->executeType = 'job'; + return; + } + + // 获取控制器类 + $controllerName = ucfirst($this->controllerName); + $folderName = ucfirst($this->config->config['application_folder_name']); + $this->classPath = "{$folderName}\\{$this->moduleName}\\Controllers\\{$controllerName}"; + $this->executeType = 'controller'; + } + + /** + * 路由策略决策 + * + * @param void + */ + public function strategyJudge() + { + // 路由策略 + if (! empty($this->routeSrategy)) { + return; + } + + // 任务路由 + if ($this->app->isCli === 'yes' && $this->request->get('router_mode') === 'job') { + $this->routeStrategy = 'job'; + return; + } + + // 普通路由 + if (strpos($this->requestUri, 'index.php') || $this->app->isCli === 'yes') { + $this->routeStrategy = 'general'; + return; + } else { + $this->routeStrategy = 'pathinfo'; + return; + } + } + + /** + * 路由机制 + * + * @param void + */ + public function start() + { + // 判断模块存不存在 + if (! in_array(strtolower($this->moduleName), $this->config->config['module'])) { + throw new CoreHttpException(404, 'Module:'.$this->moduleName); + } + + // 判断控制器存不存在 + if (! class_exists($this->classPath)) { + throw new CoreHttpException(404, "{$this->executeType}:{$this->classPath}"); + } + + // 反射解析当前控制器类  + // 判断是否有当前操作方法 + // 不使用反射 + // $reflaction = new ReflectionClass($controllerPath); + // if (!$reflaction->hasMethod($this->actionName)) { + // throw new CoreHttpException(404, 'Action:'.$this->actionName); + // } + + // 实例化当前控制器 + $controller = new $this->classPath(); + if (! method_exists($controller, $this->actionName)) { + throw new CoreHttpException(404, 'Action:'.$this->actionName); + } + + // 调用操作 + $actionName = $this->actionName; + // 获取返回值 + $this->app->responseData = $controller->$actionName(); + } +} diff --git a/framework/router/General.php b/framework/router/General.php new file mode 100644 index 0000000..0ce9de2 --- /dev/null +++ b/framework/router/General.php @@ -0,0 +1,51 @@ + * + * * + *************************************************/ + +namespace Framework\Router; + +use Framework\Router\RouterInterface; +use Framework\Router\EasyRouter; + +/** + * 普通路由策略. + * + * @author TIERGB + */ +class General implements RouterInterface +{ + /** + * 路由方法 + * + * @param void + */ + public function route(EasyRouter $entrance) + { + $app = $entrance->app; + $request = $app::$container->getSingle('request'); + $moduleName = $request->request('module'); + $controllerName = $request->request('controller'); + $actionName = $request->request('action'); + if (! empty($moduleName)) { + $entrance->moduleName = $moduleName; + } + if (! empty($controllerName)) { + $entrance->controllerName = $controllerName; + } + if (! empty($actionName)) { + $entrance->actionName = $actionName; + } + + // CLI 模式不输出 + if (empty($actionName) && $entrance->app->isCli === 'yes') { + $entrance->app->notOutput = true; + } + } +} diff --git a/framework/router/Job.php b/framework/router/Job.php new file mode 100644 index 0000000..83363d4 --- /dev/null +++ b/framework/router/Job.php @@ -0,0 +1,67 @@ + * + * * + *************************************************/ + +namespace Framework\Router; + +use Framework\App; +use Framework\Router\RouterInterface; +use Framework\Router\EasyRouter; +use Framework\Exceptions\CoreHttpException; +use ReflectionClass; +use Closure; + +/** + * 任务路由处理机制. + * + * @author TIERGB + */ +class Job implements RouterInterface +{ + /** + * 框架实例. + * + * @var App + */ + private $app; + + /** + * 配置实例 + * + * @var + */ + private $config; + + /** + * 路由方法. + * + * @param App $app 框架实例 + * @param void + */ + public function route(EasyRouter $entrance) + { + $entrance->app->notOutput = true; + + $app = $entrance->app; + $request = $app::$container->getSingle('request'); + $moduleName = $request->request('module'); + $jobName = $request->request('job'); + $actionName = $request->request('action'); + + $entrance->moduleName = $moduleName; + $entrance->jobName = $jobName; + $entrance->actionName = $actionName; + + // 获job类 + $jobName = ucfirst($jobName); + $appName = ucfirst($entrance->config->config['jobs_folder_name']); + $this->classPath = "{$appName}\\{$moduleName}\\{$jobName}"; + } +} diff --git a/framework/router/Micromonomer.php b/framework/router/Micromonomer.php new file mode 100644 index 0000000..1cfe75e --- /dev/null +++ b/framework/router/Micromonomer.php @@ -0,0 +1,35 @@ + * + * * + *************************************************/ + +namespace Framework\Router; + +use Framework\Router\RouterInterface; +use Framework\Router\EasyRouter; + +/** + * 微单体路由策略. + * + * APP内部调用 可构建微单体架构 + * + * @author TIERGB + */ +class Micromonomer implements RouterInterface +{ + /** + * 路由方法 + * + * @param void + */ + public function route(EasyRouter $entrance) + { + # do nothing... + } +} diff --git a/framework/router/Pathinfo.php b/framework/router/Pathinfo.php new file mode 100644 index 0000000..c2dc334 --- /dev/null +++ b/framework/router/Pathinfo.php @@ -0,0 +1,79 @@ + * + * * + *************************************************/ + +namespace Framework\Router; + +use Framework\Router\RouterInterface; +use Framework\Router\EasyRouter; + +/** + * pathinfo路由策略. + * + * @author TIERGB + */ +class Pathinfo implements RouterInterface +{ + /** + * 路由方法 + * + * @param void + */ + public function route(EasyRouter $entrance) + { + /* 匹配出uri */ + if (strpos($entrance->requestUri, '?')) { + preg_match_all('/^\/(.*)\?/', $entrance->requestUri, $uri); + } else { + preg_match_all('/^\/(.*)/', $entrance->requestUri, $uri); + } + + // 使用默认模块/控制器/操作逻辑 + if (!isset($uri[1][0]) || empty($uri[1][0])) { + // CLI 模式不输出 + if ($entrance->app->isCli === 'yes') { + $entrance->app->notOutput = true; + } + return; + } + $uri = $uri[1][0]; + + /* 自定义路由判断 */ + + $uri = explode('/', $uri); + switch (count($uri)) { + case 3: + $entrance->moduleName = $uri['0']; + $entrance->controllerName = $uri['1']; + $entrance->actionName = $uri['2']; + break; + + case 2: + /* + * 使用默认模块 + */ + $entrance->controllerName = $uri['0']; + $entrance->actionName = $uri['1']; + break; + case 1: + /* + * 使用默认模块/控制器 + */ + $entrance->actionName = $uri['0']; + break; + + default: + /* + * 使用默认模块/控制器/操作逻辑 + */ + break; + } + } +} diff --git a/framework/router/RouterInterface.php b/framework/router/RouterInterface.php new file mode 100644 index 0000000..fd033dd --- /dev/null +++ b/framework/router/RouterInterface.php @@ -0,0 +1,29 @@ + * + * * + *************************************************/ + +namespace Framework\Router; + +use Framework\Router\EasyRouter; + +/** + * 路由策略接口. + * + * @author TIERGB + */ +Interface RouterInterface +{ + /** + * 路由方法 + * + * @param void + */ + public function route(EasyRouter $entrance); +} diff --git a/framework/router/Userdefined.php b/framework/router/Userdefined.php new file mode 100644 index 0000000..90769c6 --- /dev/null +++ b/framework/router/Userdefined.php @@ -0,0 +1,157 @@ + * + * * + *************************************************/ + +namespace Framework\Router; + +use Framework\Router\RouterInterface; +use Framework\Router\EasyRouter; + +/** + * 自定义路由策略. + * + * @author TIERGB + */ +class Userdefined implements RouterInterface +{ + /** + * 自定义路由规则 + * + * get请求 + * + * 查询数据 + * + * @var array + */ + private $getMap = []; + + /** + * 自定义路由规则 + * + * post请求 + * + * 新增数据 + * + * @var array + */ + private $postMap = []; + + /** + * 自定义路由规则 + * + * put请求 + * + * 更新数据 + * + * @var array + */ + private $putMap = []; + + /** + * 自定义路由规则 + * + * delete请求 + * + * 删除数据 + * + * @var array + */ + private $deleteMap = []; + + /** + * 自定义get请求路由 + * + * @param string $uri 请求uri + * @param mixed $function 匿名函数或者控制器方法标示 + * @return void + */ + public function get($uri = '', $function = '') + { + $this->getMap[$uri] = $function; + } + + /** + * 自定义post请求路由 + * + * @param string $uri 请求uri + * @param mixed $function 匿名函数或者控制器方法标示 + * @return void + */ + public function post($uri = '', $function = '') + { + $this->postMap[$uri] = $function; + } + + /** + * 自定义put请求路由 + * + * @param string $uri 请求uri + * @param mixed $function 匿名函数或者控制器方法标示 + * @return void + */ + public function put($uri = '', $function = '') + { + $this->putMap[$uri] = $function; + } + + /** + * 自定义delete请求路由 + * + * @param string $uri 请求uri + * @param mixed $function 匿名函数或者控制器方法标示 + * @return void + */ + public function delete($uri = '', $function = '') + { + $this->deleteMap[$uri] = $function; + } + + /** + * 路由方法 + * + * @param void + */ + public function route(EasyRouter $entrance) + { + if ($entrance->routeStrategy === 'job') { + return; + } + + $module = $entrance->config->config['module']; + foreach ($module as $v) { + // 加载自定义路由配置文件 + $routeFile = "{$entrance->app->rootPath}/config/{$v}/route.php"; + if (file_exists($routeFile)) { + require($routeFile); + } + } + $uri = "{$entrance->moduleName}/{$entrance->controllerName}/{$entrance->actionName}"; + $app = $entrance->app; + $request = $app::$container->getSingle('request'); + $method = $request->method . 'Map'; + if (! isset($this->$method)) { + throw new CoreHttpException( + 404, + 'Http Method:'. $request->method + ); + } + if (! array_key_exists($uri, $this->$method)) { + return false; + } + + // 执行自定义路由匿名函数 + $map = $this->$method; + $entrance->app->responseData = $map[$uri]($app); + if ($entrance->app->isCli === 'yes') { + $entrance->app->notOutput = false; + } + return true; + } +} diff --git a/framework/run.php b/framework/run.php index 985160c..0109bf0 100644 --- a/framework/run.php +++ b/framework/run.php @@ -1,13 +1,13 @@ * - * * - ********************************************/ +/************************************************* + * Easy PHP * + * * + * A Faster Lightweight Full-Stack PHP Framework * + * * + * TIERGB * + * * + * * + *************************************************/ use Framework\Handles\ErrorHandle; use Framework\Handles\ExceptionHandle; diff --git a/public/index.php b/public/index.php index ecca76d..fa3dcdc 100644 --- a/public/index.php +++ b/public/index.php @@ -1,14 +1,14 @@ * - * * - * Version: 0.7.0 * - ********************************************/ +/************************************************* + * Easy PHP * + * * + * A Faster Lightweight Full-Stack PHP Framework * + * * + * TIERGB * + * * + * * + * Version: 0.7.1 * + *************************************************/ // 载入框架运行文件 require(dirname(__DIR__) . DIRECTORY_SEPARATOR . 'framework/run.php');