@@ -27,139 +27,139 @@ |
||
27 | 27 | */ |
28 | 28 | trait RouteMap |
29 | 29 | { |
30 | - /** |
|
31 | - * All of the verbs supported by the router. |
|
32 | - * |
|
33 | - * @var array $verbs |
|
34 | - */ |
|
35 | - public static $verbs = ['GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS']; |
|
30 | + /** |
|
31 | + * All of the verbs supported by the router. |
|
32 | + * |
|
33 | + * @var array $verbs |
|
34 | + */ |
|
35 | + public static $verbs = ['GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS']; |
|
36 | 36 | |
37 | - /** |
|
38 | - * Add a route to the underlying route collection. |
|
39 | - * |
|
40 | - * @param string $method |
|
41 | - * @param string $route |
|
42 | - * @param mixed $action |
|
43 | - * |
|
44 | - * @return \Syscodes\Components\Routing\Route |
|
45 | - */ |
|
46 | - abstract public function addRoute($method, $route, $action); |
|
37 | + /** |
|
38 | + * Add a route to the underlying route collection. |
|
39 | + * |
|
40 | + * @param string $method |
|
41 | + * @param string $route |
|
42 | + * @param mixed $action |
|
43 | + * |
|
44 | + * @return \Syscodes\Components\Routing\Route |
|
45 | + */ |
|
46 | + abstract public function addRoute($method, $route, $action); |
|
47 | 47 | |
48 | - /** |
|
49 | - * Add a route for all posible methods. |
|
50 | - * |
|
51 | - * @param string $route |
|
52 | - * @param \Closure|array|string|null $action |
|
53 | - * |
|
54 | - * @return \Syscodes\Components\Routing\Route |
|
55 | - */ |
|
56 | - public function any($route, $action = null) |
|
57 | - { |
|
58 | - return $this->addRoute(self::$verbs, $route, $action); |
|
59 | - } |
|
48 | + /** |
|
49 | + * Add a route for all posible methods. |
|
50 | + * |
|
51 | + * @param string $route |
|
52 | + * @param \Closure|array|string|null $action |
|
53 | + * |
|
54 | + * @return \Syscodes\Components\Routing\Route |
|
55 | + */ |
|
56 | + public function any($route, $action = null) |
|
57 | + { |
|
58 | + return $this->addRoute(self::$verbs, $route, $action); |
|
59 | + } |
|
60 | 60 | |
61 | - /** |
|
62 | - * Add a route with delete method. |
|
63 | - * |
|
64 | - * @param string $route |
|
65 | - * @param \Closure|array|string|null $action |
|
66 | - * |
|
67 | - * @return \Syscodes\Components\Routing\Route |
|
68 | - */ |
|
69 | - public function delete($route, $action = null) |
|
70 | - { |
|
71 | - return $this->addRoute('DELETE', $route, $action); |
|
72 | - } |
|
61 | + /** |
|
62 | + * Add a route with delete method. |
|
63 | + * |
|
64 | + * @param string $route |
|
65 | + * @param \Closure|array|string|null $action |
|
66 | + * |
|
67 | + * @return \Syscodes\Components\Routing\Route |
|
68 | + */ |
|
69 | + public function delete($route, $action = null) |
|
70 | + { |
|
71 | + return $this->addRoute('DELETE', $route, $action); |
|
72 | + } |
|
73 | 73 | |
74 | - /** |
|
75 | - * Add a route with get method. |
|
76 | - * |
|
77 | - * @param string $route |
|
78 | - * @param \Closure|array|string|null $action |
|
79 | - * |
|
80 | - * @return \Syscodes\Components\Routing\Route |
|
81 | - */ |
|
82 | - public function get($route, $action = null) |
|
83 | - { |
|
84 | - return $this->addRoute(['GET', 'HEAD'], $route, $action); |
|
85 | - } |
|
74 | + /** |
|
75 | + * Add a route with get method. |
|
76 | + * |
|
77 | + * @param string $route |
|
78 | + * @param \Closure|array|string|null $action |
|
79 | + * |
|
80 | + * @return \Syscodes\Components\Routing\Route |
|
81 | + */ |
|
82 | + public function get($route, $action = null) |
|
83 | + { |
|
84 | + return $this->addRoute(['GET', 'HEAD'], $route, $action); |
|
85 | + } |
|
86 | 86 | |
87 | - /** |
|
88 | - * Add a route with head method. |
|
89 | - * |
|
90 | - * @param string $route |
|
91 | - * @param \Closure|array|string|null $action |
|
92 | - * |
|
93 | - * @return \Syscodes\Components\Routing\Route |
|
94 | - */ |
|
95 | - public function head($route, $action = null) |
|
96 | - { |
|
97 | - return $this->addRoute('HEAD', $route, $action); |
|
98 | - } |
|
87 | + /** |
|
88 | + * Add a route with head method. |
|
89 | + * |
|
90 | + * @param string $route |
|
91 | + * @param \Closure|array|string|null $action |
|
92 | + * |
|
93 | + * @return \Syscodes\Components\Routing\Route |
|
94 | + */ |
|
95 | + public function head($route, $action = null) |
|
96 | + { |
|
97 | + return $this->addRoute('HEAD', $route, $action); |
|
98 | + } |
|
99 | 99 | |
100 | - /** |
|
101 | - * Register a new route with the given methods. |
|
102 | - * |
|
103 | - * @param array|string $methods |
|
104 | - * @param string $route |
|
105 | - * @param \Closure|array|string|null $action |
|
106 | - * |
|
107 | - * @return \Syscodes\Components\Routing\Route |
|
108 | - */ |
|
109 | - public function match($methods, $route, $action = null) |
|
110 | - { |
|
111 | - return $this->addRoute(array_map('strtoupper', (array) $methods), $route, $action); |
|
112 | - } |
|
100 | + /** |
|
101 | + * Register a new route with the given methods. |
|
102 | + * |
|
103 | + * @param array|string $methods |
|
104 | + * @param string $route |
|
105 | + * @param \Closure|array|string|null $action |
|
106 | + * |
|
107 | + * @return \Syscodes\Components\Routing\Route |
|
108 | + */ |
|
109 | + public function match($methods, $route, $action = null) |
|
110 | + { |
|
111 | + return $this->addRoute(array_map('strtoupper', (array) $methods), $route, $action); |
|
112 | + } |
|
113 | 113 | |
114 | - /** |
|
115 | - * Add a route with options method |
|
116 | - * |
|
117 | - * @param string $route |
|
118 | - * @param \Closure|array|string|null $action |
|
119 | - * |
|
120 | - * @return \Syscodes\Components\Routing\Route |
|
121 | - */ |
|
122 | - public function options($route, $action = null) |
|
123 | - { |
|
124 | - return $this->addRoute('OPTIONS', $route, $action); |
|
125 | - } |
|
114 | + /** |
|
115 | + * Add a route with options method |
|
116 | + * |
|
117 | + * @param string $route |
|
118 | + * @param \Closure|array|string|null $action |
|
119 | + * |
|
120 | + * @return \Syscodes\Components\Routing\Route |
|
121 | + */ |
|
122 | + public function options($route, $action = null) |
|
123 | + { |
|
124 | + return $this->addRoute('OPTIONS', $route, $action); |
|
125 | + } |
|
126 | 126 | |
127 | - /** |
|
128 | - * Add a route with patch method. |
|
129 | - * |
|
130 | - * @param string $route |
|
131 | - * @param \Closure|array|string|null $action |
|
132 | - * |
|
133 | - * @return \Syscodes\Components\Routing\Route |
|
134 | - */ |
|
135 | - public function patch($route, $action = null) |
|
136 | - { |
|
137 | - return $this->addRoute('PATCH', $route, $action); |
|
138 | - } |
|
127 | + /** |
|
128 | + * Add a route with patch method. |
|
129 | + * |
|
130 | + * @param string $route |
|
131 | + * @param \Closure|array|string|null $action |
|
132 | + * |
|
133 | + * @return \Syscodes\Components\Routing\Route |
|
134 | + */ |
|
135 | + public function patch($route, $action = null) |
|
136 | + { |
|
137 | + return $this->addRoute('PATCH', $route, $action); |
|
138 | + } |
|
139 | 139 | |
140 | - /** |
|
141 | - * Add a route with post method. |
|
142 | - * |
|
143 | - * @param string $route |
|
144 | - * @param \Closure|array|string|null $action |
|
145 | - * |
|
146 | - * @return \Syscodes\Components\Routing\Route |
|
147 | - */ |
|
148 | - public function post($route, $action = null) |
|
149 | - { |
|
150 | - return $this->addRoute('POST', $route, $action); |
|
151 | - } |
|
140 | + /** |
|
141 | + * Add a route with post method. |
|
142 | + * |
|
143 | + * @param string $route |
|
144 | + * @param \Closure|array|string|null $action |
|
145 | + * |
|
146 | + * @return \Syscodes\Components\Routing\Route |
|
147 | + */ |
|
148 | + public function post($route, $action = null) |
|
149 | + { |
|
150 | + return $this->addRoute('POST', $route, $action); |
|
151 | + } |
|
152 | 152 | |
153 | - /** |
|
154 | - * Add a route with put method. |
|
155 | - * |
|
156 | - * @param string $route |
|
157 | - * @param \Closure|array|string|null $action |
|
158 | - * |
|
159 | - * @return \Syscodes\Components\Routing\Route |
|
160 | - */ |
|
161 | - public function put($route, $action = null) |
|
162 | - { |
|
163 | - return $this->addRoute('PUT', $route, $action); |
|
164 | - } |
|
153 | + /** |
|
154 | + * Add a route with put method. |
|
155 | + * |
|
156 | + * @param string $route |
|
157 | + * @param \Closure|array|string|null $action |
|
158 | + * |
|
159 | + * @return \Syscodes\Components\Routing\Route |
|
160 | + */ |
|
161 | + public function put($route, $action = null) |
|
162 | + { |
|
163 | + return $this->addRoute('PUT', $route, $action); |
|
164 | + } |
|
165 | 165 | } |
166 | 166 | \ No newline at end of file |
@@ -854,7 +854,7 @@ |
||
854 | 854 | * |
855 | 855 | * @return array|string |
856 | 856 | */ |
857 | - public function getMethod(): array|string |
|
857 | + public function getMethod(): array | string |
|
858 | 858 | { |
859 | 859 | return $this->method; |
860 | 860 | } |
@@ -43,857 +43,857 @@ |
||
43 | 43 | */ |
44 | 44 | class Route |
45 | 45 | { |
46 | - use Concerns\RouteCondition, |
|
47 | - Concerns\RouteDependencyResolver; |
|
48 | - |
|
49 | - /** |
|
50 | - * The validators used by the routes. |
|
51 | - * |
|
52 | - * @var array $validators |
|
53 | - */ |
|
54 | - public static $validators; |
|
46 | + use Concerns\RouteCondition, |
|
47 | + Concerns\RouteDependencyResolver; |
|
48 | + |
|
49 | + /** |
|
50 | + * The validators used by the routes. |
|
51 | + * |
|
52 | + * @var array $validators |
|
53 | + */ |
|
54 | + public static $validators; |
|
55 | 55 | |
56 | - /** |
|
57 | - * Action that the route will use when called. |
|
58 | - * |
|
59 | - * @var \Closure|string|array $action |
|
60 | - */ |
|
61 | - public $action; |
|
62 | - |
|
63 | - /** |
|
64 | - * The compiled version of the route. |
|
65 | - * |
|
66 | - * @var \Syscodes\Bundles\ApplicationBundle\Routing\RouteCompiler|string $compiled |
|
67 | - */ |
|
68 | - public $compiled; |
|
69 | - |
|
70 | - /** |
|
71 | - * The computed gathered middleware. |
|
72 | - * |
|
73 | - * @var array|null $computedMiddleware |
|
74 | - */ |
|
75 | - public $computedMiddleware; |
|
76 | - |
|
77 | - /** |
|
78 | - * The container instance used by the route. |
|
79 | - * |
|
80 | - * @var \Syscodes\Components\Container\Container $container |
|
81 | - */ |
|
82 | - protected $container; |
|
83 | - |
|
84 | - /** |
|
85 | - * The controller instance. |
|
86 | - * |
|
87 | - * @var string $controller |
|
88 | - */ |
|
89 | - public $controller; |
|
90 | - |
|
91 | - /** |
|
92 | - * The default values for the route. |
|
93 | - * |
|
94 | - * @var array $defaults |
|
95 | - */ |
|
96 | - public $defaults = []; |
|
97 | - |
|
98 | - /** |
|
99 | - * Indicates whether the route is a fallback route. |
|
100 | - * |
|
101 | - * @var bool $fallback |
|
102 | - */ |
|
103 | - protected $fallback = false; |
|
104 | - |
|
105 | - /** |
|
106 | - * Variable of HTTP method. |
|
107 | - * |
|
108 | - * @var array|string $method |
|
109 | - */ |
|
110 | - public $method; |
|
111 | - |
|
112 | - /** |
|
113 | - * The array of matched parameters. |
|
114 | - * |
|
115 | - * @var array $parameters |
|
116 | - */ |
|
117 | - public $parameters = []; |
|
118 | - |
|
119 | - /** |
|
120 | - * The parameter names for the route. |
|
121 | - * |
|
122 | - * @var string|null $parameterNames |
|
123 | - */ |
|
124 | - public $parameterNames; |
|
125 | - |
|
126 | - /** |
|
127 | - * The URI pattern the route responds to. |
|
128 | - * |
|
129 | - * @var string $uri |
|
130 | - */ |
|
131 | - public $uri; |
|
132 | - |
|
133 | - /** |
|
134 | - * Contains the arguments of the current route. |
|
135 | - * |
|
136 | - * @var array $where |
|
137 | - */ |
|
138 | - public $wheres = []; |
|
139 | - |
|
140 | - /** |
|
141 | - * Constructor. Initialize route. |
|
142 | - * |
|
143 | - * @param array|string $method |
|
144 | - * @param string $uri |
|
145 | - * @param \Closure|array $action |
|
146 | - * |
|
147 | - * @return void |
|
148 | - */ |
|
149 | - public function __construct($method, $uri, $action) |
|
150 | - { |
|
151 | - $this->uri = $uri; |
|
152 | - |
|
153 | - // Set the method |
|
154 | - $this->method = $this->parseMethod($method); |
|
155 | - |
|
156 | - // Set the action |
|
157 | - $this->action = Arr::except($this->parseAction($action), ['prefix']); |
|
56 | + /** |
|
57 | + * Action that the route will use when called. |
|
58 | + * |
|
59 | + * @var \Closure|string|array $action |
|
60 | + */ |
|
61 | + public $action; |
|
62 | + |
|
63 | + /** |
|
64 | + * The compiled version of the route. |
|
65 | + * |
|
66 | + * @var \Syscodes\Bundles\ApplicationBundle\Routing\RouteCompiler|string $compiled |
|
67 | + */ |
|
68 | + public $compiled; |
|
69 | + |
|
70 | + /** |
|
71 | + * The computed gathered middleware. |
|
72 | + * |
|
73 | + * @var array|null $computedMiddleware |
|
74 | + */ |
|
75 | + public $computedMiddleware; |
|
76 | + |
|
77 | + /** |
|
78 | + * The container instance used by the route. |
|
79 | + * |
|
80 | + * @var \Syscodes\Components\Container\Container $container |
|
81 | + */ |
|
82 | + protected $container; |
|
83 | + |
|
84 | + /** |
|
85 | + * The controller instance. |
|
86 | + * |
|
87 | + * @var string $controller |
|
88 | + */ |
|
89 | + public $controller; |
|
90 | + |
|
91 | + /** |
|
92 | + * The default values for the route. |
|
93 | + * |
|
94 | + * @var array $defaults |
|
95 | + */ |
|
96 | + public $defaults = []; |
|
97 | + |
|
98 | + /** |
|
99 | + * Indicates whether the route is a fallback route. |
|
100 | + * |
|
101 | + * @var bool $fallback |
|
102 | + */ |
|
103 | + protected $fallback = false; |
|
104 | + |
|
105 | + /** |
|
106 | + * Variable of HTTP method. |
|
107 | + * |
|
108 | + * @var array|string $method |
|
109 | + */ |
|
110 | + public $method; |
|
111 | + |
|
112 | + /** |
|
113 | + * The array of matched parameters. |
|
114 | + * |
|
115 | + * @var array $parameters |
|
116 | + */ |
|
117 | + public $parameters = []; |
|
118 | + |
|
119 | + /** |
|
120 | + * The parameter names for the route. |
|
121 | + * |
|
122 | + * @var string|null $parameterNames |
|
123 | + */ |
|
124 | + public $parameterNames; |
|
125 | + |
|
126 | + /** |
|
127 | + * The URI pattern the route responds to. |
|
128 | + * |
|
129 | + * @var string $uri |
|
130 | + */ |
|
131 | + public $uri; |
|
132 | + |
|
133 | + /** |
|
134 | + * Contains the arguments of the current route. |
|
135 | + * |
|
136 | + * @var array $where |
|
137 | + */ |
|
138 | + public $wheres = []; |
|
139 | + |
|
140 | + /** |
|
141 | + * Constructor. Initialize route. |
|
142 | + * |
|
143 | + * @param array|string $method |
|
144 | + * @param string $uri |
|
145 | + * @param \Closure|array $action |
|
146 | + * |
|
147 | + * @return void |
|
148 | + */ |
|
149 | + public function __construct($method, $uri, $action) |
|
150 | + { |
|
151 | + $this->uri = $uri; |
|
152 | + |
|
153 | + // Set the method |
|
154 | + $this->method = $this->parseMethod($method); |
|
155 | + |
|
156 | + // Set the action |
|
157 | + $this->action = Arr::except($this->parseAction($action), ['prefix']); |
|
158 | 158 | |
159 | - if (in_array('GET', $this->method) && ! in_array('HEAD', $this->method)) { |
|
160 | - $this->method[] = 'HEAD'; |
|
161 | - } |
|
162 | - |
|
163 | - $this->prefix(is_array($action) ? Arr::get($action, 'prefix') : ''); |
|
164 | - } |
|
165 | - |
|
166 | - /** |
|
167 | - * Get the controller instance for the route. |
|
168 | - * |
|
169 | - * @return mixed |
|
170 | - */ |
|
171 | - public function getController() |
|
172 | - { |
|
173 | - if ( ! $this->controller) { |
|
174 | - $class = $this->parseControllerCallback()[0]; |
|
159 | + if (in_array('GET', $this->method) && ! in_array('HEAD', $this->method)) { |
|
160 | + $this->method[] = 'HEAD'; |
|
161 | + } |
|
162 | + |
|
163 | + $this->prefix(is_array($action) ? Arr::get($action, 'prefix') : ''); |
|
164 | + } |
|
165 | + |
|
166 | + /** |
|
167 | + * Get the controller instance for the route. |
|
168 | + * |
|
169 | + * @return mixed |
|
170 | + */ |
|
171 | + public function getController() |
|
172 | + { |
|
173 | + if ( ! $this->controller) { |
|
174 | + $class = $this->parseControllerCallback()[0]; |
|
175 | 175 | |
176 | - $this->controller = $this->container->make(ltrim($class, '\\')); |
|
177 | - } |
|
178 | - |
|
179 | - return $this->controller; |
|
180 | - } |
|
181 | - |
|
182 | - /** |
|
183 | - * Get the controller method used for the route. |
|
184 | - * |
|
185 | - * @return string |
|
186 | - */ |
|
187 | - public function getControllerMethod(): string |
|
188 | - { |
|
189 | - return $this->parseControllerCallback()[1]; |
|
190 | - } |
|
191 | - |
|
192 | - /** |
|
193 | - * Get or set the domain for the route. |
|
194 | - * |
|
195 | - * @param string|null $domain |
|
196 | - * |
|
197 | - * @return mixed |
|
198 | - */ |
|
199 | - public function domain($domain = null) |
|
200 | - { |
|
201 | - if (is_null($domain)) { |
|
202 | - return $this->getDomain(); |
|
203 | - } |
|
176 | + $this->controller = $this->container->make(ltrim($class, '\\')); |
|
177 | + } |
|
178 | + |
|
179 | + return $this->controller; |
|
180 | + } |
|
181 | + |
|
182 | + /** |
|
183 | + * Get the controller method used for the route. |
|
184 | + * |
|
185 | + * @return string |
|
186 | + */ |
|
187 | + public function getControllerMethod(): string |
|
188 | + { |
|
189 | + return $this->parseControllerCallback()[1]; |
|
190 | + } |
|
191 | + |
|
192 | + /** |
|
193 | + * Get or set the domain for the route. |
|
194 | + * |
|
195 | + * @param string|null $domain |
|
196 | + * |
|
197 | + * @return mixed |
|
198 | + */ |
|
199 | + public function domain($domain = null) |
|
200 | + { |
|
201 | + if (is_null($domain)) { |
|
202 | + return $this->getDomain(); |
|
203 | + } |
|
204 | 204 | |
205 | - $parsed = RouteUri::parse($domain); |
|
206 | - |
|
207 | - $this->action['domain'] = $parsed->uri; |
|
208 | - |
|
209 | - return $this; |
|
210 | - } |
|
211 | - |
|
212 | - /** |
|
213 | - * Get the domain defined for the route. |
|
214 | - * |
|
215 | - * @return string|null |
|
216 | - */ |
|
217 | - public function getDomain() |
|
218 | - { |
|
219 | - return isset($this->action['domain']) |
|
220 | - ? str_replace(['http://', 'https://'], '', $this->action['domain']) |
|
221 | - : null; |
|
222 | - } |
|
223 | - |
|
224 | - /** |
|
225 | - * Parse the controller. |
|
226 | - * |
|
227 | - * @return array |
|
228 | - */ |
|
229 | - public function parseControllerCallback(): array |
|
230 | - { |
|
231 | - return Str::parseCallback($this->action['uses']); |
|
232 | - } |
|
205 | + $parsed = RouteUri::parse($domain); |
|
206 | + |
|
207 | + $this->action['domain'] = $parsed->uri; |
|
208 | + |
|
209 | + return $this; |
|
210 | + } |
|
211 | + |
|
212 | + /** |
|
213 | + * Get the domain defined for the route. |
|
214 | + * |
|
215 | + * @return string|null |
|
216 | + */ |
|
217 | + public function getDomain() |
|
218 | + { |
|
219 | + return isset($this->action['domain']) |
|
220 | + ? str_replace(['http://', 'https://'], '', $this->action['domain']) |
|
221 | + : null; |
|
222 | + } |
|
223 | + |
|
224 | + /** |
|
225 | + * Parse the controller. |
|
226 | + * |
|
227 | + * @return array |
|
228 | + */ |
|
229 | + public function parseControllerCallback(): array |
|
230 | + { |
|
231 | + return Str::parseCallback($this->action['uses']); |
|
232 | + } |
|
233 | 233 | |
234 | - /** |
|
235 | - * Checks whether the route's action is a controller. |
|
236 | - * |
|
237 | - * @return bool |
|
238 | - */ |
|
239 | - public function isControllerAction(): bool |
|
240 | - { |
|
241 | - return is_string($this->action['uses']); |
|
242 | - } |
|
243 | - |
|
244 | - /** |
|
245 | - * Get the dispatcher for the route's controller. |
|
246 | - * |
|
247 | - * @return \Syscodes\Components\Routing\ControllerDispatcher |
|
248 | - */ |
|
249 | - private function controllerDispatcher(): ControllerDispatcher |
|
250 | - { |
|
251 | - return new ControllerDispatcher($this->container); |
|
252 | - } |
|
234 | + /** |
|
235 | + * Checks whether the route's action is a controller. |
|
236 | + * |
|
237 | + * @return bool |
|
238 | + */ |
|
239 | + public function isControllerAction(): bool |
|
240 | + { |
|
241 | + return is_string($this->action['uses']); |
|
242 | + } |
|
243 | + |
|
244 | + /** |
|
245 | + * Get the dispatcher for the route's controller. |
|
246 | + * |
|
247 | + * @return \Syscodes\Components\Routing\ControllerDispatcher |
|
248 | + */ |
|
249 | + private function controllerDispatcher(): ControllerDispatcher |
|
250 | + { |
|
251 | + return new ControllerDispatcher($this->container); |
|
252 | + } |
|
253 | 253 | |
254 | - /** |
|
255 | - * Run the route action and return the response. |
|
256 | - * |
|
257 | - * @return mixed |
|
258 | - */ |
|
259 | - public function runResolver() |
|
260 | - { |
|
261 | - $this->container = $this->container ?: new Container; |
|
262 | - |
|
263 | - try { |
|
264 | - if ($this->isControllerAction()) { |
|
265 | - return $this->runResolverController(); |
|
266 | - } |
|
267 | - |
|
268 | - return $this->runResolverCallable(); |
|
269 | - } catch (HttpResponseException $e) { |
|
270 | - return $e->getResponse(); |
|
271 | - } |
|
272 | - } |
|
273 | - |
|
274 | - /** |
|
275 | - * Run the route action and return the response. |
|
276 | - * |
|
277 | - * @return mixed |
|
278 | - */ |
|
279 | - protected function runResolverCallable() |
|
280 | - { |
|
281 | - $callable = $this->action['uses']; |
|
282 | - |
|
283 | - return $callable(...array_values($this->resolveMethodDependencies( |
|
284 | - $this->parametersWithouNulls(), new ReflectionFunction($callable) |
|
285 | - ))); |
|
286 | - } |
|
287 | - |
|
288 | - /** |
|
289 | - * Run the route action and return the response. |
|
290 | - * |
|
291 | - * @return mixed |
|
292 | - */ |
|
293 | - protected function runResolverController() |
|
294 | - { |
|
295 | - return $this->controllerDispatcher()->dispatch($this, $this->getController(), $this->getControllerMethod()); |
|
296 | - } |
|
297 | - |
|
298 | - /** |
|
299 | - * Determine if the route matches a given request. |
|
300 | - * |
|
301 | - * @param \Syscodes\Components\Http\Request $request |
|
302 | - * @param bool $method |
|
303 | - * |
|
304 | - * @return bool |
|
305 | - */ |
|
306 | - public function matches(Request $request, bool $method = true): bool |
|
307 | - { |
|
308 | - $this->compileRoute(); |
|
309 | - |
|
310 | - foreach (self::getValidators() as $validator) { |
|
311 | - if ($method && $validator instanceof MethodValidator) { |
|
312 | - continue; |
|
313 | - } |
|
314 | - |
|
315 | - if ( ! $validator->matches($this, $request)) { |
|
316 | - return false; |
|
317 | - } |
|
318 | - } |
|
319 | - |
|
320 | - return true; |
|
321 | - } |
|
322 | - |
|
323 | - /** |
|
324 | - * Get the route validators for the instance. |
|
325 | - * |
|
326 | - * @return array |
|
327 | - */ |
|
328 | - public function getValidators(): array |
|
329 | - { |
|
330 | - if (isset(static::$validators)) { |
|
331 | - return static::$validators; |
|
332 | - } |
|
333 | - |
|
334 | - return static::$validators = [ |
|
335 | - new HostValidator, new MethodValidator, |
|
336 | - new SchemeValidator, new UriValidator |
|
337 | - ]; |
|
338 | - } |
|
339 | - |
|
340 | - /** |
|
341 | - * Parse the route action. |
|
342 | - * |
|
343 | - * @param \callable|array|null $action |
|
344 | - * |
|
345 | - * @return array |
|
346 | - * |
|
347 | - * @throws \InvalidArgumentException |
|
348 | - */ |
|
349 | - protected function parseAction($action): array |
|
350 | - { |
|
351 | - if ( ! (is_object($action) && ($action instanceof Closure)) && ($action === null || $action === '')) { |
|
352 | - throw new InvalidArgumentException(__('route.actionClosureOrFunction')); |
|
353 | - } |
|
354 | - |
|
355 | - return RouteAction::parse($this->uri, $action); |
|
356 | - } |
|
357 | - |
|
358 | - /** |
|
359 | - * Set the method of the current route. |
|
360 | - * |
|
361 | - * @param array $method |
|
362 | - * |
|
363 | - * @return array |
|
364 | - * |
|
365 | - * @throws \InvalidArgumentException |
|
366 | - */ |
|
367 | - public function parseMethod($method): array |
|
368 | - { |
|
369 | - if ($method === null || empty($method)) { |
|
370 | - throw new InvalidArgumentException(__('route.methodNotProvided')); |
|
371 | - } |
|
372 | - |
|
373 | - foreach ((array) $method as $httpMethod) { |
|
374 | - if ( ! in_array($httpMethod, ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS', 'HEAD', 'ANY'])) { |
|
375 | - throw new InvalidArgumentException(__('route.methodNotAllowed')); |
|
376 | - } |
|
377 | - } |
|
378 | - |
|
379 | - return array_map('strtoupper', (array) $method); |
|
380 | - } |
|
381 | - |
|
382 | - /** |
|
383 | - * Set the URI that the route responds to. |
|
384 | - * |
|
385 | - * @param string|null $uri |
|
386 | - * |
|
387 | - * @return static |
|
388 | - * |
|
389 | - * @throws \InvalidArgumentException |
|
390 | - */ |
|
391 | - public function setUri($uri): static |
|
392 | - { |
|
393 | - if ($uri === null) { |
|
394 | - throw new InvalidArgumentException(__('route.uriNotProvided')); |
|
395 | - } |
|
396 | - |
|
397 | - $this->uri = $this->parseUri($uri); |
|
398 | - |
|
399 | - return $this; |
|
400 | - } |
|
254 | + /** |
|
255 | + * Run the route action and return the response. |
|
256 | + * |
|
257 | + * @return mixed |
|
258 | + */ |
|
259 | + public function runResolver() |
|
260 | + { |
|
261 | + $this->container = $this->container ?: new Container; |
|
262 | + |
|
263 | + try { |
|
264 | + if ($this->isControllerAction()) { |
|
265 | + return $this->runResolverController(); |
|
266 | + } |
|
267 | + |
|
268 | + return $this->runResolverCallable(); |
|
269 | + } catch (HttpResponseException $e) { |
|
270 | + return $e->getResponse(); |
|
271 | + } |
|
272 | + } |
|
273 | + |
|
274 | + /** |
|
275 | + * Run the route action and return the response. |
|
276 | + * |
|
277 | + * @return mixed |
|
278 | + */ |
|
279 | + protected function runResolverCallable() |
|
280 | + { |
|
281 | + $callable = $this->action['uses']; |
|
282 | + |
|
283 | + return $callable(...array_values($this->resolveMethodDependencies( |
|
284 | + $this->parametersWithouNulls(), new ReflectionFunction($callable) |
|
285 | + ))); |
|
286 | + } |
|
287 | + |
|
288 | + /** |
|
289 | + * Run the route action and return the response. |
|
290 | + * |
|
291 | + * @return mixed |
|
292 | + */ |
|
293 | + protected function runResolverController() |
|
294 | + { |
|
295 | + return $this->controllerDispatcher()->dispatch($this, $this->getController(), $this->getControllerMethod()); |
|
296 | + } |
|
297 | + |
|
298 | + /** |
|
299 | + * Determine if the route matches a given request. |
|
300 | + * |
|
301 | + * @param \Syscodes\Components\Http\Request $request |
|
302 | + * @param bool $method |
|
303 | + * |
|
304 | + * @return bool |
|
305 | + */ |
|
306 | + public function matches(Request $request, bool $method = true): bool |
|
307 | + { |
|
308 | + $this->compileRoute(); |
|
309 | + |
|
310 | + foreach (self::getValidators() as $validator) { |
|
311 | + if ($method && $validator instanceof MethodValidator) { |
|
312 | + continue; |
|
313 | + } |
|
314 | + |
|
315 | + if ( ! $validator->matches($this, $request)) { |
|
316 | + return false; |
|
317 | + } |
|
318 | + } |
|
319 | + |
|
320 | + return true; |
|
321 | + } |
|
322 | + |
|
323 | + /** |
|
324 | + * Get the route validators for the instance. |
|
325 | + * |
|
326 | + * @return array |
|
327 | + */ |
|
328 | + public function getValidators(): array |
|
329 | + { |
|
330 | + if (isset(static::$validators)) { |
|
331 | + return static::$validators; |
|
332 | + } |
|
333 | + |
|
334 | + return static::$validators = [ |
|
335 | + new HostValidator, new MethodValidator, |
|
336 | + new SchemeValidator, new UriValidator |
|
337 | + ]; |
|
338 | + } |
|
339 | + |
|
340 | + /** |
|
341 | + * Parse the route action. |
|
342 | + * |
|
343 | + * @param \callable|array|null $action |
|
344 | + * |
|
345 | + * @return array |
|
346 | + * |
|
347 | + * @throws \InvalidArgumentException |
|
348 | + */ |
|
349 | + protected function parseAction($action): array |
|
350 | + { |
|
351 | + if ( ! (is_object($action) && ($action instanceof Closure)) && ($action === null || $action === '')) { |
|
352 | + throw new InvalidArgumentException(__('route.actionClosureOrFunction')); |
|
353 | + } |
|
354 | + |
|
355 | + return RouteAction::parse($this->uri, $action); |
|
356 | + } |
|
357 | + |
|
358 | + /** |
|
359 | + * Set the method of the current route. |
|
360 | + * |
|
361 | + * @param array $method |
|
362 | + * |
|
363 | + * @return array |
|
364 | + * |
|
365 | + * @throws \InvalidArgumentException |
|
366 | + */ |
|
367 | + public function parseMethod($method): array |
|
368 | + { |
|
369 | + if ($method === null || empty($method)) { |
|
370 | + throw new InvalidArgumentException(__('route.methodNotProvided')); |
|
371 | + } |
|
372 | + |
|
373 | + foreach ((array) $method as $httpMethod) { |
|
374 | + if ( ! in_array($httpMethod, ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS', 'HEAD', 'ANY'])) { |
|
375 | + throw new InvalidArgumentException(__('route.methodNotAllowed')); |
|
376 | + } |
|
377 | + } |
|
378 | + |
|
379 | + return array_map('strtoupper', (array) $method); |
|
380 | + } |
|
381 | + |
|
382 | + /** |
|
383 | + * Set the URI that the route responds to. |
|
384 | + * |
|
385 | + * @param string|null $uri |
|
386 | + * |
|
387 | + * @return static |
|
388 | + * |
|
389 | + * @throws \InvalidArgumentException |
|
390 | + */ |
|
391 | + public function setUri($uri): static |
|
392 | + { |
|
393 | + if ($uri === null) { |
|
394 | + throw new InvalidArgumentException(__('route.uriNotProvided')); |
|
395 | + } |
|
396 | + |
|
397 | + $this->uri = $this->parseUri($uri); |
|
398 | + |
|
399 | + return $this; |
|
400 | + } |
|
401 | 401 | |
402 | - /** |
|
403 | - * Parse the route URI and normalize. |
|
404 | - * |
|
405 | - * @param string $uri |
|
406 | - * |
|
407 | - * @return string |
|
408 | - */ |
|
409 | - protected function parseUri($uri): string |
|
410 | - { |
|
411 | - return take(RouteUri::parse($uri), fn ($uri) => $uri)->uri; |
|
412 | - } |
|
413 | - |
|
414 | - /** |
|
415 | - * Add a prefix to the route URI. |
|
416 | - * |
|
417 | - * @param string $prefix |
|
418 | - * |
|
419 | - * @return static |
|
420 | - */ |
|
421 | - public function prefix($prefix): static |
|
422 | - { |
|
423 | - if ( ! empty($newPrefix = trim(rtrim($prefix, '/').'/'.ltrim($this->action['prefix'] ?? '', '/'), '/'))) { |
|
424 | - $this->action['prefix'] = $newPrefix; |
|
425 | - } |
|
402 | + /** |
|
403 | + * Parse the route URI and normalize. |
|
404 | + * |
|
405 | + * @param string $uri |
|
406 | + * |
|
407 | + * @return string |
|
408 | + */ |
|
409 | + protected function parseUri($uri): string |
|
410 | + { |
|
411 | + return take(RouteUri::parse($uri), fn ($uri) => $uri)->uri; |
|
412 | + } |
|
413 | + |
|
414 | + /** |
|
415 | + * Add a prefix to the route URI. |
|
416 | + * |
|
417 | + * @param string $prefix |
|
418 | + * |
|
419 | + * @return static |
|
420 | + */ |
|
421 | + public function prefix($prefix): static |
|
422 | + { |
|
423 | + if ( ! empty($newPrefix = trim(rtrim($prefix, '/').'/'.ltrim($this->action['prefix'] ?? '', '/'), '/'))) { |
|
424 | + $this->action['prefix'] = $newPrefix; |
|
425 | + } |
|
426 | 426 | |
427 | - $uri = rtrim($prefix, '/').'/'.ltrim($this->uri, '/'); |
|
427 | + $uri = rtrim($prefix, '/').'/'.ltrim($this->uri, '/'); |
|
428 | 428 | |
429 | - return $this->setUri($uri !== '/' ? trim($uri, '/') : $uri); |
|
430 | - } |
|
431 | - |
|
432 | - /** |
|
433 | - * Set the action array for the route. |
|
434 | - * |
|
435 | - * @param array $action |
|
436 | - * |
|
437 | - * @return mixed |
|
438 | - */ |
|
439 | - public function setAction(array $action) |
|
440 | - { |
|
441 | - $this->action = $action; |
|
442 | - |
|
443 | - if (isset($this->action['domain'])) { |
|
444 | - $this->domain($this->action['domain']); |
|
445 | - } |
|
429 | + return $this->setUri($uri !== '/' ? trim($uri, '/') : $uri); |
|
430 | + } |
|
431 | + |
|
432 | + /** |
|
433 | + * Set the action array for the route. |
|
434 | + * |
|
435 | + * @param array $action |
|
436 | + * |
|
437 | + * @return mixed |
|
438 | + */ |
|
439 | + public function setAction(array $action) |
|
440 | + { |
|
441 | + $this->action = $action; |
|
442 | + |
|
443 | + if (isset($this->action['domain'])) { |
|
444 | + $this->domain($this->action['domain']); |
|
445 | + } |
|
446 | 446 | |
447 | - return $this; |
|
448 | - } |
|
449 | - |
|
450 | - /** |
|
451 | - * Set the name. |
|
452 | - * |
|
453 | - * @param string $name |
|
454 | - * |
|
455 | - * @return static |
|
456 | - */ |
|
457 | - public function name($name): static |
|
458 | - { |
|
459 | - $this->action['as'] = isset($this->action['as']) ? $this->action['as'].$name : $name; |
|
460 | - |
|
461 | - return $this; |
|
462 | - } |
|
463 | - |
|
464 | - /** |
|
465 | - * Determine whether the route's name matches the given patterns. |
|
466 | - * |
|
467 | - * @param mixed ...$patterns |
|
468 | - * |
|
469 | - * @return bool |
|
470 | - */ |
|
471 | - public function named(...$patterns): bool |
|
472 | - { |
|
473 | - if (is_null($routeName = $this->getName())) { |
|
474 | - return false; |
|
475 | - } |
|
476 | - |
|
477 | - foreach ($patterns as $pattern) { |
|
478 | - if (Str::is($pattern, $routeName)) { |
|
479 | - return true; |
|
480 | - } |
|
481 | - } |
|
482 | - |
|
483 | - return false; |
|
484 | - } |
|
485 | - |
|
486 | - /** |
|
487 | - * Set a default value for the route. |
|
488 | - * |
|
489 | - * @param string $key |
|
490 | - * @param mixed $value |
|
491 | - * |
|
492 | - * @return static |
|
493 | - */ |
|
494 | - public function defaults($key, $value): static |
|
495 | - { |
|
496 | - $this->defaults[$key] = $value; |
|
497 | - |
|
498 | - return $this; |
|
499 | - } |
|
500 | - |
|
501 | - /** |
|
502 | - * Set a default values for the route. |
|
503 | - * |
|
504 | - * @param array $defaults |
|
505 | - * |
|
506 | - * @return static |
|
507 | - */ |
|
508 | - public function setDefaults(array $defaults): static |
|
509 | - { |
|
510 | - $this->defaults = $defaults; |
|
511 | - |
|
512 | - return $this; |
|
513 | - } |
|
514 | - |
|
515 | - /** |
|
516 | - * Set the flag of fallback mode on the route. |
|
517 | - * |
|
518 | - * @return static |
|
519 | - */ |
|
520 | - public function fallback(): static |
|
521 | - { |
|
522 | - $this->fallback = true; |
|
523 | - |
|
524 | - return $this; |
|
525 | - } |
|
526 | - |
|
527 | - /** |
|
528 | - * Set the facllback value. |
|
529 | - * |
|
530 | - * @param bool $fallback |
|
531 | - * |
|
532 | - * @return static |
|
533 | - */ |
|
534 | - public function setFallback(bool $fallback): static |
|
535 | - { |
|
536 | - $this->fallback = $fallback; |
|
537 | - |
|
538 | - return $this; |
|
539 | - } |
|
540 | - |
|
541 | - /** |
|
542 | - * Set the where. |
|
543 | - * |
|
544 | - * @param array|string $name |
|
545 | - * @param string|null $expression |
|
546 | - * |
|
547 | - * @return static |
|
548 | - */ |
|
549 | - public function where($name, string $expression = null): static |
|
550 | - { |
|
551 | - $wheres = is_array($name) ? $name : [$name => $expression]; |
|
447 | + return $this; |
|
448 | + } |
|
449 | + |
|
450 | + /** |
|
451 | + * Set the name. |
|
452 | + * |
|
453 | + * @param string $name |
|
454 | + * |
|
455 | + * @return static |
|
456 | + */ |
|
457 | + public function name($name): static |
|
458 | + { |
|
459 | + $this->action['as'] = isset($this->action['as']) ? $this->action['as'].$name : $name; |
|
460 | + |
|
461 | + return $this; |
|
462 | + } |
|
463 | + |
|
464 | + /** |
|
465 | + * Determine whether the route's name matches the given patterns. |
|
466 | + * |
|
467 | + * @param mixed ...$patterns |
|
468 | + * |
|
469 | + * @return bool |
|
470 | + */ |
|
471 | + public function named(...$patterns): bool |
|
472 | + { |
|
473 | + if (is_null($routeName = $this->getName())) { |
|
474 | + return false; |
|
475 | + } |
|
476 | + |
|
477 | + foreach ($patterns as $pattern) { |
|
478 | + if (Str::is($pattern, $routeName)) { |
|
479 | + return true; |
|
480 | + } |
|
481 | + } |
|
482 | + |
|
483 | + return false; |
|
484 | + } |
|
485 | + |
|
486 | + /** |
|
487 | + * Set a default value for the route. |
|
488 | + * |
|
489 | + * @param string $key |
|
490 | + * @param mixed $value |
|
491 | + * |
|
492 | + * @return static |
|
493 | + */ |
|
494 | + public function defaults($key, $value): static |
|
495 | + { |
|
496 | + $this->defaults[$key] = $value; |
|
497 | + |
|
498 | + return $this; |
|
499 | + } |
|
500 | + |
|
501 | + /** |
|
502 | + * Set a default values for the route. |
|
503 | + * |
|
504 | + * @param array $defaults |
|
505 | + * |
|
506 | + * @return static |
|
507 | + */ |
|
508 | + public function setDefaults(array $defaults): static |
|
509 | + { |
|
510 | + $this->defaults = $defaults; |
|
511 | + |
|
512 | + return $this; |
|
513 | + } |
|
514 | + |
|
515 | + /** |
|
516 | + * Set the flag of fallback mode on the route. |
|
517 | + * |
|
518 | + * @return static |
|
519 | + */ |
|
520 | + public function fallback(): static |
|
521 | + { |
|
522 | + $this->fallback = true; |
|
523 | + |
|
524 | + return $this; |
|
525 | + } |
|
526 | + |
|
527 | + /** |
|
528 | + * Set the facllback value. |
|
529 | + * |
|
530 | + * @param bool $fallback |
|
531 | + * |
|
532 | + * @return static |
|
533 | + */ |
|
534 | + public function setFallback(bool $fallback): static |
|
535 | + { |
|
536 | + $this->fallback = $fallback; |
|
537 | + |
|
538 | + return $this; |
|
539 | + } |
|
540 | + |
|
541 | + /** |
|
542 | + * Set the where. |
|
543 | + * |
|
544 | + * @param array|string $name |
|
545 | + * @param string|null $expression |
|
546 | + * |
|
547 | + * @return static |
|
548 | + */ |
|
549 | + public function where($name, string $expression = null): static |
|
550 | + { |
|
551 | + $wheres = is_array($name) ? $name : [$name => $expression]; |
|
552 | 552 | |
553 | - foreach ($wheres as $name => $expression) { |
|
554 | - $this->wheres[$name] = $expression; |
|
555 | - } |
|
556 | - |
|
557 | - return $this; |
|
558 | - } |
|
559 | - |
|
560 | - /** |
|
561 | - * Set the where when have a variable assign. |
|
562 | - * |
|
563 | - * @param string $key |
|
564 | - * |
|
565 | - * @return string|null |
|
566 | - */ |
|
567 | - public function setPattern(string $key): ?string |
|
568 | - { |
|
569 | - return $this->wheres[$key] ?? null; |
|
570 | - } |
|
571 | - |
|
572 | - /** |
|
573 | - * Bind the route to a given request for execution. |
|
574 | - * |
|
575 | - * @param \Syscodes\Components\Http\Request $request |
|
576 | - * |
|
577 | - * @return static |
|
578 | - */ |
|
579 | - public function bind(Request $request): static |
|
580 | - { |
|
581 | - $this->compileRoute(); |
|
553 | + foreach ($wheres as $name => $expression) { |
|
554 | + $this->wheres[$name] = $expression; |
|
555 | + } |
|
556 | + |
|
557 | + return $this; |
|
558 | + } |
|
559 | + |
|
560 | + /** |
|
561 | + * Set the where when have a variable assign. |
|
562 | + * |
|
563 | + * @param string $key |
|
564 | + * |
|
565 | + * @return string|null |
|
566 | + */ |
|
567 | + public function setPattern(string $key): ?string |
|
568 | + { |
|
569 | + return $this->wheres[$key] ?? null; |
|
570 | + } |
|
571 | + |
|
572 | + /** |
|
573 | + * Bind the route to a given request for execution. |
|
574 | + * |
|
575 | + * @param \Syscodes\Components\Http\Request $request |
|
576 | + * |
|
577 | + * @return static |
|
578 | + */ |
|
579 | + public function bind(Request $request): static |
|
580 | + { |
|
581 | + $this->compileRoute(); |
|
582 | 582 | |
583 | - $this->parameters = (new RouteParameter($this))->parameters($request); |
|
584 | - |
|
585 | - return $this; |
|
586 | - } |
|
587 | - |
|
588 | - /** |
|
589 | - * Compile into a Route Compile instance. |
|
590 | - * |
|
591 | - * @return \Syscodes\Bundles\ApplicationBundle\Routing\RouteCompiler |
|
592 | - */ |
|
593 | - protected function compileRoute() |
|
594 | - { |
|
595 | - if ( ! $this->compiled) { |
|
596 | - $this->compiled = $this->toBundleRoute()->compile(); |
|
597 | - } |
|
598 | - |
|
599 | - return $this->compiled; |
|
600 | - } |
|
601 | - |
|
602 | - /** |
|
603 | - * Get all of the parameter names for the route. |
|
604 | - * |
|
605 | - * @return array |
|
606 | - */ |
|
607 | - public function parameterNames(): array |
|
608 | - { |
|
609 | - if (isset($this->parameterNames)) { |
|
610 | - return $this->parameterNames; |
|
611 | - } |
|
612 | - |
|
613 | - return $this->parameterNames = $this->compileParameterNames(); |
|
614 | - } |
|
615 | - |
|
616 | - /** |
|
617 | - * Get the parameter names for the route. |
|
618 | - * |
|
619 | - * @return array |
|
620 | - */ |
|
621 | - protected function compileParameterNames(): array |
|
622 | - { |
|
623 | - preg_match_all('~\{(.*?)\}~', $this->getDomain().$this->getUri(), $matches); |
|
624 | - |
|
625 | - return array_map(fn ($match) => trim($match, '?'), $matches[1]); |
|
626 | - } |
|
627 | - |
|
628 | - /** |
|
629 | - * Get a given parameter from the route. |
|
630 | - * |
|
631 | - * @param string $name |
|
632 | - * @param mixed $default |
|
633 | - * |
|
634 | - * @return mixed |
|
635 | - */ |
|
636 | - public function parameter($name, $default = null) |
|
637 | - { |
|
638 | - return Arr::get($this->parameters(), $name, $default); |
|
639 | - } |
|
640 | - |
|
641 | - /** |
|
642 | - * Set a parameter to the given value. |
|
643 | - * |
|
644 | - * @param string $name |
|
645 | - * @param mixed $value |
|
646 | - * |
|
647 | - * @return void |
|
648 | - */ |
|
649 | - public function setParameter($name, $value): void |
|
650 | - { |
|
651 | - $this->parameters(); |
|
652 | - |
|
653 | - $this->parameters[$name] = $value; |
|
654 | - } |
|
655 | - |
|
656 | - /** |
|
657 | - * Get the key / value list of parameters without null values. |
|
658 | - * |
|
659 | - * @return array |
|
660 | - */ |
|
661 | - public function parametersWithouNulls(): array |
|
662 | - { |
|
663 | - return array_filter($this->parameters(), fn ($parameter) => ! is_null($parameter)); |
|
664 | - } |
|
665 | - |
|
666 | - /** |
|
667 | - * Get the key / value list of parameters for the route. |
|
668 | - * |
|
669 | - * @return array |
|
670 | - */ |
|
671 | - public function parameters(): array |
|
672 | - { |
|
673 | - if (isset($this->parameters)) { |
|
674 | - return $this->parameters; |
|
675 | - } |
|
676 | - |
|
677 | - throw new LogicException('The route is not bound'); |
|
678 | - } |
|
679 | - |
|
680 | - /** |
|
681 | - * Get all middleware, including the ones from the controller. |
|
682 | - * |
|
683 | - * @return array |
|
684 | - */ |
|
685 | - public function gatherMiddleware(): array |
|
686 | - { |
|
687 | - if ( ! is_null($this->computedMiddleware)) { |
|
688 | - return $this->computedMiddleware; |
|
689 | - } |
|
690 | - |
|
691 | - $this->computedMiddleware = []; |
|
692 | - |
|
693 | - return $this->computedMiddleware = Router::uniqueMiddleware(array_merge( |
|
694 | - $this->middleware(), |
|
695 | - $this->controllerMiddleware() |
|
696 | - )); |
|
697 | - } |
|
698 | - |
|
699 | - /** |
|
700 | - * Get or set the middlewares attached to the route. |
|
701 | - * |
|
702 | - * @param array|string|null $middleware |
|
703 | - * |
|
704 | - * @return array|static |
|
705 | - */ |
|
706 | - public function middleware($middleware = null) |
|
707 | - { |
|
708 | - if (is_null($middleware)) { |
|
709 | - return $this->getMiddleware(); |
|
710 | - } |
|
711 | - |
|
712 | - if (is_string($middleware)) { |
|
713 | - $middleware = func_get_args(); |
|
714 | - } |
|
715 | - |
|
716 | - foreach ($middleware as $index => $value) { |
|
717 | - $middleware[$index] = (string) $value; |
|
718 | - } |
|
719 | - |
|
720 | - $this->action['middleware'] = array_merge( |
|
721 | - $this->getMiddleware(), |
|
722 | - $middleware |
|
723 | - ); |
|
724 | - |
|
725 | - return $this; |
|
726 | - } |
|
727 | - |
|
728 | - /** |
|
729 | - * Get the middlewares attached to the route. |
|
730 | - * |
|
731 | - * @return array |
|
732 | - */ |
|
733 | - protected function getMiddleware(): array |
|
734 | - { |
|
735 | - return (array) ($this->action['middleware'] ?? []); |
|
736 | - } |
|
737 | - |
|
738 | - /** |
|
739 | - * Get the middleware for the route's controller. |
|
740 | - * |
|
741 | - * @return array |
|
742 | - */ |
|
743 | - public function controllerMiddleware(): array |
|
744 | - { |
|
745 | - if ( ! $this->isControllerAction()) { |
|
746 | - return []; |
|
747 | - } |
|
748 | - |
|
749 | - return $this->controllerDispatcher()->getMiddleware( |
|
750 | - $this->getController(), |
|
751 | - $this->getControllerMethod() |
|
752 | - ); |
|
753 | - } |
|
754 | - |
|
755 | - /** |
|
756 | - * Determine if the route only responds to HTTP requests. |
|
757 | - * |
|
758 | - * @return bool |
|
759 | - */ |
|
760 | - public function httpOnly(): bool |
|
761 | - { |
|
762 | - return in_array('http', $this->action, true); |
|
763 | - } |
|
764 | - |
|
765 | - /** |
|
766 | - * Determine if the route only responds to HTTPS requests. |
|
767 | - * |
|
768 | - * @return bool |
|
769 | - */ |
|
770 | - public function httpsOnly(): bool |
|
771 | - { |
|
772 | - return $this->secure(); |
|
773 | - } |
|
774 | - |
|
775 | - /** |
|
776 | - * Determine if the route only responds to HTTPS requests. |
|
777 | - * |
|
778 | - * @return bool |
|
779 | - */ |
|
780 | - public function secure(): bool |
|
781 | - { |
|
782 | - return in_array('https', $this->action, true); |
|
783 | - } |
|
583 | + $this->parameters = (new RouteParameter($this))->parameters($request); |
|
584 | + |
|
585 | + return $this; |
|
586 | + } |
|
587 | + |
|
588 | + /** |
|
589 | + * Compile into a Route Compile instance. |
|
590 | + * |
|
591 | + * @return \Syscodes\Bundles\ApplicationBundle\Routing\RouteCompiler |
|
592 | + */ |
|
593 | + protected function compileRoute() |
|
594 | + { |
|
595 | + if ( ! $this->compiled) { |
|
596 | + $this->compiled = $this->toBundleRoute()->compile(); |
|
597 | + } |
|
598 | + |
|
599 | + return $this->compiled; |
|
600 | + } |
|
601 | + |
|
602 | + /** |
|
603 | + * Get all of the parameter names for the route. |
|
604 | + * |
|
605 | + * @return array |
|
606 | + */ |
|
607 | + public function parameterNames(): array |
|
608 | + { |
|
609 | + if (isset($this->parameterNames)) { |
|
610 | + return $this->parameterNames; |
|
611 | + } |
|
612 | + |
|
613 | + return $this->parameterNames = $this->compileParameterNames(); |
|
614 | + } |
|
615 | + |
|
616 | + /** |
|
617 | + * Get the parameter names for the route. |
|
618 | + * |
|
619 | + * @return array |
|
620 | + */ |
|
621 | + protected function compileParameterNames(): array |
|
622 | + { |
|
623 | + preg_match_all('~\{(.*?)\}~', $this->getDomain().$this->getUri(), $matches); |
|
624 | + |
|
625 | + return array_map(fn ($match) => trim($match, '?'), $matches[1]); |
|
626 | + } |
|
627 | + |
|
628 | + /** |
|
629 | + * Get a given parameter from the route. |
|
630 | + * |
|
631 | + * @param string $name |
|
632 | + * @param mixed $default |
|
633 | + * |
|
634 | + * @return mixed |
|
635 | + */ |
|
636 | + public function parameter($name, $default = null) |
|
637 | + { |
|
638 | + return Arr::get($this->parameters(), $name, $default); |
|
639 | + } |
|
640 | + |
|
641 | + /** |
|
642 | + * Set a parameter to the given value. |
|
643 | + * |
|
644 | + * @param string $name |
|
645 | + * @param mixed $value |
|
646 | + * |
|
647 | + * @return void |
|
648 | + */ |
|
649 | + public function setParameter($name, $value): void |
|
650 | + { |
|
651 | + $this->parameters(); |
|
652 | + |
|
653 | + $this->parameters[$name] = $value; |
|
654 | + } |
|
655 | + |
|
656 | + /** |
|
657 | + * Get the key / value list of parameters without null values. |
|
658 | + * |
|
659 | + * @return array |
|
660 | + */ |
|
661 | + public function parametersWithouNulls(): array |
|
662 | + { |
|
663 | + return array_filter($this->parameters(), fn ($parameter) => ! is_null($parameter)); |
|
664 | + } |
|
665 | + |
|
666 | + /** |
|
667 | + * Get the key / value list of parameters for the route. |
|
668 | + * |
|
669 | + * @return array |
|
670 | + */ |
|
671 | + public function parameters(): array |
|
672 | + { |
|
673 | + if (isset($this->parameters)) { |
|
674 | + return $this->parameters; |
|
675 | + } |
|
676 | + |
|
677 | + throw new LogicException('The route is not bound'); |
|
678 | + } |
|
679 | + |
|
680 | + /** |
|
681 | + * Get all middleware, including the ones from the controller. |
|
682 | + * |
|
683 | + * @return array |
|
684 | + */ |
|
685 | + public function gatherMiddleware(): array |
|
686 | + { |
|
687 | + if ( ! is_null($this->computedMiddleware)) { |
|
688 | + return $this->computedMiddleware; |
|
689 | + } |
|
690 | + |
|
691 | + $this->computedMiddleware = []; |
|
692 | + |
|
693 | + return $this->computedMiddleware = Router::uniqueMiddleware(array_merge( |
|
694 | + $this->middleware(), |
|
695 | + $this->controllerMiddleware() |
|
696 | + )); |
|
697 | + } |
|
698 | + |
|
699 | + /** |
|
700 | + * Get or set the middlewares attached to the route. |
|
701 | + * |
|
702 | + * @param array|string|null $middleware |
|
703 | + * |
|
704 | + * @return array|static |
|
705 | + */ |
|
706 | + public function middleware($middleware = null) |
|
707 | + { |
|
708 | + if (is_null($middleware)) { |
|
709 | + return $this->getMiddleware(); |
|
710 | + } |
|
711 | + |
|
712 | + if (is_string($middleware)) { |
|
713 | + $middleware = func_get_args(); |
|
714 | + } |
|
715 | + |
|
716 | + foreach ($middleware as $index => $value) { |
|
717 | + $middleware[$index] = (string) $value; |
|
718 | + } |
|
719 | + |
|
720 | + $this->action['middleware'] = array_merge( |
|
721 | + $this->getMiddleware(), |
|
722 | + $middleware |
|
723 | + ); |
|
724 | + |
|
725 | + return $this; |
|
726 | + } |
|
727 | + |
|
728 | + /** |
|
729 | + * Get the middlewares attached to the route. |
|
730 | + * |
|
731 | + * @return array |
|
732 | + */ |
|
733 | + protected function getMiddleware(): array |
|
734 | + { |
|
735 | + return (array) ($this->action['middleware'] ?? []); |
|
736 | + } |
|
737 | + |
|
738 | + /** |
|
739 | + * Get the middleware for the route's controller. |
|
740 | + * |
|
741 | + * @return array |
|
742 | + */ |
|
743 | + public function controllerMiddleware(): array |
|
744 | + { |
|
745 | + if ( ! $this->isControllerAction()) { |
|
746 | + return []; |
|
747 | + } |
|
748 | + |
|
749 | + return $this->controllerDispatcher()->getMiddleware( |
|
750 | + $this->getController(), |
|
751 | + $this->getControllerMethod() |
|
752 | + ); |
|
753 | + } |
|
754 | + |
|
755 | + /** |
|
756 | + * Determine if the route only responds to HTTP requests. |
|
757 | + * |
|
758 | + * @return bool |
|
759 | + */ |
|
760 | + public function httpOnly(): bool |
|
761 | + { |
|
762 | + return in_array('http', $this->action, true); |
|
763 | + } |
|
764 | + |
|
765 | + /** |
|
766 | + * Determine if the route only responds to HTTPS requests. |
|
767 | + * |
|
768 | + * @return bool |
|
769 | + */ |
|
770 | + public function httpsOnly(): bool |
|
771 | + { |
|
772 | + return $this->secure(); |
|
773 | + } |
|
774 | + |
|
775 | + /** |
|
776 | + * Determine if the route only responds to HTTPS requests. |
|
777 | + * |
|
778 | + * @return bool |
|
779 | + */ |
|
780 | + public function secure(): bool |
|
781 | + { |
|
782 | + return in_array('https', $this->action, true); |
|
783 | + } |
|
784 | 784 | |
785 | - /** |
|
786 | - * Convert the route to a bundle route. |
|
787 | - * |
|
788 | - * @return \Syscodes\Bundles\ApplicationBundle\Routing\Route |
|
789 | - */ |
|
790 | - public function toBundleRoute() |
|
791 | - { |
|
792 | - return new BundleRoute( |
|
793 | - preg_replace('~\{(\w+?)\?\}~', '{$1}', $this->getUri()), |
|
794 | - $this->getOptionalParameterNames(), |
|
795 | - $this->getPatterns(), |
|
796 | - $this->getDomain() ?: '' |
|
797 | - ); |
|
798 | - } |
|
785 | + /** |
|
786 | + * Convert the route to a bundle route. |
|
787 | + * |
|
788 | + * @return \Syscodes\Bundles\ApplicationBundle\Routing\Route |
|
789 | + */ |
|
790 | + public function toBundleRoute() |
|
791 | + { |
|
792 | + return new BundleRoute( |
|
793 | + preg_replace('~\{(\w+?)\?\}~', '{$1}', $this->getUri()), |
|
794 | + $this->getOptionalParameterNames(), |
|
795 | + $this->getPatterns(), |
|
796 | + $this->getDomain() ?: '' |
|
797 | + ); |
|
798 | + } |
|
799 | 799 | |
800 | - /** |
|
801 | - * Get the optional parameter names for the route. |
|
802 | - * |
|
803 | - * @return array |
|
804 | - */ |
|
805 | - protected function getOptionalParameterNames(): array |
|
806 | - { |
|
807 | - preg_match_all('~\{(\w+?)\?\}~', $this->getUri(), $matches); |
|
800 | + /** |
|
801 | + * Get the optional parameter names for the route. |
|
802 | + * |
|
803 | + * @return array |
|
804 | + */ |
|
805 | + protected function getOptionalParameterNames(): array |
|
806 | + { |
|
807 | + preg_match_all('~\{(\w+?)\?\}~', $this->getUri(), $matches); |
|
808 | 808 | |
809 | - return isset($matches[1]) ? array_fill_keys($matches[1], null) : []; |
|
810 | - } |
|
811 | - |
|
812 | - /** |
|
813 | - * Get the action of the current route. |
|
814 | - * |
|
815 | - * @return \Closure|string|array |
|
816 | - */ |
|
817 | - public function getAction() |
|
818 | - { |
|
819 | - return $this->action; |
|
820 | - } |
|
809 | + return isset($matches[1]) ? array_fill_keys($matches[1], null) : []; |
|
810 | + } |
|
811 | + |
|
812 | + /** |
|
813 | + * Get the action of the current route. |
|
814 | + * |
|
815 | + * @return \Closure|string|array |
|
816 | + */ |
|
817 | + public function getAction() |
|
818 | + { |
|
819 | + return $this->action; |
|
820 | + } |
|
821 | 821 | |
822 | - /** |
|
823 | - * Get the compiled version of the route. |
|
824 | - * |
|
825 | - * @return \Syscodes\Bundles\ApplicationBundle\Routing\CompiledRoute |
|
826 | - */ |
|
827 | - public function getCompiled() |
|
828 | - { |
|
829 | - return $this->compiled; |
|
830 | - } |
|
831 | - |
|
832 | - /** |
|
833 | - * Get the URI associated with the route. |
|
834 | - * |
|
835 | - * @return string |
|
836 | - */ |
|
837 | - public function getUri(): string |
|
838 | - { |
|
839 | - return $this->uri; |
|
840 | - } |
|
841 | - |
|
842 | - /** |
|
843 | - * Get the patterns of the current route. |
|
844 | - * |
|
845 | - * @return array |
|
846 | - */ |
|
847 | - public function getPatterns(): array |
|
848 | - { |
|
849 | - return $this->wheres; |
|
850 | - } |
|
851 | - |
|
852 | - /** |
|
853 | - * Get the request method of the current route. |
|
854 | - * |
|
855 | - * @return array|string |
|
856 | - */ |
|
857 | - public function getMethod(): array|string |
|
858 | - { |
|
859 | - return $this->method; |
|
860 | - } |
|
861 | - |
|
862 | - /** |
|
863 | - * Get the url of the current route. |
|
864 | - * |
|
865 | - * @return string|null |
|
866 | - */ |
|
867 | - public function getName(): ?string |
|
868 | - { |
|
869 | - return $this->action['as'] ?? null; |
|
870 | - } |
|
871 | - |
|
872 | - /** |
|
873 | - * Set the container instance on the route. |
|
874 | - * |
|
875 | - * @param \Syscodes\Components\Container\Container $container |
|
876 | - * |
|
877 | - * @return static |
|
878 | - */ |
|
879 | - public function setContainer(Container $container): static |
|
880 | - { |
|
881 | - $this->container = $container; |
|
882 | - |
|
883 | - return $this; |
|
884 | - } |
|
885 | - |
|
886 | - /** |
|
887 | - * Magic method. |
|
888 | - * |
|
889 | - * Dynamically access route parameters. |
|
890 | - * |
|
891 | - * @param string $key |
|
892 | - * |
|
893 | - * @return mixed |
|
894 | - */ |
|
895 | - public function __get($key) |
|
896 | - { |
|
897 | - return $this->parameter($key); |
|
898 | - } |
|
822 | + /** |
|
823 | + * Get the compiled version of the route. |
|
824 | + * |
|
825 | + * @return \Syscodes\Bundles\ApplicationBundle\Routing\CompiledRoute |
|
826 | + */ |
|
827 | + public function getCompiled() |
|
828 | + { |
|
829 | + return $this->compiled; |
|
830 | + } |
|
831 | + |
|
832 | + /** |
|
833 | + * Get the URI associated with the route. |
|
834 | + * |
|
835 | + * @return string |
|
836 | + */ |
|
837 | + public function getUri(): string |
|
838 | + { |
|
839 | + return $this->uri; |
|
840 | + } |
|
841 | + |
|
842 | + /** |
|
843 | + * Get the patterns of the current route. |
|
844 | + * |
|
845 | + * @return array |
|
846 | + */ |
|
847 | + public function getPatterns(): array |
|
848 | + { |
|
849 | + return $this->wheres; |
|
850 | + } |
|
851 | + |
|
852 | + /** |
|
853 | + * Get the request method of the current route. |
|
854 | + * |
|
855 | + * @return array|string |
|
856 | + */ |
|
857 | + public function getMethod(): array|string |
|
858 | + { |
|
859 | + return $this->method; |
|
860 | + } |
|
861 | + |
|
862 | + /** |
|
863 | + * Get the url of the current route. |
|
864 | + * |
|
865 | + * @return string|null |
|
866 | + */ |
|
867 | + public function getName(): ?string |
|
868 | + { |
|
869 | + return $this->action['as'] ?? null; |
|
870 | + } |
|
871 | + |
|
872 | + /** |
|
873 | + * Set the container instance on the route. |
|
874 | + * |
|
875 | + * @param \Syscodes\Components\Container\Container $container |
|
876 | + * |
|
877 | + * @return static |
|
878 | + */ |
|
879 | + public function setContainer(Container $container): static |
|
880 | + { |
|
881 | + $this->container = $container; |
|
882 | + |
|
883 | + return $this; |
|
884 | + } |
|
885 | + |
|
886 | + /** |
|
887 | + * Magic method. |
|
888 | + * |
|
889 | + * Dynamically access route parameters. |
|
890 | + * |
|
891 | + * @param string $key |
|
892 | + * |
|
893 | + * @return mixed |
|
894 | + */ |
|
895 | + public function __get($key) |
|
896 | + { |
|
897 | + return $this->parameter($key); |
|
898 | + } |
|
899 | 899 | } |
900 | 900 | \ No newline at end of file |
@@ -37,171 +37,171 @@ |
||
37 | 37 | */ |
38 | 38 | class RouteResolver |
39 | 39 | { |
40 | - /** |
|
41 | - * The container instance used by the router. |
|
42 | - * |
|
43 | - * @var \Syscodes\Components\Contracts\Container\Container $container |
|
44 | - */ |
|
45 | - protected $container; |
|
46 | - |
|
47 | - /** |
|
48 | - * The currently dispatched route instance. |
|
49 | - * |
|
50 | - * @var \Syscodes\Components\Routing\Route|null |
|
51 | - */ |
|
52 | - protected $current; |
|
53 | - |
|
54 | - /** |
|
55 | - * The Route instance. |
|
56 | - * |
|
57 | - * @var \Syscodes\Components\Routing\Router|null |
|
58 | - */ |
|
59 | - protected $router; |
|
60 | - |
|
61 | - /** |
|
62 | - * The route collection instance. |
|
63 | - * |
|
64 | - * @var \Syscodes\Components\Routing\Collections\RouteCollection $routes |
|
65 | - */ |
|
66 | - protected $routes; |
|
67 | - |
|
68 | - /** |
|
69 | - * Constructor. Create a new RouteResolver instance. |
|
70 | - * |
|
71 | - * @param \Syscodes\Components\Routing\Router $router |
|
72 | - * @param \Syscodes\Components\Routing\Collections\RouteCollection $routes |
|
73 | - * @param \Syscodes\Components\Contracts\Container\Container|null $container |
|
74 | - * |
|
75 | - * @return void |
|
76 | - */ |
|
77 | - public function __construct(Router $router, RouteCollection $routes, Container $container = null) |
|
78 | - { |
|
79 | - $this->router = $router; |
|
80 | - $this->routes = $routes; |
|
81 | - $this->container = $container ?: new Container; |
|
82 | - } |
|
83 | - |
|
84 | - /** |
|
85 | - * Resolve the given route and call the method that belongs to the route. |
|
86 | - * |
|
87 | - * @param \Syscodes\Components\Http\Request $request |
|
88 | - * |
|
89 | - * @return \Syscodes\Components\Http\Response |
|
90 | - */ |
|
91 | - public function resolve(Request $request): Response |
|
92 | - { |
|
93 | - return $this->dispatchToRoute($request); |
|
94 | - } |
|
95 | - |
|
96 | - /** |
|
97 | - * Dispatch the request to a route and return the response. |
|
98 | - * |
|
99 | - * @param \Syscodes\Components\Http\Request $request |
|
100 | - * |
|
101 | - * @return \Syscodes\Components\Http\Response |
|
102 | - */ |
|
103 | - protected function dispatchToRoute(Request $request): Response |
|
104 | - { |
|
105 | - return $this->runRoute($request, $this->findRoute($request)); |
|
106 | - } |
|
107 | - |
|
108 | - /** |
|
109 | - * Find the route matching a given request. |
|
110 | - * |
|
111 | - * @param \Syscodes\Components\Http\Request $request |
|
112 | - * |
|
113 | - * @return \Syscodes\Components\Routing\Route |
|
114 | - */ |
|
115 | - protected function findRoute($request): Route |
|
116 | - { |
|
117 | - // Get all register routes with the same request method |
|
118 | - $this->current = $route = $this->routes->match($request); |
|
119 | - |
|
120 | - $this->container->instance(Route::class, $route); |
|
121 | - |
|
122 | - return $route; |
|
123 | - } |
|
124 | - |
|
125 | - /** |
|
126 | - * Return the response for the given route. |
|
127 | - * |
|
128 | - * @param \Syscodes\Components\Http\Request $request |
|
129 | - * @param \Syscodes\Components\Routing\Route $route |
|
130 | - * |
|
131 | - * @return \Syscodes\Components\Http\Response |
|
132 | - */ |
|
133 | - protected function runRoute(Request $request, Route $route): Response |
|
134 | - { |
|
135 | - $request->setRouteResolver(fn () => $route); |
|
136 | - |
|
137 | - return $this->callResponse($request, |
|
138 | - $this->runRouteStack($route, $request) |
|
139 | - ); |
|
140 | - } |
|
141 | - |
|
142 | - /** |
|
143 | - * Run the given route through a stack response instance. |
|
144 | - * |
|
145 | - * @param \Syscodes\Components\Routing\Route $route |
|
146 | - * @param \Syscodes\Components\Http\Request $request |
|
147 | - * |
|
148 | - * @return mixed |
|
149 | - */ |
|
150 | - protected function runRouteStack(Route $route, Request $request) |
|
151 | - { |
|
152 | - $skipMiddleware = $this->container->bound('middleware.disable') && |
|
153 | - ($this->container->make('middleware.disable') === true); |
|
40 | + /** |
|
41 | + * The container instance used by the router. |
|
42 | + * |
|
43 | + * @var \Syscodes\Components\Contracts\Container\Container $container |
|
44 | + */ |
|
45 | + protected $container; |
|
46 | + |
|
47 | + /** |
|
48 | + * The currently dispatched route instance. |
|
49 | + * |
|
50 | + * @var \Syscodes\Components\Routing\Route|null |
|
51 | + */ |
|
52 | + protected $current; |
|
53 | + |
|
54 | + /** |
|
55 | + * The Route instance. |
|
56 | + * |
|
57 | + * @var \Syscodes\Components\Routing\Router|null |
|
58 | + */ |
|
59 | + protected $router; |
|
60 | + |
|
61 | + /** |
|
62 | + * The route collection instance. |
|
63 | + * |
|
64 | + * @var \Syscodes\Components\Routing\Collections\RouteCollection $routes |
|
65 | + */ |
|
66 | + protected $routes; |
|
67 | + |
|
68 | + /** |
|
69 | + * Constructor. Create a new RouteResolver instance. |
|
70 | + * |
|
71 | + * @param \Syscodes\Components\Routing\Router $router |
|
72 | + * @param \Syscodes\Components\Routing\Collections\RouteCollection $routes |
|
73 | + * @param \Syscodes\Components\Contracts\Container\Container|null $container |
|
74 | + * |
|
75 | + * @return void |
|
76 | + */ |
|
77 | + public function __construct(Router $router, RouteCollection $routes, Container $container = null) |
|
78 | + { |
|
79 | + $this->router = $router; |
|
80 | + $this->routes = $routes; |
|
81 | + $this->container = $container ?: new Container; |
|
82 | + } |
|
83 | + |
|
84 | + /** |
|
85 | + * Resolve the given route and call the method that belongs to the route. |
|
86 | + * |
|
87 | + * @param \Syscodes\Components\Http\Request $request |
|
88 | + * |
|
89 | + * @return \Syscodes\Components\Http\Response |
|
90 | + */ |
|
91 | + public function resolve(Request $request): Response |
|
92 | + { |
|
93 | + return $this->dispatchToRoute($request); |
|
94 | + } |
|
95 | + |
|
96 | + /** |
|
97 | + * Dispatch the request to a route and return the response. |
|
98 | + * |
|
99 | + * @param \Syscodes\Components\Http\Request $request |
|
100 | + * |
|
101 | + * @return \Syscodes\Components\Http\Response |
|
102 | + */ |
|
103 | + protected function dispatchToRoute(Request $request): Response |
|
104 | + { |
|
105 | + return $this->runRoute($request, $this->findRoute($request)); |
|
106 | + } |
|
107 | + |
|
108 | + /** |
|
109 | + * Find the route matching a given request. |
|
110 | + * |
|
111 | + * @param \Syscodes\Components\Http\Request $request |
|
112 | + * |
|
113 | + * @return \Syscodes\Components\Routing\Route |
|
114 | + */ |
|
115 | + protected function findRoute($request): Route |
|
116 | + { |
|
117 | + // Get all register routes with the same request method |
|
118 | + $this->current = $route = $this->routes->match($request); |
|
119 | + |
|
120 | + $this->container->instance(Route::class, $route); |
|
121 | + |
|
122 | + return $route; |
|
123 | + } |
|
124 | + |
|
125 | + /** |
|
126 | + * Return the response for the given route. |
|
127 | + * |
|
128 | + * @param \Syscodes\Components\Http\Request $request |
|
129 | + * @param \Syscodes\Components\Routing\Route $route |
|
130 | + * |
|
131 | + * @return \Syscodes\Components\Http\Response |
|
132 | + */ |
|
133 | + protected function runRoute(Request $request, Route $route): Response |
|
134 | + { |
|
135 | + $request->setRouteResolver(fn () => $route); |
|
136 | + |
|
137 | + return $this->callResponse($request, |
|
138 | + $this->runRouteStack($route, $request) |
|
139 | + ); |
|
140 | + } |
|
141 | + |
|
142 | + /** |
|
143 | + * Run the given route through a stack response instance. |
|
144 | + * |
|
145 | + * @param \Syscodes\Components\Routing\Route $route |
|
146 | + * @param \Syscodes\Components\Http\Request $request |
|
147 | + * |
|
148 | + * @return mixed |
|
149 | + */ |
|
150 | + protected function runRouteStack(Route $route, Request $request) |
|
151 | + { |
|
152 | + $skipMiddleware = $this->container->bound('middleware.disable') && |
|
153 | + ($this->container->make('middleware.disable') === true); |
|
154 | 154 | |
155 | 155 | |
156 | - $middleware = $skipMiddleware ? [] : $this->router->gatherRouteMiddleware($route); |
|
157 | - |
|
158 | - return (new Pipeline($this->container)) |
|
159 | - ->send($request) |
|
160 | - ->through($middleware) |
|
161 | - ->then(fn ($request) => $this->callResponse($request, $route->runResolver())); |
|
162 | - } |
|
163 | - |
|
164 | - /** |
|
165 | - * Create a response instance from the given value. |
|
166 | - * |
|
167 | - * @param \Syscodes\Components\Http\Request $request |
|
168 | - * @param mixed $response |
|
169 | - * |
|
170 | - * @return \Syscodes\Components\Http\Response |
|
171 | - */ |
|
172 | - public function callResponse($request, $response): Response |
|
173 | - { |
|
174 | - return static::toResponse($request, $response); |
|
175 | - } |
|
176 | - |
|
177 | - /** |
|
178 | - * Static version of callResponse. |
|
179 | - * |
|
180 | - * @param \Syscodes\Components\Http\Request $request |
|
181 | - * @param mixed $response |
|
182 | - * |
|
183 | - * @return \Syscodes\Components\Http\Response |
|
184 | - */ |
|
185 | - public static function toResponse($request, $response): Response |
|
186 | - { |
|
187 | - if ( ! $response instanceof Response && |
|
188 | - ($response instanceof Jsonserializable || |
|
189 | - is_array($response))) { |
|
190 | - $response = new JsonResponse($response); |
|
191 | - } elseif ( ! $response instanceof Response) { |
|
192 | - $response = new Response($response, 200, ['Content-Type' => 'text/html']); |
|
193 | - } |
|
194 | - |
|
195 | - return $response->prepare($request); |
|
196 | - } |
|
197 | - |
|
198 | - /** |
|
199 | - * Get the currently dispatched route instance. |
|
200 | - * |
|
201 | - * @return \Syscodes\Components\Routing\Route|null |
|
202 | - */ |
|
203 | - public function current() |
|
204 | - { |
|
205 | - return $this->current; |
|
206 | - } |
|
156 | + $middleware = $skipMiddleware ? [] : $this->router->gatherRouteMiddleware($route); |
|
157 | + |
|
158 | + return (new Pipeline($this->container)) |
|
159 | + ->send($request) |
|
160 | + ->through($middleware) |
|
161 | + ->then(fn ($request) => $this->callResponse($request, $route->runResolver())); |
|
162 | + } |
|
163 | + |
|
164 | + /** |
|
165 | + * Create a response instance from the given value. |
|
166 | + * |
|
167 | + * @param \Syscodes\Components\Http\Request $request |
|
168 | + * @param mixed $response |
|
169 | + * |
|
170 | + * @return \Syscodes\Components\Http\Response |
|
171 | + */ |
|
172 | + public function callResponse($request, $response): Response |
|
173 | + { |
|
174 | + return static::toResponse($request, $response); |
|
175 | + } |
|
176 | + |
|
177 | + /** |
|
178 | + * Static version of callResponse. |
|
179 | + * |
|
180 | + * @param \Syscodes\Components\Http\Request $request |
|
181 | + * @param mixed $response |
|
182 | + * |
|
183 | + * @return \Syscodes\Components\Http\Response |
|
184 | + */ |
|
185 | + public static function toResponse($request, $response): Response |
|
186 | + { |
|
187 | + if ( ! $response instanceof Response && |
|
188 | + ($response instanceof Jsonserializable || |
|
189 | + is_array($response))) { |
|
190 | + $response = new JsonResponse($response); |
|
191 | + } elseif ( ! $response instanceof Response) { |
|
192 | + $response = new Response($response, 200, ['Content-Type' => 'text/html']); |
|
193 | + } |
|
194 | + |
|
195 | + return $response->prepare($request); |
|
196 | + } |
|
197 | + |
|
198 | + /** |
|
199 | + * Get the currently dispatched route instance. |
|
200 | + * |
|
201 | + * @return \Syscodes\Components\Routing\Route|null |
|
202 | + */ |
|
203 | + public function current() |
|
204 | + { |
|
205 | + return $this->current; |
|
206 | + } |
|
207 | 207 | } |
208 | 208 | \ No newline at end of file |
@@ -29,105 +29,105 @@ |
||
29 | 29 | */ |
30 | 30 | class RouteGroup |
31 | 31 | { |
32 | - /** |
|
33 | - * Merge the given group attributes. |
|
34 | - * |
|
35 | - * @param array $new |
|
36 | - * @param array $old |
|
37 | - * @param bool $existsPrefix |
|
38 | - * |
|
39 | - * @return array |
|
40 | - */ |
|
41 | - public static function mergeGroup($new, $old, $existsPrefix = true): array |
|
42 | - { |
|
43 | - if (isset($new['domain'])) { |
|
44 | - unset($old['domain']); |
|
45 | - } |
|
32 | + /** |
|
33 | + * Merge the given group attributes. |
|
34 | + * |
|
35 | + * @param array $new |
|
36 | + * @param array $old |
|
37 | + * @param bool $existsPrefix |
|
38 | + * |
|
39 | + * @return array |
|
40 | + */ |
|
41 | + public static function mergeGroup($new, $old, $existsPrefix = true): array |
|
42 | + { |
|
43 | + if (isset($new['domain'])) { |
|
44 | + unset($old['domain']); |
|
45 | + } |
|
46 | 46 | |
47 | - if (isset($new['controller'])) { |
|
48 | - unset($old['controller']); |
|
49 | - } |
|
47 | + if (isset($new['controller'])) { |
|
48 | + unset($old['controller']); |
|
49 | + } |
|
50 | 50 | |
51 | - $new = array_merge(static::formatUseAs($new, $old), [ |
|
52 | - 'namespace' => static::formatUseNamespace($new, $old), |
|
53 | - 'prefix' => static::formatUsePrefix($new, $old, $existsPrefix), |
|
54 | - 'where' => static::formatUseWhere($new, $old) |
|
55 | - ]); |
|
51 | + $new = array_merge(static::formatUseAs($new, $old), [ |
|
52 | + 'namespace' => static::formatUseNamespace($new, $old), |
|
53 | + 'prefix' => static::formatUsePrefix($new, $old, $existsPrefix), |
|
54 | + 'where' => static::formatUseWhere($new, $old) |
|
55 | + ]); |
|
56 | 56 | |
57 | - return array_merge_recursive( |
|
58 | - Arr::except($old, array('namespace', 'prefix', 'where', 'as')), $new |
|
59 | - ); |
|
60 | - } |
|
57 | + return array_merge_recursive( |
|
58 | + Arr::except($old, array('namespace', 'prefix', 'where', 'as')), $new |
|
59 | + ); |
|
60 | + } |
|
61 | 61 | |
62 | - /** |
|
63 | - * Format the uses namespace for the new group attributes. |
|
64 | - * |
|
65 | - * @param array $new |
|
66 | - * @param array $old |
|
67 | - * |
|
68 | - * @return string|null |
|
69 | - */ |
|
70 | - protected static function formatUseNamespace($new, $old): ?string |
|
71 | - { |
|
72 | - if (isset($new['namespace'])) { |
|
73 | - return isset($old['namespace']) |
|
74 | - ? trim($old['namespace'], '\\').'\\'.trim($new['namespace'], '\\') |
|
75 | - : trim($new['namespace'], '\\'); |
|
76 | - } |
|
62 | + /** |
|
63 | + * Format the uses namespace for the new group attributes. |
|
64 | + * |
|
65 | + * @param array $new |
|
66 | + * @param array $old |
|
67 | + * |
|
68 | + * @return string|null |
|
69 | + */ |
|
70 | + protected static function formatUseNamespace($new, $old): ?string |
|
71 | + { |
|
72 | + if (isset($new['namespace'])) { |
|
73 | + return isset($old['namespace']) |
|
74 | + ? trim($old['namespace'], '\\').'\\'.trim($new['namespace'], '\\') |
|
75 | + : trim($new['namespace'], '\\'); |
|
76 | + } |
|
77 | 77 | |
78 | - return $old['namespace'] ?? null; |
|
79 | - } |
|
78 | + return $old['namespace'] ?? null; |
|
79 | + } |
|
80 | 80 | |
81 | - /** |
|
82 | - * Format the prefix for the new group attributes. |
|
83 | - * |
|
84 | - * @param array $new |
|
85 | - * @param array $old |
|
86 | - * @param bool $existPrefix |
|
87 | - * |
|
88 | - * @return string|null |
|
89 | - */ |
|
90 | - protected static function formatUsePrefix($new, $old, bool $existsPrefix = true): ?string |
|
91 | - { |
|
92 | - $old = $old['prefix'] ?? null; |
|
81 | + /** |
|
82 | + * Format the prefix for the new group attributes. |
|
83 | + * |
|
84 | + * @param array $new |
|
85 | + * @param array $old |
|
86 | + * @param bool $existPrefix |
|
87 | + * |
|
88 | + * @return string|null |
|
89 | + */ |
|
90 | + protected static function formatUsePrefix($new, $old, bool $existsPrefix = true): ?string |
|
91 | + { |
|
92 | + $old = $old['prefix'] ?? null; |
|
93 | 93 | |
94 | - if ($existsPrefix) { |
|
95 | - return trim(isset($new['prefix']) ? trim($old, '/').'/'.trim($new['prefix'], '/') : $old, '/'); |
|
96 | - } else { |
|
97 | - return trim(isset($new['prefix']) ? trim($new['prefix'], '/').'/'.trim($old, '/') : $old, '/'); |
|
98 | - } |
|
99 | - } |
|
94 | + if ($existsPrefix) { |
|
95 | + return trim(isset($new['prefix']) ? trim($old, '/').'/'.trim($new['prefix'], '/') : $old, '/'); |
|
96 | + } else { |
|
97 | + return trim(isset($new['prefix']) ? trim($new['prefix'], '/').'/'.trim($old, '/') : $old, '/'); |
|
98 | + } |
|
99 | + } |
|
100 | 100 | |
101 | - /** |
|
102 | - * Format the "wheres" for the new group attributes. |
|
103 | - * |
|
104 | - * @param array $new |
|
105 | - * @param array $old |
|
106 | - * |
|
107 | - * @return array |
|
108 | - */ |
|
109 | - protected static function formatUseWhere($new, $old): array |
|
110 | - { |
|
111 | - return array_merge( |
|
112 | - $old['where'] ?? [], |
|
113 | - $new['where'] ?? [] |
|
114 | - ); |
|
115 | - } |
|
101 | + /** |
|
102 | + * Format the "wheres" for the new group attributes. |
|
103 | + * |
|
104 | + * @param array $new |
|
105 | + * @param array $old |
|
106 | + * |
|
107 | + * @return array |
|
108 | + */ |
|
109 | + protected static function formatUseWhere($new, $old): array |
|
110 | + { |
|
111 | + return array_merge( |
|
112 | + $old['where'] ?? [], |
|
113 | + $new['where'] ?? [] |
|
114 | + ); |
|
115 | + } |
|
116 | 116 | |
117 | - /** |
|
118 | - * Format the "as" clause of the new group attributes. |
|
119 | - * |
|
120 | - * @param array $new |
|
121 | - * @param array $old |
|
122 | - * |
|
123 | - * @return array |
|
124 | - */ |
|
125 | - protected static function formatUseAs($new, $old): array |
|
126 | - { |
|
127 | - if (isset($old['as'])) { |
|
128 | - $new['as'] = $old['as'].($new['as'] ?? ''); |
|
129 | - } |
|
117 | + /** |
|
118 | + * Format the "as" clause of the new group attributes. |
|
119 | + * |
|
120 | + * @param array $new |
|
121 | + * @param array $old |
|
122 | + * |
|
123 | + * @return array |
|
124 | + */ |
|
125 | + protected static function formatUseAs($new, $old): array |
|
126 | + { |
|
127 | + if (isset($old['as'])) { |
|
128 | + $new['as'] = $old['as'].($new['as'] ?? ''); |
|
129 | + } |
|
130 | 130 | |
131 | - return $new; |
|
132 | - } |
|
131 | + return $new; |
|
132 | + } |
|
133 | 133 | } |
134 | 134 | \ No newline at end of file |
@@ -40,7 +40,6 @@ |
||
40 | 40 | * Create a new route file registrar instance. |
41 | 41 | * |
42 | 42 | * @param \Syscodes\Components\Routing\Router $router |
43 | - |
|
44 | 43 | * @return void |
45 | 44 | */ |
46 | 45 | public function __construct(Router $router) |
@@ -74,14 +74,14 @@ discard block |
||
74 | 74 | */ |
75 | 75 | protected function registerUrlGenerator() |
76 | 76 | { |
77 | - $this->app->singleton('url', function ($app) { |
|
77 | + $this->app->singleton('url', function($app) { |
|
78 | 78 | $routes = $app['router']->getRoutes(); |
79 | 79 | |
80 | 80 | return new UrlGenerator($routes, $app['request']); |
81 | 81 | }); |
82 | 82 | |
83 | - $this->app->extend('url', function (UrlGeneratorContract $url, $app) { |
|
84 | - $url->setSessionResolver(function () use ($app) { |
|
83 | + $this->app->extend('url', function(UrlGeneratorContract $url, $app) { |
|
84 | + $url->setSessionResolver(function() use ($app) { |
|
85 | 85 | return $app['session'] ?? null; |
86 | 86 | }); |
87 | 87 | |
@@ -96,7 +96,7 @@ discard block |
||
96 | 96 | */ |
97 | 97 | protected function registerRedirector() |
98 | 98 | { |
99 | - $this->app->singleton('redirect', function ($app) { |
|
99 | + $this->app->singleton('redirect', function($app) { |
|
100 | 100 | $redirector = new Redirector($app['url']); |
101 | 101 | |
102 | 102 | if (isset($app['session.store'])) { |
@@ -152,7 +152,7 @@ discard block |
||
152 | 152 | * |
153 | 153 | * @return string|null |
154 | 154 | */ |
155 | - protected function getPreviousUrlSession(): string|null |
|
155 | + protected function getPreviousUrlSession(): string | null |
|
156 | 156 | { |
157 | 157 | $session = $this->getSession(); |
158 | 158 | |
@@ -427,7 +427,7 @@ discard block |
||
427 | 427 | */ |
428 | 428 | public function format($root, $path, $tail = ''): string |
429 | 429 | { |
430 | - return trim($root .'/' .trim($path .'/' .$tail, '/'), '/'); |
|
430 | + return trim($root.'/'.trim($path.'/'.$tail, '/'), '/'); |
|
431 | 431 | } |
432 | 432 | |
433 | 433 | /** |
@@ -103,7 +103,7 @@ |
||
103 | 103 | $uri = $this->url->format( |
104 | 104 | $root, |
105 | 105 | $this->replaceRouteParameters($route->getUri(), $parameters) |
106 | - ); |
|
106 | + ); |
|
107 | 107 | |
108 | 108 | if (preg_match_all('/{(.*?)}/', $uri, $missingParameters)) { |
109 | 109 | throw UrlGeneratorException::missingParameters($route, $missingParameters[1]); |
@@ -286,10 +286,10 @@ |
||
286 | 286 | // parameters that are in the array and add them to the query string or we |
287 | 287 | // will make the initial query string if it wasn't started with strings. |
288 | 288 | if (count($keyed) < count($parameters)) { |
289 | - $query .= '&' .implode('&', $this->getNumericParameters($parameters)); |
|
289 | + $query .= '&'.implode('&', $this->getNumericParameters($parameters)); |
|
290 | 290 | } |
291 | 291 | |
292 | - return '?' .trim($query, '&'); |
|
292 | + return '?'.trim($query, '&'); |
|
293 | 293 | } |
294 | 294 | |
295 | 295 | /** |
@@ -30,247 +30,247 @@ |
||
30 | 30 | */ |
31 | 31 | class TemplateHandler |
32 | 32 | { |
33 | - /** |
|
34 | - * Benchmark instance. |
|
35 | - * |
|
36 | - * @var string $benchmark |
|
37 | - */ |
|
38 | - protected $benchmark; |
|
33 | + /** |
|
34 | + * Benchmark instance. |
|
35 | + * |
|
36 | + * @var string $benchmark |
|
37 | + */ |
|
38 | + protected $benchmark; |
|
39 | 39 | |
40 | - /** |
|
41 | - * Nesting level of the output buffering mechanism. |
|
42 | - * |
|
43 | - * @var string $obLevel |
|
44 | - */ |
|
45 | - public $obLevel; |
|
40 | + /** |
|
41 | + * Nesting level of the output buffering mechanism. |
|
42 | + * |
|
43 | + * @var string $obLevel |
|
44 | + */ |
|
45 | + public $obLevel; |
|
46 | 46 | |
47 | - /** |
|
48 | - * The functions of system what control errors and exceptions. |
|
49 | - * |
|
50 | - * @var string|object $system |
|
51 | - */ |
|
52 | - protected $system; |
|
47 | + /** |
|
48 | + * The functions of system what control errors and exceptions. |
|
49 | + * |
|
50 | + * @var string|object $system |
|
51 | + */ |
|
52 | + protected $system; |
|
53 | 53 | |
54 | - /** |
|
55 | - * An array of variables to be passed to all templates. |
|
56 | - * |
|
57 | - * @var array $variables |
|
58 | - */ |
|
59 | - protected $variables = []; |
|
54 | + /** |
|
55 | + * An array of variables to be passed to all templates. |
|
56 | + * |
|
57 | + * @var array $variables |
|
58 | + */ |
|
59 | + protected $variables = []; |
|
60 | 60 | |
61 | - /** |
|
62 | - * Constructor. The TemplateHandler class instance. |
|
63 | - * |
|
64 | - * @return void |
|
65 | - */ |
|
66 | - public function __construct() |
|
67 | - { |
|
68 | - $this->system = new System; |
|
69 | - $this->benchmark = new Benchmark; |
|
70 | - $this->obLevel = $this->system->getOutputBufferLevel(); |
|
71 | - } |
|
61 | + /** |
|
62 | + * Constructor. The TemplateHandler class instance. |
|
63 | + * |
|
64 | + * @return void |
|
65 | + */ |
|
66 | + public function __construct() |
|
67 | + { |
|
68 | + $this->system = new System; |
|
69 | + $this->benchmark = new Benchmark; |
|
70 | + $this->obLevel = $this->system->getOutputBufferLevel(); |
|
71 | + } |
|
72 | 72 | |
73 | - /** |
|
74 | - * Clean Path: This makes nicer looking paths for the error output. |
|
75 | - * |
|
76 | - * @param string $file |
|
77 | - * |
|
78 | - * @return string |
|
79 | - */ |
|
80 | - public function cleanPath($file): string |
|
81 | - { |
|
82 | - if (strpos($file, appPath().DIRECTORY_SEPARATOR) === 0) { |
|
83 | - $file = appPath().DIRECTORY_SEPARATOR.substr($file, strlen(appPath().DIRECTORY_SEPARATOR)); |
|
84 | - } elseif (strpos($file, basePath().DIRECTORY_SEPARATOR) === 0) { |
|
85 | - $file = basePath().DIRECTORY_SEPARATOR.substr($file, strlen(basePath().DIRECTORY_SEPARATOR)); |
|
86 | - } elseif (strpos($file, configPath().DIRECTORY_SEPARATOR) === 0) { |
|
87 | - $file = configPath().DIRECTORY_SEPARATOR.substr($file, strlen(configPath().DIRECTORY_SEPARATOR)); |
|
88 | - } elseif (strpos($file, resourcePath().DIRECTORY_SEPARATOR) === 0) { |
|
89 | - $file = resourcePath().DIRECTORY_SEPARATOR.substr($file, strlen(resourcePath().DIRECTORY_SEPARATOR)); |
|
90 | - } |
|
73 | + /** |
|
74 | + * Clean Path: This makes nicer looking paths for the error output. |
|
75 | + * |
|
76 | + * @param string $file |
|
77 | + * |
|
78 | + * @return string |
|
79 | + */ |
|
80 | + public function cleanPath($file): string |
|
81 | + { |
|
82 | + if (strpos($file, appPath().DIRECTORY_SEPARATOR) === 0) { |
|
83 | + $file = appPath().DIRECTORY_SEPARATOR.substr($file, strlen(appPath().DIRECTORY_SEPARATOR)); |
|
84 | + } elseif (strpos($file, basePath().DIRECTORY_SEPARATOR) === 0) { |
|
85 | + $file = basePath().DIRECTORY_SEPARATOR.substr($file, strlen(basePath().DIRECTORY_SEPARATOR)); |
|
86 | + } elseif (strpos($file, configPath().DIRECTORY_SEPARATOR) === 0) { |
|
87 | + $file = configPath().DIRECTORY_SEPARATOR.substr($file, strlen(configPath().DIRECTORY_SEPARATOR)); |
|
88 | + } elseif (strpos($file, resourcePath().DIRECTORY_SEPARATOR) === 0) { |
|
89 | + $file = resourcePath().DIRECTORY_SEPARATOR.substr($file, strlen(resourcePath().DIRECTORY_SEPARATOR)); |
|
90 | + } |
|
91 | 91 | |
92 | - return $file; |
|
93 | - } |
|
92 | + return $file; |
|
93 | + } |
|
94 | 94 | |
95 | - /** |
|
96 | - * Display memory usage in real-world units. Intended for use |
|
97 | - * with memory_get_usage, etc. |
|
98 | - * |
|
99 | - * @param int $bytes |
|
100 | - * |
|
101 | - * @return string |
|
102 | - */ |
|
103 | - public function displayMemory(int $bytes): string |
|
104 | - { |
|
105 | - if ($bytes < 1024) { |
|
106 | - return $bytes.'B'; |
|
107 | - } else if ($bytes < 1048576) { |
|
108 | - return round($bytes/1024, 2).'KB'; |
|
109 | - } |
|
95 | + /** |
|
96 | + * Display memory usage in real-world units. Intended for use |
|
97 | + * with memory_get_usage, etc. |
|
98 | + * |
|
99 | + * @param int $bytes |
|
100 | + * |
|
101 | + * @return string |
|
102 | + */ |
|
103 | + public function displayMemory(int $bytes): string |
|
104 | + { |
|
105 | + if ($bytes < 1024) { |
|
106 | + return $bytes.'B'; |
|
107 | + } else if ($bytes < 1048576) { |
|
108 | + return round($bytes/1024, 2).'KB'; |
|
109 | + } |
|
110 | 110 | |
111 | - return round($bytes/1048576, 2).'MB'; |
|
112 | - } |
|
111 | + return round($bytes/1048576, 2).'MB'; |
|
112 | + } |
|
113 | 113 | |
114 | - /** |
|
115 | - * Escapes a string for output in an HTML document. |
|
116 | - * |
|
117 | - * @param string $text |
|
118 | - * |
|
119 | - * @return string |
|
120 | - */ |
|
121 | - public function escape($text): string |
|
122 | - { |
|
123 | - $flags = ENT_QUOTES; |
|
114 | + /** |
|
115 | + * Escapes a string for output in an HTML document. |
|
116 | + * |
|
117 | + * @param string $text |
|
118 | + * |
|
119 | + * @return string |
|
120 | + */ |
|
121 | + public function escape($text): string |
|
122 | + { |
|
123 | + $flags = ENT_QUOTES; |
|
124 | 124 | |
125 | - // HHVM has all constants defined, but only ENT_IGNORE |
|
126 | - // works at the moment |
|
127 | - if (defined("ENT_SUBSTITUTE") && ! defined("HHVM_VERSION")) { |
|
128 | - $flags |= ENT_SUBSTITUTE; |
|
129 | - } else { |
|
130 | - $flags |= ENT_IGNORE; |
|
131 | - } |
|
125 | + // HHVM has all constants defined, but only ENT_IGNORE |
|
126 | + // works at the moment |
|
127 | + if (defined("ENT_SUBSTITUTE") && ! defined("HHVM_VERSION")) { |
|
128 | + $flags |= ENT_SUBSTITUTE; |
|
129 | + } else { |
|
130 | + $flags |= ENT_IGNORE; |
|
131 | + } |
|
132 | 132 | |
133 | - $text = str_replace(chr(9), ' ', $text); |
|
133 | + $text = str_replace(chr(9), ' ', $text); |
|
134 | 134 | |
135 | - return htmlspecialchars($text, $flags, "UTF-8"); |
|
136 | - } |
|
135 | + return htmlspecialchars($text, $flags, "UTF-8"); |
|
136 | + } |
|
137 | 137 | |
138 | - /** |
|
139 | - * Returns all variables for this helper. |
|
140 | - * |
|
141 | - * @return array |
|
142 | - */ |
|
143 | - public function getVariables(): array |
|
144 | - { |
|
145 | - return $this->variables; |
|
146 | - } |
|
138 | + /** |
|
139 | + * Returns all variables for this helper. |
|
140 | + * |
|
141 | + * @return array |
|
142 | + */ |
|
143 | + public function getVariables(): array |
|
144 | + { |
|
145 | + return $this->variables; |
|
146 | + } |
|
147 | 147 | |
148 | - /** |
|
149 | - * Creates a syntax-highlighted version of a PHP file. |
|
150 | - * |
|
151 | - * @param string $file |
|
152 | - * @param int $lineNumber |
|
153 | - * @param int $lines |
|
154 | - * |
|
155 | - * @return bool|string |
|
156 | - * |
|
157 | - * @throws \Exception |
|
158 | - */ |
|
159 | - public function highlightFile($file, $lineNumber, $lines = 15) |
|
160 | - { |
|
161 | - if (empty ($file) || ! is_readable($file)) { |
|
162 | - return false; |
|
163 | - } |
|
148 | + /** |
|
149 | + * Creates a syntax-highlighted version of a PHP file. |
|
150 | + * |
|
151 | + * @param string $file |
|
152 | + * @param int $lineNumber |
|
153 | + * @param int $lines |
|
154 | + * |
|
155 | + * @return bool|string |
|
156 | + * |
|
157 | + * @throws \Exception |
|
158 | + */ |
|
159 | + public function highlightFile($file, $lineNumber, $lines = 15) |
|
160 | + { |
|
161 | + if (empty ($file) || ! is_readable($file)) { |
|
162 | + return false; |
|
163 | + } |
|
164 | 164 | |
165 | - // Set our highlight colors: |
|
166 | - if (function_exists('ini_set')) { |
|
167 | - ini_set('highlight.bg', '#000'); |
|
168 | - ini_set('highlight.comment', '#B5B5B5'); |
|
169 | - ini_set('highlight.default', '#E5E5E5'); |
|
170 | - ini_set('highlight.html', '#06B'); |
|
171 | - ini_set('highlight.keyword', '#FF9361'); |
|
172 | - ini_set('highlight.string', '#61ABFF'); |
|
173 | - } |
|
165 | + // Set our highlight colors: |
|
166 | + if (function_exists('ini_set')) { |
|
167 | + ini_set('highlight.bg', '#000'); |
|
168 | + ini_set('highlight.comment', '#B5B5B5'); |
|
169 | + ini_set('highlight.default', '#E5E5E5'); |
|
170 | + ini_set('highlight.html', '#06B'); |
|
171 | + ini_set('highlight.keyword', '#FF9361'); |
|
172 | + ini_set('highlight.string', '#61ABFF'); |
|
173 | + } |
|
174 | 174 | |
175 | - try { |
|
176 | - $origin = file_get_contents($file); |
|
177 | - } catch (Exception $e) { |
|
178 | - return false; |
|
179 | - } |
|
175 | + try { |
|
176 | + $origin = file_get_contents($file); |
|
177 | + } catch (Exception $e) { |
|
178 | + return false; |
|
179 | + } |
|
180 | 180 | |
181 | - $origin = str_replace(["\r\n", "\r"], "\n", $origin); |
|
182 | - $origin = explode("\n", highlight_string($origin , true)); |
|
183 | - $origin = str_replace('<br />', "\n", $origin [1]); |
|
181 | + $origin = str_replace(["\r\n", "\r"], "\n", $origin); |
|
182 | + $origin = explode("\n", highlight_string($origin , true)); |
|
183 | + $origin = str_replace('<br />', "\n", $origin [1]); |
|
184 | 184 | |
185 | - $origin = explode("\n", str_replace("\r\n", "\n", $origin)); |
|
185 | + $origin = explode("\n", str_replace("\r\n", "\n", $origin)); |
|
186 | 186 | |
187 | - // Get just the part to show |
|
188 | - $start = $lineNumber - (int) round($lines / 2); |
|
189 | - $start = $start < 0 ? 0 : $start; |
|
187 | + // Get just the part to show |
|
188 | + $start = $lineNumber - (int) round($lines / 2); |
|
189 | + $start = $start < 0 ? 0 : $start; |
|
190 | 190 | |
191 | - // Get just the lines we need to display, while keeping line numbers... |
|
192 | - $origin = array_splice($origin, $start, $lines, true); |
|
191 | + // Get just the lines we need to display, while keeping line numbers... |
|
192 | + $origin = array_splice($origin, $start, $lines, true); |
|
193 | 193 | |
194 | - // Used to format the line number in the source |
|
195 | - $format = '% '.strlen($start + $lines).'d'; |
|
194 | + // Used to format the line number in the source |
|
195 | + $format = '% '.strlen($start + $lines).'d'; |
|
196 | 196 | |
197 | - $out = ''; |
|
198 | - // Because the highlighting may have an uneven number |
|
199 | - // of open and close span tags on one line, we need |
|
200 | - // to ensure we can close them all to get the lines |
|
201 | - // showing correctly. |
|
202 | - $spans = 1; |
|
197 | + $out = ''; |
|
198 | + // Because the highlighting may have an uneven number |
|
199 | + // of open and close span tags on one line, we need |
|
200 | + // to ensure we can close them all to get the lines |
|
201 | + // showing correctly. |
|
202 | + $spans = 1; |
|
203 | 203 | |
204 | - foreach ($origin as $n => $row) { |
|
205 | - $spans += substr_count($row, '<span') - substr_count($row, '</span'); |
|
206 | - $row = str_replace(["\r", "\n"], ['', ''], $row); |
|
204 | + foreach ($origin as $n => $row) { |
|
205 | + $spans += substr_count($row, '<span') - substr_count($row, '</span'); |
|
206 | + $row = str_replace(["\r", "\n"], ['', ''], $row); |
|
207 | 207 | |
208 | - if (($n+$start+1) == $lineNumber) { |
|
209 | - preg_match_all('#<[^>]+>#', $row, $tags); |
|
210 | - $out .= sprintf("<span class='line highlight'><span class='number'>{$format}</span> %s\n</span>%s", |
|
211 | - $n + $start + 1, |
|
212 | - strip_tags($row), |
|
213 | - implode('', $tags[0]) |
|
214 | - ); |
|
215 | - } else { |
|
216 | - $out .= sprintf('<span class="number">'.$format.'</span> %s <span class="line">', $n + $start + 1, $row) ."\n"; |
|
217 | - } |
|
218 | - } |
|
208 | + if (($n+$start+1) == $lineNumber) { |
|
209 | + preg_match_all('#<[^>]+>#', $row, $tags); |
|
210 | + $out .= sprintf("<span class='line highlight'><span class='number'>{$format}</span> %s\n</span>%s", |
|
211 | + $n + $start + 1, |
|
212 | + strip_tags($row), |
|
213 | + implode('', $tags[0]) |
|
214 | + ); |
|
215 | + } else { |
|
216 | + $out .= sprintf('<span class="number">'.$format.'</span> %s <span class="line">', $n + $start + 1, $row) ."\n"; |
|
217 | + } |
|
218 | + } |
|
219 | 219 | |
220 | - $out .= str_repeat('</span>', $spans); |
|
220 | + $out .= str_repeat('</span>', $spans); |
|
221 | 221 | |
222 | - return '<pre class="code-blocks"><code>'.$out.'</code></pre>'; |
|
223 | - } |
|
222 | + return '<pre class="code-blocks"><code>'.$out.'</code></pre>'; |
|
223 | + } |
|
224 | 224 | |
225 | - /** |
|
226 | - * Sets the variables to be passed to all templates rendered |
|
227 | - * by this template helper. |
|
228 | - * |
|
229 | - * @param array $variables |
|
230 | - * |
|
231 | - * @return void |
|
232 | - */ |
|
233 | - public function setVariables(array $variables): void |
|
234 | - { |
|
235 | - $this->variables = $variables; |
|
236 | - } |
|
225 | + /** |
|
226 | + * Sets the variables to be passed to all templates rendered |
|
227 | + * by this template helper. |
|
228 | + * |
|
229 | + * @param array $variables |
|
230 | + * |
|
231 | + * @return void |
|
232 | + */ |
|
233 | + public function setVariables(array $variables): void |
|
234 | + { |
|
235 | + $this->variables = $variables; |
|
236 | + } |
|
237 | 237 | |
238 | - /** |
|
239 | - * Convert a string to a slug version of itself. |
|
240 | - * |
|
241 | - * @param string $original |
|
242 | - * |
|
243 | - * @return string |
|
244 | - */ |
|
245 | - public function slug($original): string |
|
246 | - { |
|
247 | - $slug = str_replace(" ", "-", $original); |
|
248 | - $slug = preg_replace('/[^\w\d\-\_]/i',' ', $slug); |
|
238 | + /** |
|
239 | + * Convert a string to a slug version of itself. |
|
240 | + * |
|
241 | + * @param string $original |
|
242 | + * |
|
243 | + * @return string |
|
244 | + */ |
|
245 | + public function slug($original): string |
|
246 | + { |
|
247 | + $slug = str_replace(" ", "-", $original); |
|
248 | + $slug = preg_replace('/[^\w\d\-\_]/i',' ', $slug); |
|
249 | 249 | |
250 | - return strtolower($slug); |
|
251 | - } |
|
250 | + return strtolower($slug); |
|
251 | + } |
|
252 | 252 | |
253 | - /** |
|
254 | - * Given an exception and status code will display the error to the client. |
|
255 | - * |
|
256 | - * @param string $template |
|
257 | - * |
|
258 | - * @return void |
|
259 | - */ |
|
260 | - public function render($template): void |
|
261 | - { |
|
262 | - $vars = $this->getVariables(); |
|
253 | + /** |
|
254 | + * Given an exception and status code will display the error to the client. |
|
255 | + * |
|
256 | + * @param string $template |
|
257 | + * |
|
258 | + * @return void |
|
259 | + */ |
|
260 | + public function render($template): void |
|
261 | + { |
|
262 | + $vars = $this->getVariables(); |
|
263 | 263 | |
264 | - $vars['template'] = $this; |
|
264 | + $vars['template'] = $this; |
|
265 | 265 | |
266 | - if ($this->system->getOutputBufferLevel() > $this->obLevel + 1) { |
|
267 | - @$this->system->endOutputBuffering(); |
|
268 | - } |
|
266 | + if ($this->system->getOutputBufferLevel() > $this->obLevel + 1) { |
|
267 | + @$this->system->endOutputBuffering(); |
|
268 | + } |
|
269 | 269 | |
270 | - // Instantiate the error view and prepare the vars |
|
271 | - call_user_func(function () { |
|
272 | - extract(func_get_arg(1)); |
|
273 | - include func_get_arg(0); |
|
274 | - }, $template, $vars); |
|
275 | - } |
|
270 | + // Instantiate the error view and prepare the vars |
|
271 | + call_user_func(function () { |
|
272 | + extract(func_get_arg(1)); |
|
273 | + include func_get_arg(0); |
|
274 | + }, $template, $vars); |
|
275 | + } |
|
276 | 276 | } |
277 | 277 | \ No newline at end of file |
@@ -105,10 +105,10 @@ discard block |
||
105 | 105 | if ($bytes < 1024) { |
106 | 106 | return $bytes.'B'; |
107 | 107 | } else if ($bytes < 1048576) { |
108 | - return round($bytes/1024, 2).'KB'; |
|
108 | + return round($bytes / 1024, 2).'KB'; |
|
109 | 109 | } |
110 | 110 | |
111 | - return round($bytes/1048576, 2).'MB'; |
|
111 | + return round($bytes / 1048576, 2).'MB'; |
|
112 | 112 | } |
113 | 113 | |
114 | 114 | /** |
@@ -179,7 +179,7 @@ discard block |
||
179 | 179 | } |
180 | 180 | |
181 | 181 | $origin = str_replace(["\r\n", "\r"], "\n", $origin); |
182 | - $origin = explode("\n", highlight_string($origin , true)); |
|
182 | + $origin = explode("\n", highlight_string($origin, true)); |
|
183 | 183 | $origin = str_replace('<br />', "\n", $origin [1]); |
184 | 184 | |
185 | 185 | $origin = explode("\n", str_replace("\r\n", "\n", $origin)); |
@@ -189,7 +189,7 @@ discard block |
||
189 | 189 | $start = $start < 0 ? 0 : $start; |
190 | 190 | |
191 | 191 | // Get just the lines we need to display, while keeping line numbers... |
192 | - $origin = array_splice($origin, $start, $lines, true); |
|
192 | + $origin = array_splice($origin, $start, $lines, true); |
|
193 | 193 | |
194 | 194 | // Used to format the line number in the source |
195 | 195 | $format = '% '.strlen($start + $lines).'d'; |
@@ -205,7 +205,7 @@ discard block |
||
205 | 205 | $spans += substr_count($row, '<span') - substr_count($row, '</span'); |
206 | 206 | $row = str_replace(["\r", "\n"], ['', ''], $row); |
207 | 207 | |
208 | - if (($n+$start+1) == $lineNumber) { |
|
208 | + if (($n + $start + 1) == $lineNumber) { |
|
209 | 209 | preg_match_all('#<[^>]+>#', $row, $tags); |
210 | 210 | $out .= sprintf("<span class='line highlight'><span class='number'>{$format}</span> %s\n</span>%s", |
211 | 211 | $n + $start + 1, |
@@ -213,7 +213,7 @@ discard block |
||
213 | 213 | implode('', $tags[0]) |
214 | 214 | ); |
215 | 215 | } else { |
216 | - $out .= sprintf('<span class="number">'.$format.'</span> %s <span class="line">', $n + $start + 1, $row) ."\n"; |
|
216 | + $out .= sprintf('<span class="number">'.$format.'</span> %s <span class="line">', $n + $start + 1, $row)."\n"; |
|
217 | 217 | } |
218 | 218 | } |
219 | 219 | |
@@ -245,7 +245,7 @@ discard block |
||
245 | 245 | public function slug($original): string |
246 | 246 | { |
247 | 247 | $slug = str_replace(" ", "-", $original); |
248 | - $slug = preg_replace('/[^\w\d\-\_]/i',' ', $slug); |
|
248 | + $slug = preg_replace('/[^\w\d\-\_]/i', ' ', $slug); |
|
249 | 249 | |
250 | 250 | return strtolower($slug); |
251 | 251 | } |
@@ -268,7 +268,7 @@ discard block |
||
268 | 268 | } |
269 | 269 | |
270 | 270 | // Instantiate the error view and prepare the vars |
271 | - call_user_func(function () { |
|
271 | + call_user_func(function() { |
|
272 | 272 | extract(func_get_arg(1)); |
273 | 273 | include func_get_arg(0); |
274 | 274 | }, $template, $vars); |