@@ -18,7 +18,7 @@ discard block |
||
| 18 | 18 | * |
| 19 | 19 | * @var array |
| 20 | 20 | */ |
| 21 | - protected $loadedConfigurations = []; |
|
| 21 | + protected $loadedConfigurations=[]; |
|
| 22 | 22 | |
| 23 | 23 | /** |
| 24 | 24 | * 应用的根目录. |
@@ -32,21 +32,21 @@ discard block |
||
| 32 | 32 | $this->initContainer(); |
| 33 | 33 | $this->initConfig(); |
| 34 | 34 | } |
| 35 | - private function initConfig(){ |
|
| 35 | + private function initConfig() { |
|
| 36 | 36 | $this->configure('core'); |
| 37 | 37 | } |
| 38 | 38 | |
| 39 | - private function initEnv(){ |
|
| 40 | - try{ |
|
| 41 | - $dotEnv = new Dotenv($this->basePath); |
|
| 39 | + private function initEnv() { |
|
| 40 | + try { |
|
| 41 | + $dotEnv=new Dotenv($this->basePath); |
|
| 42 | 42 | $dotEnv->load(); |
| 43 | 43 | } |
| 44 | - catch (\Dotenv\Exception\InvalidPathException $e){ |
|
| 44 | + catch (\Dotenv\Exception\InvalidPathException $e) { |
|
| 45 | 45 | die("环境配置文件`.env`没有被找到."); |
| 46 | 46 | } |
| 47 | 47 | date_default_timezone_set(env('APP_TIMEZONE', 'Asia/Shanghai')); |
| 48 | 48 | define('IS_CLI', $this->runningInConsole()); |
| 49 | - define('IS_DEBUG',env('DEBUG',false)); |
|
| 49 | + define('IS_DEBUG', env('DEBUG', false)); |
|
| 50 | 50 | if (IS_DEBUG) { |
| 51 | 51 | error_reporting(E_ALL); |
| 52 | 52 | @ini_set('display_errors', 'On'); |
@@ -68,12 +68,12 @@ discard block |
||
| 68 | 68 | */ |
| 69 | 69 | private function initContainer() { |
| 70 | 70 | static::setInstance($this); |
| 71 | - $this->instance('app',$this); |
|
| 72 | - $this->instance('config',new Config()); |
|
| 73 | - $this->instance('request',new Request($this->config)); |
|
| 74 | - $this->instance('route',new Route($this->request)); |
|
| 75 | - $this->bind('pinyin','\puck\helpers\PinYin'); |
|
| 76 | - $this->bind('curl','\puck\helpers\Curl'); |
|
| 71 | + $this->instance('app', $this); |
|
| 72 | + $this->instance('config', new Config()); |
|
| 73 | + $this->instance('request', new Request($this->config)); |
|
| 74 | + $this->instance('route', new Route($this->request)); |
|
| 75 | + $this->bind('pinyin', '\puck\helpers\PinYin'); |
|
| 76 | + $this->bind('curl', '\puck\helpers\Curl'); |
|
| 77 | 77 | } |
| 78 | 78 | |
| 79 | 79 | /** |
@@ -82,16 +82,16 @@ discard block |
||
| 82 | 82 | * @param string|null $path |
| 83 | 83 | * @return string |
| 84 | 84 | */ |
| 85 | - public function basePath($path = null) |
|
| 85 | + public function basePath($path=null) |
|
| 86 | 86 | { |
| 87 | 87 | if (isset($this->basePath)) { |
| 88 | 88 | return $this->basePath.($path ? '/'.$path : $path); |
| 89 | 89 | } |
| 90 | 90 | |
| 91 | 91 | if ($this->runningInConsole()) { |
| 92 | - $this->basePath = getcwd(); |
|
| 92 | + $this->basePath=getcwd(); |
|
| 93 | 93 | } else { |
| 94 | - $this->basePath = realpath(getcwd().'/../'); |
|
| 94 | + $this->basePath=realpath(getcwd().'/../'); |
|
| 95 | 95 | } |
| 96 | 96 | |
| 97 | 97 | return $this->basePath($path); |
@@ -117,8 +117,8 @@ discard block |
||
| 117 | 117 | return; |
| 118 | 118 | } |
| 119 | 119 | //标记为已加载 |
| 120 | - $this->loadedConfigurations[$name] = true; |
|
| 121 | - $path = $this->getConfigurationPath($name); |
|
| 120 | + $this->loadedConfigurations[$name]=true; |
|
| 121 | + $path=$this->getConfigurationPath($name); |
|
| 122 | 122 | if ($path) { |
| 123 | 123 | $this->make('config')->set($name, require $path); |
| 124 | 124 | } |
@@ -135,21 +135,21 @@ discard block |
||
| 135 | 135 | * @param string|null $name |
| 136 | 136 | * @return string |
| 137 | 137 | */ |
| 138 | - public function getConfigurationPath($name = null) |
|
| 138 | + public function getConfigurationPath($name=null) |
|
| 139 | 139 | { |
| 140 | - if (! $name) { |
|
| 141 | - $appConfigDir = $this->basePath('configs').'/'; |
|
| 140 | + if (!$name) { |
|
| 141 | + $appConfigDir=$this->basePath('configs').'/'; |
|
| 142 | 142 | |
| 143 | 143 | if (file_exists($appConfigDir)) { |
| 144 | 144 | return $appConfigDir; |
| 145 | - } elseif (file_exists($path = __DIR__.'/configs/')) { |
|
| 145 | + } elseif (file_exists($path=__DIR__.'/configs/')) { |
|
| 146 | 146 | return $path; |
| 147 | 147 | } |
| 148 | 148 | } else { |
| 149 | - $appConfigPath = $this->basePath('configs').'/'.$name.'.php'; |
|
| 149 | + $appConfigPath=$this->basePath('configs').'/'.$name.'.php'; |
|
| 150 | 150 | if (file_exists($appConfigPath)) { |
| 151 | 151 | return $appConfigPath; |
| 152 | - } elseif (file_exists($path = __DIR__.'/configs/'.$name.'.php')) { |
|
| 152 | + } elseif (file_exists($path=__DIR__.'/configs/'.$name.'.php')) { |
|
| 153 | 153 | return $path; |
| 154 | 154 | } |
| 155 | 155 | } |
@@ -40,8 +40,7 @@ |
||
| 40 | 40 | try{ |
| 41 | 41 | $dotEnv = new Dotenv($this->basePath); |
| 42 | 42 | $dotEnv->load(); |
| 43 | - } |
|
| 44 | - catch (\Dotenv\Exception\InvalidPathException $e){ |
|
| 43 | + } catch (\Dotenv\Exception\InvalidPathException $e){ |
|
| 45 | 44 | die("环境配置文件`.env`没有被找到."); |
| 46 | 45 | } |
| 47 | 46 | date_default_timezone_set(env('APP_TIMEZONE', 'Asia/Shanghai')); |
@@ -9,18 +9,18 @@ discard block |
||
| 9 | 9 | private $request; |
| 10 | 10 | |
| 11 | 11 | public function __construct(Request $request) { |
| 12 | - $this->request = $request; |
|
| 13 | - $this->errorCallback=function (){ |
|
| 12 | + $this->request=$request; |
|
| 13 | + $this->errorCallback=function() { |
|
| 14 | 14 | die("404"); |
| 15 | 15 | }; |
| 16 | 16 | } |
| 17 | 17 | |
| 18 | - public static $halts = false; |
|
| 19 | - private $routes = array(); |
|
| 20 | - private $regexRoutes = []; |
|
| 21 | - public static $methods = array(); |
|
| 22 | - public static $callbacks = array(); |
|
| 23 | - private $patterns = array( |
|
| 18 | + public static $halts=false; |
|
| 19 | + private $routes=array(); |
|
| 20 | + private $regexRoutes=[]; |
|
| 21 | + public static $methods=array(); |
|
| 22 | + public static $callbacks=array(); |
|
| 23 | + private $patterns=array( |
|
| 24 | 24 | ':any' => '[^/]+', |
| 25 | 25 | ':num' => '[0-9]+', |
| 26 | 26 | ':all' => '.*' |
@@ -41,29 +41,29 @@ discard block |
||
| 41 | 41 | * @param mixed $callBack |
| 42 | 42 | */ |
| 43 | 43 | public function addRoute($method, $uri, $callBack) { |
| 44 | - $method = strtoupper($method); |
|
| 44 | + $method=strtoupper($method); |
|
| 45 | 45 | //预定义正则路由 |
| 46 | 46 | if (strpos($uri, ':') !== false) { |
| 47 | - $searches = array_keys($this->patterns); |
|
| 48 | - $replaces = array_values($this->patterns); |
|
| 49 | - $uri = str_replace($searches, $replaces, $uri); |
|
| 50 | - $this->regexRoutes[] = [ |
|
| 47 | + $searches=array_keys($this->patterns); |
|
| 48 | + $replaces=array_values($this->patterns); |
|
| 49 | + $uri=str_replace($searches, $replaces, $uri); |
|
| 50 | + $this->regexRoutes[]=[ |
|
| 51 | 51 | 'method' => $method, |
| 52 | - 'regex' => '#^' . $uri . '$#', |
|
| 52 | + 'regex' => '#^'.$uri.'$#', |
|
| 53 | 53 | 'callback' => $callBack |
| 54 | 54 | ]; |
| 55 | 55 | } //自定义正则路由 |
| 56 | 56 | elseif ($uri[0] == '#' |
| 57 | 57 | || (strlen($uri) > 2 && tools\Str::endsWith($uri, '/') && tools\Str::startsWith($uri, '/')) |
| 58 | 58 | ) { |
| 59 | - $this->regexRoutes[] = [ |
|
| 59 | + $this->regexRoutes[]=[ |
|
| 60 | 60 | 'method' => $method, |
| 61 | 61 | 'regex' => $uri, |
| 62 | 62 | 'callback' => $callBack |
| 63 | 63 | ]; |
| 64 | 64 | } //直接定义的路由 |
| 65 | 65 | else { |
| 66 | - $this->routes[$method . $uri] = [ |
|
| 66 | + $this->routes[$method.$uri]=[ |
|
| 67 | 67 | 'method' => $method, |
| 68 | 68 | 'uri' => $uri, |
| 69 | 69 | 'callback' => $callBack |
@@ -75,25 +75,25 @@ discard block |
||
| 75 | 75 | * Defines callback if route is not found |
| 76 | 76 | */ |
| 77 | 77 | public function error($callback) { |
| 78 | - $this->errorCallback = $callback; |
|
| 78 | + $this->errorCallback=$callback; |
|
| 79 | 79 | } |
| 80 | 80 | |
| 81 | - public static function haltOnMatch($flag = true) { |
|
| 82 | - self::$halts = $flag; |
|
| 81 | + public static function haltOnMatch($flag=true) { |
|
| 82 | + self::$halts=$flag; |
|
| 83 | 83 | } |
| 84 | 84 | |
| 85 | 85 | |
| 86 | - private function foundRoute($route, $param = []) { |
|
| 86 | + private function foundRoute($route, $param=[]) { |
|
| 87 | 87 | try { |
| 88 | 88 | if ($route['callback'] instanceof \Closure) { |
| 89 | - app()->invokeFunction($route['callback'],$param); |
|
| 89 | + app()->invokeFunction($route['callback'], $param); |
|
| 90 | 90 | } else { |
| 91 | 91 | // Grab all parts based on a / separator |
| 92 | - $parts = explode('/', $route['callback']); |
|
| 92 | + $parts=explode('/', $route['callback']); |
|
| 93 | 93 | // Collect the last index of the array |
| 94 | - $last = end($parts); |
|
| 94 | + $last=end($parts); |
|
| 95 | 95 | // Grab the controller name and method call |
| 96 | - $segments = explode('@', $last); |
|
| 96 | + $segments=explode('@', $last); |
|
| 97 | 97 | app()->invokeMethod($segments, $param); |
| 98 | 98 | } |
| 99 | 99 | } catch (\ReflectionException $e) { |
@@ -125,20 +125,20 @@ discard block |
||
| 125 | 125 | */ |
| 126 | 126 | public function dispatch() { |
| 127 | 127 | $uri=$this->request->path(); |
| 128 | - $current['uri'] = $uri?$uri:'/'; |
|
| 129 | - $current['method'] = $this->request->method(); |
|
| 128 | + $current['uri']=$uri ? $uri : '/'; |
|
| 129 | + $current['method']=$this->request->method(); |
|
| 130 | 130 | # 第一种情况,直接命中 |
| 131 | - if (isset($this->routes[$current['method'] . $current['uri']])) { |
|
| 132 | - $this->foundRoute($this->routes[$current['method'] . $current['uri']]); |
|
| 131 | + if (isset($this->routes[$current['method'].$current['uri']])) { |
|
| 132 | + $this->foundRoute($this->routes[$current['method'].$current['uri']]); |
|
| 133 | 133 | } # 第二种情况,any命中 |
| 134 | - else if (isset($this->routes['ANY' . $current['uri']])) { |
|
| 135 | - $this->foundRoute($this->routes['ANY' . $current['method']]); |
|
| 134 | + else if (isset($this->routes['ANY'.$current['uri']])) { |
|
| 135 | + $this->foundRoute($this->routes['ANY'.$current['method']]); |
|
| 136 | 136 | } # 第三种情况,正则命中 |
| 137 | 137 | else { |
| 138 | 138 | if ($this->foundRegexRoute($current)) { |
| 139 | 139 | |
| 140 | 140 | } else { |
| 141 | - $route['callback'] = $this->errorCallback; |
|
| 141 | + $route['callback']=$this->errorCallback; |
|
| 142 | 142 | $this->foundRoute($route); |
| 143 | 143 | } |
| 144 | 144 | } |