@@ -32,7 +32,7 @@ discard block |
||
32 | 32 | private $dispatcher = [ |
33 | 33 | 'isTemplate' => 'JetFire\Routing\Dispatcher\TemplateDispatcher', |
34 | 34 | 'isController' => 'JetFire\Routing\Dispatcher\ControllerDispatcher', |
35 | - 'isControllerAndTemplate' => ['JetFire\Routing\Dispatcher\ControllerDispatcher','JetFire\Routing\Dispatcher\TemplateDispatcher'], |
|
35 | + 'isControllerAndTemplate' => ['JetFire\Routing\Dispatcher\ControllerDispatcher', 'JetFire\Routing\Dispatcher\TemplateDispatcher'], |
|
36 | 36 | ]; |
37 | 37 | |
38 | 38 | /** |
@@ -46,14 +46,14 @@ discard block |
||
46 | 46 | /** |
47 | 47 | * @param array $resolver |
48 | 48 | */ |
49 | - public function setResolver($resolver = []){ |
|
49 | + public function setResolver($resolver = []) { |
|
50 | 50 | $this->resolver = $resolver; |
51 | 51 | } |
52 | 52 | |
53 | 53 | /** |
54 | 54 | * @param string $resolver |
55 | 55 | */ |
56 | - public function addResolver($resolver){ |
|
56 | + public function addResolver($resolver) { |
|
57 | 57 | $this->resolver[] = $resolver; |
58 | 58 | } |
59 | 59 | |
@@ -78,7 +78,7 @@ discard block |
||
78 | 78 | * @param $class |
79 | 79 | * @return mixed|void |
80 | 80 | */ |
81 | - public function addDispatcher($method,$class){ |
|
81 | + public function addDispatcher($method, $class) { |
|
82 | 82 | $this->dispatcher[$method] = $class; |
83 | 83 | } |
84 | 84 | |
@@ -87,8 +87,8 @@ discard block |
||
87 | 87 | */ |
88 | 88 | public function match() |
89 | 89 | { |
90 | - foreach($this->resolver as $resolver){ |
|
91 | - if(is_array($target = call_user_func([$this,$resolver]))) { |
|
90 | + foreach ($this->resolver as $resolver) { |
|
91 | + if (is_array($target = call_user_func([$this, $resolver]))) { |
|
92 | 92 | $this->setTarget($target); |
93 | 93 | $this->router->response->setStatusCode(202); |
94 | 94 | return true; |
@@ -100,7 +100,7 @@ discard block |
||
100 | 100 | /** |
101 | 101 | * @param array $target |
102 | 102 | */ |
103 | - public function setTarget($target = []){ |
|
103 | + public function setTarget($target = []) { |
|
104 | 104 | $index = isset($this->request['collection_index']) ? $this->request['collection_index'] : 0; |
105 | 105 | $this->router->route->setDetail($this->request); |
106 | 106 | $this->router->route->setTarget($target); |
@@ -111,10 +111,10 @@ discard block |
||
111 | 111 | /** |
112 | 112 | * @return array|bool |
113 | 113 | */ |
114 | - public function isControllerAndTemplate(){ |
|
115 | - if(is_array($ctrl = $this->isController())) { |
|
114 | + public function isControllerAndTemplate() { |
|
115 | + if (is_array($ctrl = $this->isController())) { |
|
116 | 116 | if (is_array($tpl = $this->isTemplate())) { |
117 | - return array_merge(array_merge($ctrl, $tpl),[ |
|
117 | + return array_merge(array_merge($ctrl, $tpl), [ |
|
118 | 118 | 'dispatcher' => $this->dispatcher['isControllerAndTemplate'] |
119 | 119 | ]); |
120 | 120 | } |
@@ -130,10 +130,10 @@ discard block |
||
130 | 130 | { |
131 | 131 | foreach ($this->router->getConfig()['templateExtension'] as $extension) { |
132 | 132 | for ($i = 0; $i < $this->router->collection->countRoutes; ++$i) { |
133 | - $url = explode('/', str_replace($this->router->collection->getRoutes('prefix_' . $i), '',$this->router->route->getUrl())); |
|
133 | + $url = explode('/', str_replace($this->router->collection->getRoutes('prefix_'.$i), '', $this->router->route->getUrl())); |
|
134 | 134 | $end = array_pop($url); |
135 | 135 | $url = implode('/', array_map('ucwords', $url)).'/'.$end; |
136 | - if (is_file(($template = rtrim($this->router->collection->getRoutes('view_dir_' . $i), '/') . $url . $extension))) { |
|
136 | + if (is_file(($template = rtrim($this->router->collection->getRoutes('view_dir_'.$i), '/').$url.$extension))) { |
|
137 | 137 | $this->request['collection_index'] = $i; |
138 | 138 | return [ |
139 | 139 | 'dispatcher' => $this->dispatcher['isTemplate'], |
@@ -154,13 +154,13 @@ discard block |
||
154 | 154 | { |
155 | 155 | $routes = array_slice(explode('/', $this->router->route->getUrl()), 1); |
156 | 156 | $i = 0; |
157 | - do{ |
|
158 | - $route = ('/' . $routes[0] == $this->router->collection->getRoutes('prefix_' . $i)) ? array_slice($routes, 1) : $routes; |
|
157 | + do { |
|
158 | + $route = ('/'.$routes[0] == $this->router->collection->getRoutes('prefix_'.$i)) ? array_slice($routes, 1) : $routes; |
|
159 | 159 | if (isset($route[0])) { |
160 | - $class = (class_exists($this->router->collection->getRoutes('ctrl_namespace_' . $i). ucfirst($route[0]) . 'Controller')) |
|
161 | - ? $this->router->collection->getRoutes('ctrl_namespace_' . $i). ucfirst($route[0]) . 'Controller' |
|
162 | - : ucfirst($route[0]) . 'Controller'; |
|
163 | - $route[1] = isset($route[1])?$route[1]:'index'; |
|
160 | + $class = (class_exists($this->router->collection->getRoutes('ctrl_namespace_'.$i).ucfirst($route[0]).'Controller')) |
|
161 | + ? $this->router->collection->getRoutes('ctrl_namespace_'.$i).ucfirst($route[0]).'Controller' |
|
162 | + : ucfirst($route[0]).'Controller'; |
|
163 | + $route[1] = isset($route[1]) ? $route[1] : 'index'; |
|
164 | 164 | if (method_exists($class, $route[1])) { |
165 | 165 | $this->request['parameters'] = array_slice($route, 2); |
166 | 166 | $this->request['collection_index'] = $i; |
@@ -173,7 +173,7 @@ discard block |
||
173 | 173 | } |
174 | 174 | } |
175 | 175 | ++$i; |
176 | - }while($i < $this->router->collection->countRoutes); |
|
176 | + }while ($i < $this->router->collection->countRoutes); |
|
177 | 177 | return false; |
178 | 178 | } |
179 | 179 |
@@ -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 | } |
@@ -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 | } |