@@ -54,7 +54,7 @@ discard block |
||
54 | 54 | } |
55 | 55 | |
56 | 56 | /** |
57 | - * @param null $key |
|
57 | + * @param string $key |
|
58 | 58 | * @return array |
59 | 59 | */ |
60 | 60 | public function getRoutes($key = null) |
@@ -96,7 +96,7 @@ discard block |
||
96 | 96 | |
97 | 97 | /** |
98 | 98 | * @param array $args |
99 | - * @param $i |
|
99 | + * @param integer $i |
|
100 | 100 | */ |
101 | 101 | private function setRoutes($args = [], $i) |
102 | 102 | { |
@@ -152,7 +152,7 @@ discard block |
||
152 | 152 | } |
153 | 153 | |
154 | 154 | /** |
155 | - * @param $url |
|
155 | + * @param string $url |
|
156 | 156 | * @return bool |
157 | 157 | */ |
158 | 158 | public function getDomain($url) |
@@ -43,12 +43,12 @@ discard block |
||
43 | 43 | public function addRoutes($routes = null, $options = []) |
44 | 44 | { |
45 | 45 | if (!is_null($routes) && !is_array($routes)) { |
46 | - if (strpos($routes, '.php') === false) $routes = trim($routes, '/') . '/'; |
|
47 | - if (is_file($routes . '/routes.php') && is_array($routesFile = include $routes . '/routes.php')) $routes = $routesFile; |
|
46 | + if (strpos($routes, '.php') === false) $routes = trim($routes, '/').'/'; |
|
47 | + if (is_file($routes.'/routes.php') && is_array($routesFile = include $routes.'/routes.php')) $routes = $routesFile; |
|
48 | 48 | elseif (is_file($routes) && is_array($routesFile = include $routes)) $routes = $routesFile; |
49 | - else throw new \InvalidArgumentException('Argument for "' . get_called_class() . '" constructor is not recognized. Expected argument array or file containing array but "' . $routes . '" given'); |
|
49 | + else throw new \InvalidArgumentException('Argument for "'.get_called_class().'" constructor is not recognized. Expected argument array or file containing array but "'.$routes.'" given'); |
|
50 | 50 | } |
51 | - $this->routes['routes_' . $this->countRoutes] = is_array($routes) ? $routes : []; |
|
51 | + $this->routes['routes_'.$this->countRoutes] = is_array($routes) ? $routes : []; |
|
52 | 52 | $this->setRoutes($options, $this->countRoutes); |
53 | 53 | $this->countRoutes++; |
54 | 54 | } |
@@ -72,10 +72,10 @@ discard block |
||
72 | 72 | if (is_array($args)) { |
73 | 73 | $nbrArgs = count($args); |
74 | 74 | for ($i = 0; $i < $nbrArgs; ++$i) |
75 | - $this->routes['prefix_' . $i] = '/' . trim($args[$i], '/'); |
|
75 | + $this->routes['prefix_'.$i] = '/'.trim($args[$i], '/'); |
|
76 | 76 | } elseif (is_string($args)) |
77 | 77 | for ($i = 0; $i < $this->countRoutes; ++$i) |
78 | - $this->routes['prefix_' . $i] = '/' . trim($args, '/'); |
|
78 | + $this->routes['prefix_'.$i] = '/'.trim($args, '/'); |
|
79 | 79 | if ($this->countRoutes == 0) $this->countRoutes++; |
80 | 80 | } |
81 | 81 | |
@@ -88,7 +88,7 @@ discard block |
||
88 | 88 | for ($i = 0; $i < $nbrArgs; ++$i) { |
89 | 89 | if (is_array($args[$i])) { |
90 | 90 | $this->setRoutes($args[$i], $i); |
91 | - if (!isset($this->routes['routes_' . $i])) $this->routes['routes_' . $i] = []; |
|
91 | + if (!isset($this->routes['routes_'.$i])) $this->routes['routes_'.$i] = []; |
|
92 | 92 | } |
93 | 93 | } |
94 | 94 | if ($this->countRoutes == 0) $this->countRoutes++; |
@@ -100,11 +100,11 @@ discard block |
||
100 | 100 | */ |
101 | 101 | private function setRoutes($args = [], $i) |
102 | 102 | { |
103 | - $this->routes['block_' . $i] = (isset($args['block']) && !empty($args['block'])) ? rtrim($args['block'], '/') . '/' : ''; |
|
104 | - $this->routes['view_dir_' . $i] = (isset($args['view_dir']) && !empty($args['view_dir'])) ? rtrim($args['view_dir'], '/') . '/' : ''; |
|
105 | - $this->routes['ctrl_namespace_' . $i] = (isset($args['ctrl_namespace']) && !empty($args['ctrl_namespace'])) ? trim($args['ctrl_namespace'], '\\') . '\\' : ''; |
|
106 | - $this->routes['prefix_' . $i] = (isset($args['prefix']) && !empty($args['prefix'])) ? '/' . trim($args['prefix'], '/') : ''; |
|
107 | - $this->routes['subdomain_' . $i] = (isset($args['subdomain'])) ? $args['subdomain'] : ''; |
|
103 | + $this->routes['block_'.$i] = (isset($args['block']) && !empty($args['block'])) ? rtrim($args['block'], '/').'/' : ''; |
|
104 | + $this->routes['view_dir_'.$i] = (isset($args['view_dir']) && !empty($args['view_dir'])) ? rtrim($args['view_dir'], '/').'/' : ''; |
|
105 | + $this->routes['ctrl_namespace_'.$i] = (isset($args['ctrl_namespace']) && !empty($args['ctrl_namespace'])) ? trim($args['ctrl_namespace'], '\\').'\\' : ''; |
|
106 | + $this->routes['prefix_'.$i] = (isset($args['prefix']) && !empty($args['prefix'])) ? '/'.trim($args['prefix'], '/') : ''; |
|
107 | + $this->routes['subdomain_'.$i] = (isset($args['subdomain'])) ? $args['subdomain'] : ''; |
|
108 | 108 | } |
109 | 109 | |
110 | 110 | /** |
@@ -126,24 +126,24 @@ discard block |
||
126 | 126 | */ |
127 | 127 | public function generateRoutesPath() |
128 | 128 | { |
129 | - $root = (isset($_SERVER['REQUEST_SCHEME'])?$_SERVER['REQUEST_SCHEME']:'http') . '://' . ($domain = (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME'])) . str_replace('/index.php', '', $_SERVER['SCRIPT_NAME']); |
|
129 | + $root = (isset($_SERVER['REQUEST_SCHEME']) ? $_SERVER['REQUEST_SCHEME'] : 'http').'://'.($domain = (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME'])).str_replace('/index.php', '', $_SERVER['SCRIPT_NAME']); |
|
130 | 130 | if (strpos($domain, ($new_domain = $this->getDomain($root))) !== false) |
131 | 131 | $root = str_replace($domain, $new_domain, $root); |
132 | 132 | $count = 0; |
133 | 133 | for ($i = 0; $i < $this->countRoutes; ++$i) { |
134 | - $prefix = (isset($this->routes['prefix_' . $i])) ? $this->routes['prefix_' . $i] : ''; |
|
135 | - if (isset($this->routes['routes_' . $i])) |
|
136 | - foreach ($this->routes['routes_' . $i] as $route => $dependencies) { |
|
134 | + $prefix = (isset($this->routes['prefix_'.$i])) ? $this->routes['prefix_'.$i] : ''; |
|
135 | + if (isset($this->routes['routes_'.$i])) |
|
136 | + foreach ($this->routes['routes_'.$i] as $route => $dependencies) { |
|
137 | 137 | if (is_array($dependencies) && isset($dependencies['use'])) |
138 | - $use = (is_callable($dependencies['use'])) ? 'closure-' . $count : trim($dependencies['use'], '/'); |
|
138 | + $use = (is_callable($dependencies['use'])) ? 'closure-'.$count : trim($dependencies['use'], '/'); |
|
139 | 139 | elseif (!is_array($dependencies)) |
140 | - $use = (is_callable($dependencies)) ? 'closure-' . $count : trim($dependencies, '/'); |
|
140 | + $use = (is_callable($dependencies)) ? 'closure-'.$count : trim($dependencies, '/'); |
|
141 | 141 | else |
142 | 142 | $use = $route; |
143 | 143 | if (isset($route[0]) && $route[0] == '/') { |
144 | - (!is_callable($dependencies) && isset($dependencies['name'])) ? $this->routesByName[$use . '#' . $dependencies['name']] = $root . $prefix . $route : $this->routesByName[$use] = $root . $prefix . $route; |
|
144 | + (!is_callable($dependencies) && isset($dependencies['name'])) ? $this->routesByName[$use.'#'.$dependencies['name']] = $root.$prefix.$route : $this->routesByName[$use] = $root.$prefix.$route; |
|
145 | 145 | } else { |
146 | - (!is_callable($dependencies) && isset($dependencies['name'])) ? $this->routesByName[$use . '#' . $dependencies['name']] = $_SERVER['REQUEST_SCHEME'] . '://' . str_replace('{host}', $new_domain, $route) . $prefix : $this->routesByName[$use] = $_SERVER['REQUEST_SCHEME'] . '://' . str_replace('{host}', $new_domain, $route) . $prefix; |
|
146 | + (!is_callable($dependencies) && isset($dependencies['name'])) ? $this->routesByName[$use.'#'.$dependencies['name']] = $_SERVER['REQUEST_SCHEME'].'://'.str_replace('{host}', $new_domain, $route).$prefix : $this->routesByName[$use] = $_SERVER['REQUEST_SCHEME'].'://'.str_replace('{host}', $new_domain, $route).$prefix; |
|
147 | 147 | } |
148 | 148 | $count++; |
149 | 149 | } |
@@ -176,7 +176,7 @@ discard block |
||
176 | 176 | foreach ($this->routesByName as $key => $route) { |
177 | 177 | $param = explode('#', $key); |
178 | 178 | $route = str_replace('{subdomain}', $subdomain, $route); |
179 | - foreach ($params as $key2 => $value) $route = str_replace(':' . $key2, $value, $route); |
|
179 | + foreach ($params as $key2 => $value) $route = str_replace(':'.$key2, $value, $route); |
|
180 | 180 | if ($param[0] == trim($name, '/')) return $route; |
181 | 181 | else if (isset($param[1]) && $param[1] == $name) return $route; |
182 | 182 | } |
@@ -33,7 +33,9 @@ discard block |
||
33 | 33 | */ |
34 | 34 | public function __construct($routes = null, $options = []) |
35 | 35 | { |
36 | - if (!is_null($routes) || !empty($options)) $this->addRoutes($routes, $options); |
|
36 | + if (!is_null($routes) || !empty($options)) { |
|
37 | + $this->addRoutes($routes, $options); |
|
38 | + } |
|
37 | 39 | } |
38 | 40 | |
39 | 41 | /** |
@@ -43,10 +45,16 @@ discard block |
||
43 | 45 | public function addRoutes($routes = null, $options = []) |
44 | 46 | { |
45 | 47 | if (!is_null($routes) && !is_array($routes)) { |
46 | - if (strpos($routes, '.php') === false) $routes = trim($routes, '/') . '/'; |
|
47 | - if (is_file($routes . '/routes.php') && is_array($routesFile = include $routes . '/routes.php')) $routes = $routesFile; |
|
48 | - elseif (is_file($routes) && is_array($routesFile = include $routes)) $routes = $routesFile; |
|
49 | - else throw new \InvalidArgumentException('Argument for "' . get_called_class() . '" constructor is not recognized. Expected argument array or file containing array but "' . $routes . '" given'); |
|
48 | + if (strpos($routes, '.php') === false) { |
|
49 | + $routes = trim($routes, '/') . '/'; |
|
50 | + } |
|
51 | + if (is_file($routes . '/routes.php') && is_array($routesFile = include $routes . '/routes.php')) { |
|
52 | + $routes = $routesFile; |
|
53 | + } elseif (is_file($routes) && is_array($routesFile = include $routes)) { |
|
54 | + $routes = $routesFile; |
|
55 | + } else { |
|
56 | + throw new \InvalidArgumentException('Argument for "' . get_called_class() . '" constructor is not recognized. Expected argument array or file containing array but "' . $routes . '" given'); |
|
57 | + } |
|
50 | 58 | } |
51 | 59 | $this->routes['routes_' . $this->countRoutes] = is_array($routes) ? $routes : []; |
52 | 60 | $this->setRoutes($options, $this->countRoutes); |
@@ -59,8 +67,9 @@ discard block |
||
59 | 67 | */ |
60 | 68 | public function getRoutes($key = null) |
61 | 69 | { |
62 | - if (!is_null($key)) |
|
63 | - return isset($this->routes[$key]) ? $this->routes[$key] : ''; |
|
70 | + if (!is_null($key)) { |
|
71 | + return isset($this->routes[$key]) ? $this->routes[$key] : ''; |
|
72 | + } |
|
64 | 73 | return $this->routes; |
65 | 74 | } |
66 | 75 | |
@@ -71,12 +80,18 @@ discard block |
||
71 | 80 | { |
72 | 81 | if (is_array($args)) { |
73 | 82 | $nbrArgs = count($args); |
74 | - for ($i = 0; $i < $nbrArgs; ++$i) |
|
75 | - $this->routes['prefix_' . $i] = '/' . trim($args[$i], '/'); |
|
76 | - } elseif (is_string($args)) |
|
77 | - for ($i = 0; $i < $this->countRoutes; ++$i) |
|
78 | - $this->routes['prefix_' . $i] = '/' . trim($args, '/'); |
|
79 | - if ($this->countRoutes == 0) $this->countRoutes++; |
|
83 | + for ($i = 0; $i < $nbrArgs; ++$i) { |
|
84 | + $this->routes['prefix_' . $i] = '/' . trim($args[$i], '/'); |
|
85 | + } |
|
86 | + } elseif (is_string($args)) { |
|
87 | + for ($i = 0; |
|
88 | + } |
|
89 | + $i < $this->countRoutes; ++$i) { |
|
90 | + $this->routes['prefix_' . $i] = '/' . trim($args, '/'); |
|
91 | + } |
|
92 | + if ($this->countRoutes == 0) { |
|
93 | + $this->countRoutes++; |
|
94 | + } |
|
80 | 95 | } |
81 | 96 | |
82 | 97 | /** |
@@ -88,10 +103,14 @@ discard block |
||
88 | 103 | for ($i = 0; $i < $nbrArgs; ++$i) { |
89 | 104 | if (is_array($args[$i])) { |
90 | 105 | $this->setRoutes($args[$i], $i); |
91 | - if (!isset($this->routes['routes_' . $i])) $this->routes['routes_' . $i] = []; |
|
106 | + if (!isset($this->routes['routes_' . $i])) { |
|
107 | + $this->routes['routes_' . $i] = []; |
|
108 | + } |
|
92 | 109 | } |
93 | 110 | } |
94 | - if ($this->countRoutes == 0) $this->countRoutes++; |
|
111 | + if ($this->countRoutes == 0) { |
|
112 | + $this->countRoutes++; |
|
113 | + } |
|
95 | 114 | } |
96 | 115 | |
97 | 116 | /** |
@@ -113,12 +132,16 @@ discard block |
||
113 | 132 | */ |
114 | 133 | public function setMiddleware($middleware) |
115 | 134 | { |
116 | - if (is_string($middleware)) $middleware = rtrim($middleware, '/'); |
|
117 | - if (is_array($middleware)) |
|
118 | - $this->middleware = $middleware; |
|
119 | - elseif (is_file($middleware) && is_array($mid = include $middleware)) |
|
120 | - $this->middleware = $mid; |
|
121 | - else throw new \InvalidArgumentException('Accepted argument for setMiddleware are array and array file'); |
|
135 | + if (is_string($middleware)) { |
|
136 | + $middleware = rtrim($middleware, '/'); |
|
137 | + } |
|
138 | + if (is_array($middleware)) { |
|
139 | + $this->middleware = $middleware; |
|
140 | + } elseif (is_file($middleware) && is_array($mid = include $middleware)) { |
|
141 | + $this->middleware = $mid; |
|
142 | + } else { |
|
143 | + throw new \InvalidArgumentException('Accepted argument for setMiddleware are array and array file'); |
|
144 | + } |
|
122 | 145 | } |
123 | 146 | |
124 | 147 | /** |
@@ -127,19 +150,21 @@ discard block |
||
127 | 150 | public function generateRoutesPath() |
128 | 151 | { |
129 | 152 | $root = (isset($_SERVER['REQUEST_SCHEME'])?$_SERVER['REQUEST_SCHEME']:'http') . '://' . ($domain = (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME'])) . str_replace('/index.php', '', $_SERVER['SCRIPT_NAME']); |
130 | - if (strpos($domain, ($new_domain = $this->getDomain($root))) !== false) |
|
131 | - $root = str_replace($domain, $new_domain, $root); |
|
153 | + if (strpos($domain, ($new_domain = $this->getDomain($root))) !== false) { |
|
154 | + $root = str_replace($domain, $new_domain, $root); |
|
155 | + } |
|
132 | 156 | $count = 0; |
133 | 157 | for ($i = 0; $i < $this->countRoutes; ++$i) { |
134 | 158 | $prefix = (isset($this->routes['prefix_' . $i])) ? $this->routes['prefix_' . $i] : ''; |
135 | - if (isset($this->routes['routes_' . $i])) |
|
136 | - foreach ($this->routes['routes_' . $i] as $route => $dependencies) { |
|
159 | + if (isset($this->routes['routes_' . $i])) { |
|
160 | + foreach ($this->routes['routes_' . $i] as $route => $dependencies) { |
|
137 | 161 | if (is_array($dependencies) && isset($dependencies['use'])) |
138 | - $use = (is_callable($dependencies['use'])) ? 'closure-' . $count : trim($dependencies['use'], '/'); |
|
139 | - elseif (!is_array($dependencies)) |
|
140 | - $use = (is_callable($dependencies)) ? 'closure-' . $count : trim($dependencies, '/'); |
|
141 | - else |
|
142 | - $use = $route; |
|
162 | + $use = (is_callable($dependencies['use'])) ? 'closure-' . $count : trim($dependencies['use'], '/'); |
|
163 | + } elseif (!is_array($dependencies)) { |
|
164 | + $use = (is_callable($dependencies)) ? 'closure-' . $count : trim($dependencies, '/'); |
|
165 | + } else { |
|
166 | + $use = $route; |
|
167 | + } |
|
143 | 168 | if (isset($route[0]) && $route[0] == '/') { |
144 | 169 | (!is_callable($dependencies) && isset($dependencies['name'])) ? $this->routesByName[$use . '#' . $dependencies['name']] = $root . $prefix . $route : $this->routesByName[$use] = $root . $prefix . $route; |
145 | 170 | } else { |
@@ -176,9 +201,14 @@ discard block |
||
176 | 201 | foreach ($this->routesByName as $key => $route) { |
177 | 202 | $param = explode('#', $key); |
178 | 203 | $route = str_replace('{subdomain}', $subdomain, $route); |
179 | - foreach ($params as $key2 => $value) $route = str_replace(':' . $key2, $value, $route); |
|
180 | - if ($param[0] == trim($name, '/')) return $route; |
|
181 | - else if (isset($param[1]) && $param[1] == $name) return $route; |
|
204 | + foreach ($params as $key2 => $value) { |
|
205 | + $route = str_replace(':' . $key2, $value, $route); |
|
206 | + } |
|
207 | + if ($param[0] == trim($name, '/')) { |
|
208 | + return $route; |
|
209 | + } else if (isset($param[1]) && $param[1] == $name) { |
|
210 | + return $route; |
|
211 | + } |
|
182 | 212 | } |
183 | 213 | return null; |
184 | 214 | } |
@@ -25,7 +25,7 @@ discard block |
||
25 | 25 | /** |
26 | 26 | * @var array |
27 | 27 | */ |
28 | - private $resolver = ['isClosureAndTemplate','isControllerAndTemplate','isTemplate']; |
|
28 | + private $resolver = ['isClosureAndTemplate', 'isControllerAndTemplate', 'isTemplate']; |
|
29 | 29 | |
30 | 30 | /** |
31 | 31 | * @var array |
@@ -34,8 +34,8 @@ discard block |
||
34 | 34 | 'isClosure' => 'JetFire\Routing\Dispatcher\ClosureDispatcher', |
35 | 35 | 'isController' => 'JetFire\Routing\Dispatcher\ControllerDispatcher', |
36 | 36 | 'isTemplate' => 'JetFire\Routing\Dispatcher\TemplateDispatcher', |
37 | - 'isControllerAndTemplate' => ['JetFire\Routing\Dispatcher\ControllerDispatcher','JetFire\Routing\Dispatcher\TemplateDispatcher'], |
|
38 | - 'isClosureAndTemplate' => ['JetFire\Routing\Dispatcher\ClosureDispatcher','JetFire\Routing\Dispatcher\TemplateDispatcher'], |
|
37 | + 'isControllerAndTemplate' => ['JetFire\Routing\Dispatcher\ControllerDispatcher', 'JetFire\Routing\Dispatcher\TemplateDispatcher'], |
|
38 | + 'isClosureAndTemplate' => ['JetFire\Routing\Dispatcher\ClosureDispatcher', 'JetFire\Routing\Dispatcher\TemplateDispatcher'], |
|
39 | 39 | ]; |
40 | 40 | |
41 | 41 | /** |
@@ -49,14 +49,14 @@ discard block |
||
49 | 49 | /** |
50 | 50 | * @param array $resolver |
51 | 51 | */ |
52 | - public function setResolver($resolver = []){ |
|
52 | + public function setResolver($resolver = []) { |
|
53 | 53 | $this->resolver = $resolver; |
54 | 54 | } |
55 | 55 | |
56 | 56 | /** |
57 | 57 | * @param string $resolver |
58 | 58 | */ |
59 | - public function addResolver($resolver){ |
|
59 | + public function addResolver($resolver) { |
|
60 | 60 | $this->resolver[] = $resolver; |
61 | 61 | } |
62 | 62 | |
@@ -71,7 +71,7 @@ discard block |
||
71 | 71 | /** |
72 | 72 | * @param array $dispatcher |
73 | 73 | */ |
74 | - public function setDispatcher($dispatcher = []){ |
|
74 | + public function setDispatcher($dispatcher = []) { |
|
75 | 75 | $this->dispatcher = $dispatcher; |
76 | 76 | } |
77 | 77 | |
@@ -81,7 +81,7 @@ discard block |
||
81 | 81 | * @return mixed|void |
82 | 82 | * @internal param array $dispatcher |
83 | 83 | */ |
84 | - public function addDispatcher($method,$class){ |
|
84 | + public function addDispatcher($method, $class) { |
|
85 | 85 | $this->dispatcher[$method] = $class; |
86 | 86 | } |
87 | 87 | |
@@ -92,15 +92,15 @@ discard block |
||
92 | 92 | { |
93 | 93 | $this->request = []; |
94 | 94 | for ($i = 0; $i < $this->router->collection->countRoutes; ++$i) { |
95 | - $this->request['prefix'] = ($this->router->collection->getRoutes('prefix_' . $i) != '') ? $this->router->collection->getRoutes('prefix_' . $i) : ''; |
|
96 | - $this->request['subdomain'] = ($this->router->collection->getRoutes('subdomain_' . $i) != '') ? $this->router->collection->getRoutes('subdomain_' . $i) : ''; |
|
97 | - foreach ($this->router->collection->getRoutes('routes_' . $i) as $route => $params) { |
|
95 | + $this->request['prefix'] = ($this->router->collection->getRoutes('prefix_'.$i) != '') ? $this->router->collection->getRoutes('prefix_'.$i) : ''; |
|
96 | + $this->request['subdomain'] = ($this->router->collection->getRoutes('subdomain_'.$i) != '') ? $this->router->collection->getRoutes('subdomain_'.$i) : ''; |
|
97 | + foreach ($this->router->collection->getRoutes('routes_'.$i) as $route => $params) { |
|
98 | 98 | $this->request['params'] = $params; |
99 | 99 | $this->request['collection_index'] = $i; |
100 | - if($this->checkSubdomain($route)) { |
|
100 | + if ($this->checkSubdomain($route)) { |
|
101 | 101 | $route = strstr($route, '/'); |
102 | - $this->request['route'] = preg_replace_callback('#:([\w]+)#', [$this, 'paramMatch'], '/' . trim(trim($this->request['prefix'], '/') . '/' . trim($route, '/'), '/')); |
|
103 | - if ($this->routeMatch('#^' . $this->request['route'] . '$#')) { |
|
102 | + $this->request['route'] = preg_replace_callback('#:([\w]+)#', [$this, 'paramMatch'], '/'.trim(trim($this->request['prefix'], '/').'/'.trim($route, '/'), '/')); |
|
103 | + if ($this->routeMatch('#^'.$this->request['route'].'$#')) { |
|
104 | 104 | $this->setCallback(); |
105 | 105 | return $this->generateTarget(); |
106 | 106 | } |
@@ -114,17 +114,17 @@ discard block |
||
114 | 114 | * @param $route |
115 | 115 | * @return bool |
116 | 116 | */ |
117 | - private function checkSubdomain($route){ |
|
118 | - $url = (isset($_SERVER['REQUEST_SCHEME']) ? $_SERVER['REQUEST_SCHEME'] : 'http') . '://' . ($host = (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME'])); |
|
117 | + private function checkSubdomain($route) { |
|
118 | + $url = (isset($_SERVER['REQUEST_SCHEME']) ? $_SERVER['REQUEST_SCHEME'] : 'http').'://'.($host = (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME'])); |
|
119 | 119 | $domain = $this->router->collection->getDomain($url); |
120 | - if(!empty($this->request['subdomain']) && $route[0] == '/') $route = trim($this->request['subdomain'],'.').'.'.$domain.$route; |
|
121 | - if($route[0] == '/'){ |
|
120 | + if (!empty($this->request['subdomain']) && $route[0] == '/') $route = trim($this->request['subdomain'], '.').'.'.$domain.$route; |
|
121 | + if ($route[0] == '/') { |
|
122 | 122 | return ($host != $domain) ? false : true; |
123 | - }elseif($route[0] != '/' && $host != $domain){ |
|
123 | + }elseif ($route[0] != '/' && $host != $domain) { |
|
124 | 124 | $route = substr($route, 0, strpos($route, "/")); |
125 | 125 | $route = str_replace('{host}', $domain, $route); |
126 | 126 | $route = preg_replace_callback('#{subdomain}#', [$this, 'subdomainMatch'], $route); |
127 | - if (preg_match('#^' . $route . '$#', $host, $this->request['called_subdomain'])) { |
|
127 | + if (preg_match('#^'.$route.'$#', $host, $this->request['called_subdomain'])) { |
|
128 | 128 | $this->request['called_subdomain'] = array_shift($this->request['called_subdomain']); |
129 | 129 | return true; |
130 | 130 | } |
@@ -139,7 +139,7 @@ discard block |
||
139 | 139 | { |
140 | 140 | if (is_array($this->request['params']) && isset($this->request['params']['subdomain'])) { |
141 | 141 | $this->request['params']['subdomain'] = str_replace('(', '(?:', $this->request['params']['subdomain']); |
142 | - return '(' . $this->request['params']['subdomain'] . ')'; |
|
142 | + return '('.$this->request['params']['subdomain'].')'; |
|
143 | 143 | } |
144 | 144 | return '([^/]+)'; |
145 | 145 | } |
@@ -152,7 +152,7 @@ discard block |
||
152 | 152 | { |
153 | 153 | if (is_array($this->request['params']) && isset($this->request['params']['arguments'][$match[1]])) { |
154 | 154 | $this->request['params']['arguments'][$match[1]] = str_replace('(', '(?:', $this->request['params']['arguments'][$match[1]]); |
155 | - return '(' . $this->request['params']['arguments'][$match[1]] . ')'; |
|
155 | + return '('.$this->request['params']['arguments'][$match[1]].')'; |
|
156 | 156 | } |
157 | 157 | return '([^/]+)'; |
158 | 158 | } |
@@ -180,9 +180,9 @@ discard block |
||
180 | 180 | */ |
181 | 181 | private function generateTarget() |
182 | 182 | { |
183 | - if($this->validMethod()) |
|
184 | - foreach($this->resolver as $resolver) |
|
185 | - if (is_array($target = call_user_func_array([$this,$resolver],[$this->router->route->getCallback()]))) { |
|
183 | + if ($this->validMethod()) |
|
184 | + foreach ($this->resolver as $resolver) |
|
185 | + if (is_array($target = call_user_func_array([$this, $resolver], [$this->router->route->getCallback()]))) { |
|
186 | 186 | $this->setTarget($target); |
187 | 187 | $this->router->response->setStatusCode(202); |
188 | 188 | return true; |
@@ -194,7 +194,7 @@ discard block |
||
194 | 194 | /** |
195 | 195 | * @param array $target |
196 | 196 | */ |
197 | - public function setTarget($target = []){ |
|
197 | + public function setTarget($target = []) { |
|
198 | 198 | $index = isset($this->request['collection_index']) ? $this->request['collection_index'] : 0; |
199 | 199 | $this->router->route->setDetail($this->request); |
200 | 200 | $this->router->route->setTarget($target); |
@@ -205,9 +205,9 @@ discard block |
||
205 | 205 | /** |
206 | 206 | * |
207 | 207 | */ |
208 | - private function setCallback(){ |
|
208 | + private function setCallback() { |
|
209 | 209 | if (isset($this->request['params'])) { |
210 | - if(is_callable($this->request['params'])) |
|
210 | + if (is_callable($this->request['params'])) |
|
211 | 211 | $this->router->route->setCallback($this->request['params']); |
212 | 212 | else { |
213 | 213 | (is_array($this->request['params']) && isset($this->request['params']['use'])) |
@@ -224,7 +224,7 @@ discard block |
||
224 | 224 | */ |
225 | 225 | public function validMethod() |
226 | 226 | { |
227 | - if(is_callable($this->request['params']))return true; |
|
227 | + if (is_callable($this->request['params']))return true; |
|
228 | 228 | if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') |
229 | 229 | return (isset($this->request['params']['ajax']) && $this->request['params']['ajax'] === true) ? true : false; |
230 | 230 | $method = (isset($this->request['params']['method'])) ? $this->request['params']['method'] : ['GET']; |
@@ -236,10 +236,10 @@ discard block |
||
236 | 236 | * @return array|bool |
237 | 237 | * @throws \Exception |
238 | 238 | */ |
239 | - public function isClosureAndTemplate($callback){ |
|
240 | - if(is_array($cls = $this->isClosure($callback))) { |
|
239 | + public function isClosureAndTemplate($callback) { |
|
240 | + if (is_array($cls = $this->isClosure($callback))) { |
|
241 | 241 | if (is_array($this->request['params']) && isset($this->request['params']['template']) && is_array($tpl = $this->isTemplate($this->request['params']['template']))) { |
242 | - return array_merge(array_merge($cls, $tpl),[ |
|
242 | + return array_merge(array_merge($cls, $tpl), [ |
|
243 | 243 | 'dispatcher' => $this->dispatcher['isClosureAndTemplate'] |
244 | 244 | ]); |
245 | 245 | } |
@@ -253,10 +253,10 @@ discard block |
||
253 | 253 | * @return array|bool |
254 | 254 | * @throws \Exception |
255 | 255 | */ |
256 | - public function isControllerAndTemplate($callback){ |
|
257 | - if(is_array($ctrl = $this->isController($callback))) { |
|
256 | + public function isControllerAndTemplate($callback) { |
|
257 | + if (is_array($ctrl = $this->isController($callback))) { |
|
258 | 258 | if (is_array($this->request['params']) && isset($this->request['params']['template']) && is_array($tpl = $this->isTemplate($this->request['params']['template']))) { |
259 | - return array_merge(array_merge($ctrl, $tpl),[ |
|
259 | + return array_merge(array_merge($ctrl, $tpl), [ |
|
260 | 260 | 'dispatcher' => $this->dispatcher['isControllerAndTemplate'] |
261 | 261 | ]); |
262 | 262 | } |
@@ -296,7 +296,7 @@ discard block |
||
296 | 296 | ? $routes[0] |
297 | 297 | : $this->router->collection->getRoutes()['ctrl_namespace_'.$index].$routes[0]; |
298 | 298 | if (!class_exists($class)) |
299 | - throw new \Exception('Class "' . $class . '." is not found'); |
|
299 | + throw new \Exception('Class "'.$class.'." is not found'); |
|
300 | 300 | if (method_exists($class, $routes[1])) { |
301 | 301 | return [ |
302 | 302 | 'dispatcher' => $this->dispatcher['isController'], |
@@ -305,7 +305,7 @@ discard block |
||
305 | 305 | 'action' => $routes[1] |
306 | 306 | ]; |
307 | 307 | } |
308 | - throw new \Exception('The required method "' . $routes[1] . '" is not found in "' . $class . '"'); |
|
308 | + throw new \Exception('The required method "'.$routes[1].'" is not found in "'.$class.'"'); |
|
309 | 309 | } |
310 | 310 | return false; |
311 | 311 | } |
@@ -317,25 +317,25 @@ discard block |
||
317 | 317 | */ |
318 | 318 | public function isTemplate($callback) |
319 | 319 | { |
320 | - if(is_string($callback)) { |
|
320 | + if (is_string($callback)) { |
|
321 | 321 | $path = trim($callback, '/'); |
322 | 322 | $extension = substr(strrchr($path, "."), 1); |
323 | 323 | $index = isset($this->request['collection_index']) ? $this->request['collection_index'] : 0; |
324 | - $viewDir = $this->router->collection->getRoutes('view_dir_' . $index); |
|
324 | + $viewDir = $this->router->collection->getRoutes('view_dir_'.$index); |
|
325 | 325 | $target = null; |
326 | - if (in_array('.' . $extension, $this->router->getConfig()['templateExtension']) && (is_file($fullPath = $viewDir . $path) || is_file($fullPath = $path))) |
|
326 | + if (in_array('.'.$extension, $this->router->getConfig()['templateExtension']) && (is_file($fullPath = $viewDir.$path) || is_file($fullPath = $path))) |
|
327 | 327 | $target = $fullPath; |
328 | 328 | else { |
329 | 329 | foreach ($this->router->getConfig()['templateExtension'] as $ext) { |
330 | - if (is_file($fullPath = $viewDir . $path . $ext) || is_file($fullPath = $path . $ext)) { |
|
330 | + if (is_file($fullPath = $viewDir.$path.$ext) || is_file($fullPath = $path.$ext)) { |
|
331 | 331 | $target = $fullPath; |
332 | 332 | $extension = substr(strrchr($ext, "."), 1); |
333 | 333 | break; |
334 | 334 | } |
335 | 335 | } |
336 | 336 | } |
337 | - if(is_null($target)) |
|
338 | - throw new \Exception('Template file "' . $path . '" is not found in "' . $viewDir . '"'); |
|
337 | + if (is_null($target)) |
|
338 | + throw new \Exception('Template file "'.$path.'" is not found in "'.$viewDir.'"'); |
|
339 | 339 | return [ |
340 | 340 | 'dispatcher' => $this->dispatcher['isTemplate'], |
341 | 341 | 'template' => $target, |
@@ -117,10 +117,12 @@ discard block |
||
117 | 117 | private function checkSubdomain($route){ |
118 | 118 | $url = (isset($_SERVER['REQUEST_SCHEME']) ? $_SERVER['REQUEST_SCHEME'] : 'http') . '://' . ($host = (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME'])); |
119 | 119 | $domain = $this->router->collection->getDomain($url); |
120 | - if(!empty($this->request['subdomain']) && $route[0] == '/') $route = trim($this->request['subdomain'],'.').'.'.$domain.$route; |
|
120 | + if(!empty($this->request['subdomain']) && $route[0] == '/') { |
|
121 | + $route = trim($this->request['subdomain'],'.').'.'.$domain.$route; |
|
122 | + } |
|
121 | 123 | if($route[0] == '/'){ |
122 | 124 | return ($host != $domain) ? false : true; |
123 | - }elseif($route[0] != '/' && $host != $domain){ |
|
125 | + } elseif($route[0] != '/' && $host != $domain){ |
|
124 | 126 | $route = substr($route, 0, strpos($route, "/")); |
125 | 127 | $route = str_replace('{host}', $domain, $route); |
126 | 128 | $route = preg_replace_callback('#{subdomain}#', [$this, 'subdomainMatch'], $route); |
@@ -165,8 +167,9 @@ discard block |
||
165 | 167 | { |
166 | 168 | if (substr($this->request['route'], -1) == '*') { |
167 | 169 | $pos = strpos($this->request['route'], '*'); |
168 | - if (substr($this->router->route->getUrl(), 0, $pos) == substr($this->request['route'], 0, $pos) && isset($this->request['params'])) |
|
169 | - return true; |
|
170 | + if (substr($this->router->route->getUrl(), 0, $pos) == substr($this->request['route'], 0, $pos) && isset($this->request['params'])) { |
|
171 | + return true; |
|
172 | + } |
|
170 | 173 | } |
171 | 174 | if (preg_match($regex, $this->router->route->getUrl(), $this->request['parameters'])) { |
172 | 175 | array_shift($this->request['parameters']); |
@@ -180,10 +183,11 @@ discard block |
||
180 | 183 | */ |
181 | 184 | private function generateTarget() |
182 | 185 | { |
183 | - if($this->validMethod()) |
|
184 | - foreach($this->resolver as $resolver) |
|
186 | + if($this->validMethod()) { |
|
187 | + foreach($this->resolver as $resolver) |
|
185 | 188 | if (is_array($target = call_user_func_array([$this,$resolver],[$this->router->route->getCallback()]))) { |
186 | - $this->setTarget($target); |
|
189 | + $this->setTarget($target); |
|
190 | + } |
|
187 | 191 | $this->router->response->setStatusCode(202); |
188 | 192 | return true; |
189 | 193 | } |
@@ -207,14 +211,18 @@ discard block |
||
207 | 211 | */ |
208 | 212 | private function setCallback(){ |
209 | 213 | if (isset($this->request['params'])) { |
210 | - if(is_callable($this->request['params'])) |
|
211 | - $this->router->route->setCallback($this->request['params']); |
|
212 | - else { |
|
214 | + if(is_callable($this->request['params'])) { |
|
215 | + $this->router->route->setCallback($this->request['params']); |
|
216 | + } else { |
|
213 | 217 | (is_array($this->request['params']) && isset($this->request['params']['use'])) |
214 | 218 | ? $this->router->route->setCallback($this->request['params']['use']) |
215 | 219 | : $this->router->route->setCallback($this->request['params']); |
216 | - if (isset($this->request['params']['name'])) $this->router->route->setName($this->request['params']['name']); |
|
217 | - if (isset($this->request['params']['method'])) $this->request['params']['method'] = is_array($this->request['params']['method']) ? $this->request['params']['method'] : [$this->request['params']['method']]; |
|
220 | + if (isset($this->request['params']['name'])) { |
|
221 | + $this->router->route->setName($this->request['params']['name']); |
|
222 | + } |
|
223 | + if (isset($this->request['params']['method'])) { |
|
224 | + $this->request['params']['method'] = is_array($this->request['params']['method']) ? $this->request['params']['method'] : [$this->request['params']['method']]; |
|
225 | + } |
|
218 | 226 | } |
219 | 227 | } |
220 | 228 | } |
@@ -224,9 +232,12 @@ discard block |
||
224 | 232 | */ |
225 | 233 | public function validMethod() |
226 | 234 | { |
227 | - if(is_callable($this->request['params']))return true; |
|
228 | - if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') |
|
229 | - return (isset($this->request['params']['ajax']) && $this->request['params']['ajax'] === true) ? true : false; |
|
235 | + if(is_callable($this->request['params'])) { |
|
236 | + return true; |
|
237 | + } |
|
238 | + if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') { |
|
239 | + return (isset($this->request['params']['ajax']) && $this->request['params']['ajax'] === true) ? true : false; |
|
240 | + } |
|
230 | 241 | $method = (isset($this->request['params']['method'])) ? $this->request['params']['method'] : ['GET']; |
231 | 242 | return (in_array($this->router->route->getMethod(), $method)) ? true : false; |
232 | 243 | } |
@@ -290,13 +301,16 @@ discard block |
||
290 | 301 | { |
291 | 302 | if (is_string($callback) && strpos($callback, '@') !== false) { |
292 | 303 | $routes = explode('@', $callback); |
293 | - if (!isset($routes[1])) $routes[1] = 'index'; |
|
304 | + if (!isset($routes[1])) { |
|
305 | + $routes[1] = 'index'; |
|
306 | + } |
|
294 | 307 | $index = isset($this->request['collection_index']) ? $this->request['collection_index'] : 0; |
295 | 308 | $class = (class_exists($routes[0])) |
296 | 309 | ? $routes[0] |
297 | 310 | : $this->router->collection->getRoutes()['ctrl_namespace_'.$index].$routes[0]; |
298 | - if (!class_exists($class)) |
|
299 | - throw new \Exception('Class "' . $class . '." is not found'); |
|
311 | + if (!class_exists($class)) { |
|
312 | + throw new \Exception('Class "' . $class . '." is not found'); |
|
313 | + } |
|
300 | 314 | if (method_exists($class, $routes[1])) { |
301 | 315 | return [ |
302 | 316 | 'dispatcher' => $this->dispatcher['isController'], |
@@ -323,9 +337,9 @@ discard block |
||
323 | 337 | $index = isset($this->request['collection_index']) ? $this->request['collection_index'] : 0; |
324 | 338 | $viewDir = $this->router->collection->getRoutes('view_dir_' . $index); |
325 | 339 | $target = null; |
326 | - if (in_array('.' . $extension, $this->router->getConfig()['templateExtension']) && (is_file($fullPath = $viewDir . $path) || is_file($fullPath = $path))) |
|
327 | - $target = $fullPath; |
|
328 | - else { |
|
340 | + if (in_array('.' . $extension, $this->router->getConfig()['templateExtension']) && (is_file($fullPath = $viewDir . $path) || is_file($fullPath = $path))) { |
|
341 | + $target = $fullPath; |
|
342 | + } else { |
|
329 | 343 | foreach ($this->router->getConfig()['templateExtension'] as $ext) { |
330 | 344 | if (is_file($fullPath = $viewDir . $path . $ext) || is_file($fullPath = $path . $ext)) { |
331 | 345 | $target = $fullPath; |
@@ -334,8 +348,9 @@ discard block |
||
334 | 348 | } |
335 | 349 | } |
336 | 350 | } |
337 | - if(is_null($target)) |
|
338 | - throw new \Exception('Template file "' . $path . '" is not found in "' . $viewDir . '"'); |
|
351 | + if(is_null($target)) { |
|
352 | + throw new \Exception('Template file "' . $path . '" is not found in "' . $viewDir . '"'); |
|
353 | + } |
|
339 | 354 | return [ |
340 | 355 | 'dispatcher' => $this->dispatcher['isTemplate'], |
341 | 356 | 'template' => $target, |
@@ -28,30 +28,30 @@ discard block |
||
28 | 28 | /** |
29 | 29 | * @return array |
30 | 30 | */ |
31 | - public function getLoadedClass(){ |
|
31 | + public function getLoadedClass() { |
|
32 | 32 | return $this->loaded_classes; |
33 | 33 | } |
34 | 34 | |
35 | 35 | /** |
36 | 36 | * @return array |
37 | 37 | */ |
38 | - public function getNamespaces(){ |
|
38 | + public function getNamespaces() { |
|
39 | 39 | return $this->loaded_classes; |
40 | 40 | } |
41 | 41 | |
42 | 42 | /** |
43 | 43 | * @return array |
44 | 44 | */ |
45 | - public function getClassCollection(){ |
|
45 | + public function getClassCollection() { |
|
46 | 46 | return $this->loaded_classes; |
47 | 47 | } |
48 | 48 | |
49 | 49 | /** |
50 | 50 | * @param null $base_path |
51 | 51 | */ |
52 | - public function register($base_path = null){ |
|
53 | - if(!is_null($base_path))$this->base_path = rtrim($base_path,'/'); |
|
54 | - spl_autoload_register(array($this,'loadClass')); |
|
52 | + public function register($base_path = null) { |
|
53 | + if (!is_null($base_path))$this->base_path = rtrim($base_path, '/'); |
|
54 | + spl_autoload_register(array($this, 'loadClass')); |
|
55 | 55 | } |
56 | 56 | |
57 | 57 | /** |
@@ -65,13 +65,13 @@ discard block |
||
65 | 65 | /** |
66 | 66 | * @param $class |
67 | 67 | */ |
68 | - private function loadClass($class){ |
|
69 | - if(!isset($this->loaded_classes[$class])) { |
|
68 | + private function loadClass($class) { |
|
69 | + if (!isset($this->loaded_classes[$class])) { |
|
70 | 70 | if (isset($this->class_collection[$class]) && $this->loadFile($this->class_collection[$class], $class)) return; |
71 | - elseif($this->findClass($class))return; |
|
72 | - else{ |
|
71 | + elseif ($this->findClass($class))return; |
|
72 | + else { |
|
73 | 73 | $file = $this->base_path.'/'.str_replace('\\', '/', $class).'.php'; |
74 | - $this->loadFile($file,$class); |
|
74 | + $this->loadFile($file, $class); |
|
75 | 75 | return; |
76 | 76 | } |
77 | 77 | } |
@@ -82,8 +82,8 @@ discard block |
||
82 | 82 | * @param $class |
83 | 83 | * @return bool |
84 | 84 | */ |
85 | - private function loadFile($file,$class){ |
|
86 | - if(is_file($file)){ |
|
85 | + private function loadFile($file, $class) { |
|
86 | + if (is_file($file)) { |
|
87 | 87 | require $file; |
88 | 88 | $this->loaded_classes[$class] = $file; |
89 | 89 | return true; |
@@ -96,15 +96,15 @@ discard block |
||
96 | 96 | * @param $class |
97 | 97 | * @return bool |
98 | 98 | */ |
99 | - private function findClass($class){ |
|
99 | + private function findClass($class) { |
|
100 | 100 | $prefix = $class; |
101 | 101 | while (false !== $pos = strrpos($prefix, '\\')) { |
102 | 102 | $prefix = substr($class, 0, $pos + 1); |
103 | 103 | $relative_class = substr($class, $pos + 1); |
104 | - if(isset($this->namespaces[$prefix])){ |
|
105 | - foreach($this->namespaces[$prefix] as $dir) { |
|
106 | - $file = $dir . str_replace('\\', '/', $relative_class) . '.php'; |
|
107 | - if ($this->loadFile($file,$class)) return true; |
|
104 | + if (isset($this->namespaces[$prefix])) { |
|
105 | + foreach ($this->namespaces[$prefix] as $dir) { |
|
106 | + $file = $dir.str_replace('\\', '/', $relative_class).'.php'; |
|
107 | + if ($this->loadFile($file, $class)) return true; |
|
108 | 108 | } |
109 | 109 | } |
110 | 110 | $prefix = rtrim($prefix, '\\'); |
@@ -120,13 +120,13 @@ discard block |
||
120 | 120 | */ |
121 | 121 | public function addNamespace($prefix, $base_dirs, $prepend = false) |
122 | 122 | { |
123 | - if(is_array($base_dirs)) |
|
124 | - foreach($base_dirs as $dir) |
|
123 | + if (is_array($base_dirs)) |
|
124 | + foreach ($base_dirs as $dir) |
|
125 | 125 | $this->addNamespace($prefix, $dir, $prepend); |
126 | - else{ |
|
126 | + else { |
|
127 | 127 | $prefix = trim($prefix, '\\').'\\'; |
128 | 128 | $base_dir = rtrim($base_dirs, '/').'/'; |
129 | - if(!isset($this->namespaces[$prefix])) |
|
129 | + if (!isset($this->namespaces[$prefix])) |
|
130 | 130 | $this->namespaces[$prefix] = []; |
131 | 131 | ($prepend) |
132 | 132 | ? array_unshift($this->namespaces[$prefix], $base_dir) |
@@ -137,27 +137,27 @@ discard block |
||
137 | 137 | /** |
138 | 138 | * @param array $prefixes |
139 | 139 | */ |
140 | - public function setNamespaces($prefixes){ |
|
141 | - if(is_string($prefixes))$prefixes = include $prefixes; |
|
142 | - if(is_array($prefixes)) |
|
143 | - foreach($prefixes as $prefix => $base_dir) |
|
144 | - $this->addNamespace($prefix,$base_dir); |
|
140 | + public function setNamespaces($prefixes) { |
|
141 | + if (is_string($prefixes))$prefixes = include $prefixes; |
|
142 | + if (is_array($prefixes)) |
|
143 | + foreach ($prefixes as $prefix => $base_dir) |
|
144 | + $this->addNamespace($prefix, $base_dir); |
|
145 | 145 | } |
146 | 146 | |
147 | 147 | /** |
148 | 148 | * @param $class |
149 | 149 | * @param $path |
150 | 150 | */ |
151 | - public function addClass($class,$path){ |
|
151 | + public function addClass($class, $path) { |
|
152 | 152 | $this->class_collection[$class] = $path; |
153 | 153 | } |
154 | 154 | |
155 | 155 | /** |
156 | 156 | * @param array $collection |
157 | 157 | */ |
158 | - public function setClassCollection($collection){ |
|
159 | - if(is_string($collection))$collection = include $collection; |
|
160 | - if(is_array($collection)) |
|
161 | - $this->class_collection = array_merge($this->class_collection,$collection); |
|
158 | + public function setClassCollection($collection) { |
|
159 | + if (is_string($collection))$collection = include $collection; |
|
160 | + if (is_array($collection)) |
|
161 | + $this->class_collection = array_merge($this->class_collection, $collection); |
|
162 | 162 | } |
163 | 163 | } |
@@ -50,7 +50,9 @@ discard block |
||
50 | 50 | * @param null $base_path |
51 | 51 | */ |
52 | 52 | public function register($base_path = null){ |
53 | - if(!is_null($base_path))$this->base_path = rtrim($base_path,'/'); |
|
53 | + if(!is_null($base_path)) { |
|
54 | + $this->base_path = rtrim($base_path,'/'); |
|
55 | + } |
|
54 | 56 | spl_autoload_register(array($this,'loadClass')); |
55 | 57 | } |
56 | 58 | |
@@ -67,9 +69,11 @@ discard block |
||
67 | 69 | */ |
68 | 70 | private function loadClass($class){ |
69 | 71 | if(!isset($this->loaded_classes[$class])) { |
70 | - if (isset($this->class_collection[$class]) && $this->loadFile($this->class_collection[$class], $class)) return; |
|
71 | - elseif($this->findClass($class))return; |
|
72 | - else{ |
|
72 | + if (isset($this->class_collection[$class]) && $this->loadFile($this->class_collection[$class], $class)) { |
|
73 | + return; |
|
74 | + } elseif($this->findClass($class)) { |
|
75 | + return; |
|
76 | + } else{ |
|
73 | 77 | $file = $this->base_path.'/'.str_replace('\\', '/', $class).'.php'; |
74 | 78 | $this->loadFile($file,$class); |
75 | 79 | return; |
@@ -104,7 +108,9 @@ discard block |
||
104 | 108 | if(isset($this->namespaces[$prefix])){ |
105 | 109 | foreach($this->namespaces[$prefix] as $dir) { |
106 | 110 | $file = $dir . str_replace('\\', '/', $relative_class) . '.php'; |
107 | - if ($this->loadFile($file,$class)) return true; |
|
111 | + if ($this->loadFile($file,$class)) { |
|
112 | + return true; |
|
113 | + } |
|
108 | 114 | } |
109 | 115 | } |
110 | 116 | $prefix = rtrim($prefix, '\\'); |
@@ -120,14 +126,15 @@ discard block |
||
120 | 126 | */ |
121 | 127 | public function addNamespace($prefix, $base_dirs, $prepend = false) |
122 | 128 | { |
123 | - if(is_array($base_dirs)) |
|
124 | - foreach($base_dirs as $dir) |
|
125 | - $this->addNamespace($prefix, $dir, $prepend); |
|
126 | - else{ |
|
129 | + if(is_array($base_dirs)) { |
|
130 | + foreach($base_dirs as $dir) |
|
131 | + $this->addNamespace($prefix, $dir, $prepend); |
|
132 | + } else{ |
|
127 | 133 | $prefix = trim($prefix, '\\').'\\'; |
128 | 134 | $base_dir = rtrim($base_dirs, '/').'/'; |
129 | - if(!isset($this->namespaces[$prefix])) |
|
130 | - $this->namespaces[$prefix] = []; |
|
135 | + if(!isset($this->namespaces[$prefix])) { |
|
136 | + $this->namespaces[$prefix] = []; |
|
137 | + } |
|
131 | 138 | ($prepend) |
132 | 139 | ? array_unshift($this->namespaces[$prefix], $base_dir) |
133 | 140 | : array_push($this->namespaces[$prefix], $base_dir); |
@@ -138,10 +145,13 @@ discard block |
||
138 | 145 | * @param array $prefixes |
139 | 146 | */ |
140 | 147 | public function setNamespaces($prefixes){ |
141 | - if(is_string($prefixes))$prefixes = include $prefixes; |
|
142 | - if(is_array($prefixes)) |
|
143 | - foreach($prefixes as $prefix => $base_dir) |
|
144 | - $this->addNamespace($prefix,$base_dir); |
|
148 | + if(is_string($prefixes)) { |
|
149 | + $prefixes = include $prefixes; |
|
150 | + } |
|
151 | + if(is_array($prefixes)) { |
|
152 | + foreach($prefixes as $prefix => $base_dir) |
|
153 | + $this->addNamespace($prefix,$base_dir); |
|
154 | + } |
|
145 | 155 | } |
146 | 156 | |
147 | 157 | /** |
@@ -156,8 +166,11 @@ discard block |
||
156 | 166 | * @param array $collection |
157 | 167 | */ |
158 | 168 | public function setClassCollection($collection){ |
159 | - if(is_string($collection))$collection = include $collection; |
|
160 | - if(is_array($collection)) |
|
161 | - $this->class_collection = array_merge($this->class_collection,$collection); |
|
169 | + if(is_string($collection)) { |
|
170 | + $collection = include $collection; |
|
171 | + } |
|
172 | + if(is_array($collection)) { |
|
173 | + $this->class_collection = array_merge($this->class_collection,$collection); |
|
174 | + } |
|
162 | 175 | } |
163 | 176 | } |
@@ -4,28 +4,28 @@ |
||
4 | 4 | |
5 | 5 | require __DIR__.'/Autoload.php'; |
6 | 6 | |
7 | -$autoload = new Autoload(); |
|
8 | -$autoload->addNamespace('JetFire\Routing',[ |
|
7 | +$autoload = new Autoload(); |
|
8 | +$autoload->addNamespace('JetFire\Routing', [ |
|
9 | 9 | __DIR__.'/src/Routing', |
10 | 10 | __DIR__.'/', |
11 | 11 | ]); |
12 | -$autoload->addClass('Normal1Controller',__DIR__.'/Block1/Normal1Controller.php'); |
|
12 | +$autoload->addClass('Normal1Controller', __DIR__.'/Block1/Normal1Controller.php'); |
|
13 | 13 | $autoload->register(); |
14 | 14 | |
15 | 15 | |
16 | 16 | // Create RouteCollection instance |
17 | 17 | $collection = new RouteCollection(); |
18 | -$collection->addRoutes(__DIR__.'/Block1/routes.php',['view_dir'=>__DIR__.'/Block1/Views','prefix'=>'block']); |
|
18 | +$collection->addRoutes(__DIR__.'/Block1/routes.php', ['view_dir'=>__DIR__.'/Block1/Views', 'prefix'=>'block']); |
|
19 | 19 | $router = new \JetFire\Routing\Router($collection); |
20 | 20 | $matcher = new \JetFire\Routing\Matcher\ArrayMatcher($router); |
21 | 21 | |
22 | 22 | $router->addMatcher($matcher); |
23 | 23 | $router->setResponses([ |
24 | 24 | // you can use a closure to handle error |
25 | - '404' => function(){ |
|
25 | + '404' => function() { |
|
26 | 26 | return '404'; |
27 | 27 | }, |
28 | - '405' => function(){ |
|
28 | + '405' => function() { |
|
29 | 29 | return '405'; |
30 | 30 | } |
31 | 31 | ]); |
@@ -2,17 +2,17 @@ discard block |
||
2 | 2 | |
3 | 3 | return [ |
4 | 4 | |
5 | - '/' => 'index', |
|
5 | + '/' => 'index', |
|
6 | 6 | |
7 | 7 | '/user1-:id' => [ |
8 | 8 | 'use' => 'user.html', |
9 | 9 | 'arguments' => ['id' => '[0-9]+'] |
10 | 10 | ], |
11 | 11 | |
12 | - '/home1' => [ |
|
13 | - 'use' => 'JetFire\Routing\Block1\Namespace1Controller@index', |
|
12 | + '/home1' => [ |
|
13 | + 'use' => 'JetFire\Routing\Block1\Namespace1Controller@index', |
|
14 | 14 | 'template' => 'index.php' |
15 | - ], |
|
15 | + ], |
|
16 | 16 | |
17 | 17 | '/home-:id' => [ |
18 | 18 | 'use' => 'JetFire\Routing\Block1\Namespace1Controller@index2', |
@@ -24,10 +24,10 @@ discard block |
||
24 | 24 | 'name' => 'contact' |
25 | 25 | ], |
26 | 26 | |
27 | - '/search1-:id-:name' => [ |
|
28 | - 'use' => function($id,$name){ |
|
27 | + '/search1-:id-:name' => [ |
|
28 | + 'use' => function($id,$name){ |
|
29 | 29 | return 'Search'.$id.$name; |
30 | 30 | }, |
31 | 31 | 'arguments' => ['id' => '[0-9]+','name' => '[a-z]*'], |
32 | - ], |
|
32 | + ], |
|
33 | 33 | ]; |
34 | 34 | \ No newline at end of file |
@@ -25,9 +25,9 @@ |
||
25 | 25 | ], |
26 | 26 | |
27 | 27 | '/search1-:id-:name' => [ |
28 | - 'use' => function($id,$name){ |
|
28 | + 'use' => function($id, $name) { |
|
29 | 29 | return 'Search'.$id.$name; |
30 | 30 | }, |
31 | - 'arguments' => ['id' => '[0-9]+','name' => '[a-z]*'], |
|
31 | + 'arguments' => ['id' => '[0-9]+', 'name' => '[a-z]*'], |
|
32 | 32 | ], |
33 | 33 | ]; |
34 | 34 | \ No newline at end of file |
@@ -2,11 +2,11 @@ |
||
2 | 2 | |
3 | 3 | class Normal1Controller { |
4 | 4 | |
5 | - public function contact(){ |
|
5 | + public function contact() { |
|
6 | 6 | echo 'Contact1'; |
7 | 7 | } |
8 | 8 | |
9 | - public function user($name = null){ |
|
9 | + public function user($name = null) { |
|
10 | 10 | echo 'User : '.$name; |
11 | 11 | } |
12 | 12 |
@@ -3,16 +3,16 @@ |
||
3 | 3 | |
4 | 4 | class Namespace1Controller { |
5 | 5 | |
6 | - public function index(){ |
|
6 | + public function index() { |
|
7 | 7 | return ['message' => 'index']; |
8 | 8 | } |
9 | 9 | |
10 | - public function index2($id){ |
|
11 | - if($id == 2){ |
|
10 | + public function index2($id) { |
|
11 | + if ($id == 2) { |
|
12 | 12 | header('Location: http://localhost/JetFire/Routing/block/search1-1-tse'); |
13 | 13 | return true; |
14 | - }else { |
|
15 | - return 'Index-' . $id; |
|
14 | + } else { |
|
15 | + return 'Index-'.$id; |
|
16 | 16 | } |
17 | 17 | } |
18 | 18 |
@@ -11,7 +11,7 @@ |
||
11 | 11 | if($id == 2){ |
12 | 12 | header('Location: http://localhost/JetFire/Routing/block/search1-1-tse'); |
13 | 13 | return true; |
14 | - }else { |
|
14 | + } else { |
|
15 | 15 | return 'Index-' . $id; |
16 | 16 | } |
17 | 17 | } |