Skip to content

Commit 9d3ff16

Browse files
committed
Extract route compiler.
1 parent b75aca6 commit 9d3ff16

6 files changed

Lines changed: 160 additions & 147 deletions

File tree

src/Illuminate/Routing/Route.php

Lines changed: 91 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
use Illuminate\Routing\Matching\MethodValidator;
1717
use Illuminate\Routing\Matching\SchemeValidator;
1818
use Illuminate\Http\Exception\HttpResponseException;
19-
use Symfony\Component\Routing\Route as SymfonyRoute;
2019

2120
class Route
2221
{
@@ -27,63 +26,63 @@ class Route
2726
*
2827
* @var string
2928
*/
30-
protected $uri;
29+
public $uri;
3130

3231
/**
3332
* The HTTP methods the route responds to.
3433
*
3534
* @var array
3635
*/
37-
protected $methods;
36+
public $methods;
3837

3938
/**
4039
* The route action array.
4140
*
4241
* @var array
4342
*/
44-
protected $action;
43+
public $action;
4544

4645
/**
4746
* The controller instance.
4847
*
4948
* @var mixed
5049
*/
51-
protected $controller;
50+
public $controller;
5251

5352
/**
5453
* The default values for the route.
5554
*
5655
* @var array
5756
*/
58-
protected $defaults = [];
57+
public $defaults = [];
5958

6059
/**
6160
* The regular expression requirements.
6261
*
6362
* @var array
6463
*/
65-
protected $wheres = [];
64+
public $wheres = [];
6665

6766
/**
6867
* The array of matched parameters.
6968
*
7069
* @var array
7170
*/
72-
protected $parameters;
71+
public $parameters;
7372

7473
/**
7574
* The parameter names for the route.
7675
*
7776
* @var array|null
7877
*/
79-
protected $parameterNames;
78+
public $parameterNames;
8079

8180
/**
8281
* The compiled version of the route.
8382
*
8483
* @var \Symfony\Component\Routing\CompiledRoute
8584
*/
86-
protected $compiled;
85+
public $compiled;
8786

8887
/**
8988
* The router instance used by the route.
@@ -166,13 +165,11 @@ protected function isControllerAction()
166165
*/
167166
protected function runCallable()
168167
{
169-
$parameters = $this->resolveMethodDependencies(
170-
$this->parametersWithoutNulls(), new ReflectionFunction($this->action['uses'])
171-
);
172-
173168
$callable = $this->action['uses'];
174169

175-
return $callable(...array_values($parameters));
170+
return $callable(...array_values($this->resolveMethodDependencies(
171+
$this->parametersWithoutNulls(), new ReflectionFunction($this->action['uses'])
172+
)));
176173
}
177174

178175
/**
@@ -246,101 +243,11 @@ public function matches(Request $request, $includingMethod = true)
246243
*/
247244
protected function compileRoute()
248245
{
249-
if ($this->compiled) {
250-
return;
251-
}
252-
253-
$optionals = $this->extractOptionalParameters();
254-
255-
$uri = preg_replace('/\{(\w+?)\?\}/', '{$1}', $this->uri);
256-
257-
$this->compiled = (
258-
new SymfonyRoute($uri, $optionals, $this->wheres, [], $this->domain() ?: '')
259-
)->compile();
260-
}
261-
262-
/**
263-
* Get the optional parameters for the route.
264-
*
265-
* @return array
266-
*/
267-
protected function extractOptionalParameters()
268-
{
269-
preg_match_all('/\{(\w+?)\?\}/', $this->uri, $matches);
270-
271-
return isset($matches[1]) ? array_fill_keys($matches[1], null) : [];
272-
}
273-
274-
/**
275-
* Get all middleware, including the ones from the controller.
276-
*
277-
* @return array
278-
*/
279-
public function gatherMiddleware()
280-
{
281-
return array_unique(array_merge($this->middleware(), $this->controllerMiddleware()), SORT_REGULAR);
282-
}
283-
284-
/**
285-
* Get or set the middlewares attached to the route.
286-
*
287-
* @param array|string|null $middleware
288-
* @return $this|array
289-
*/
290-
public function middleware($middleware = null)
291-
{
292-
if (is_null($middleware)) {
293-
return (array) Arr::get($this->action, 'middleware', []);
294-
}
295-
296-
if (is_string($middleware)) {
297-
$middleware = func_get_args();
298-
}
299-
300-
$this->action['middleware'] = array_merge(
301-
(array) Arr::get($this->action, 'middleware', []), $middleware
302-
);
303-
304-
return $this;
305-
}
306-
307-
/**
308-
* Get the middleware for the route's controller.
309-
*
310-
* @return array
311-
*/
312-
public function controllerMiddleware()
313-
{
314-
if (! $this->isControllerAction()) {
315-
return [];
246+
if (! $this->compiled) {
247+
$this->compiled = (new RouteCompiler($this))->compile();
316248
}
317249

318-
return ControllerDispatcher::getMiddleware(
319-
$this->getController(), $this->getControllerMethod()
320-
);
321-
}
322-
323-
/**
324-
* Get the parameters that are listed in the route / controller signature.
325-
*
326-
* @param string|null $subClass
327-
* @return array
328-
*/
329-
public function signatureParameters($subClass = null)
330-
{
331-
$action = $this->getAction();
332-
333-
if (is_string($action['uses'])) {
334-
list($class, $method) = explode('@', $action['uses']);
335-
336-
$parameters = (new ReflectionMethod($class, $method))->getParameters();
337-
} else {
338-
$parameters = (new ReflectionFunction($action['uses']))->getParameters();
339-
}
340-
341-
return is_null($subClass) ? $parameters : array_filter($parameters, function ($p) use ($subClass) {
342-
return $p->getClass() && $p->getClass()->isSubclassOf($subClass);
343-
});
250+
return $this->compiled;
344251
}
345252

346253
/**
@@ -368,18 +275,6 @@ public function hasParameter($name)
368275
return array_key_exists($name, $this->parameters());
369276
}
370277

371-
/**
372-
* Get a given parameter from the route.
373-
*
374-
* @param string $name
375-
* @param mixed $default
376-
* @return string|object
377-
*/
378-
public function getParameter($name, $default = null)
379-
{
380-
return $this->parameter($name, $default);
381-
}
382-
383278
/**
384279
* Get a given parameter from the route.
385280
*
@@ -475,6 +370,29 @@ protected function compileParameterNames()
475370
}, $matches[1]);
476371
}
477372

373+
/**
374+
* Get the parameters that are listed in the route / controller signature.
375+
*
376+
* @param string|null $subClass
377+
* @return array
378+
*/
379+
public function signatureParameters($subClass = null)
380+
{
381+
$action = $this->getAction();
382+
383+
if (is_string($action['uses'])) {
384+
list($class, $method) = explode('@', $action['uses']);
385+
386+
$parameters = (new ReflectionMethod($class, $method))->getParameters();
387+
} else {
388+
$parameters = (new ReflectionFunction($action['uses']))->getParameters();
389+
}
390+
391+
return is_null($subClass) ? $parameters : array_filter($parameters, function ($p) use ($subClass) {
392+
return $p->getClass() && $p->getClass()->isSubclassOf($subClass);
393+
});
394+
}
395+
478396
/**
479397
* Bind the route to a given request for execution.
480398
*
@@ -756,16 +674,6 @@ public function getPath()
756674
return $this->uri();
757675
}
758676

759-
/**
760-
* Get the URI associated with the route.
761-
*
762-
* @return string
763-
*/
764-
public function uri()
765-
{
766-
return $this->uri;
767-
}
768-
769677
/**
770678
* Get the HTTP verbs the route responds to.
771679
*
@@ -828,11 +736,11 @@ public function domain()
828736
}
829737

830738
/**
831-
* Get the URI that the route responds to.
739+
* Get the URI associated with the route.
832740
*
833741
* @return string
834742
*/
835-
public function getUri()
743+
public function uri()
836744
{
837745
return $this->uri;
838746
}
@@ -949,6 +857,57 @@ public function setAction(array $action)
949857
return $this;
950858
}
951859

860+
/**
861+
* Get all middleware, including the ones from the controller.
862+
*
863+
* @return array
864+
*/
865+
public function gatherMiddleware()
866+
{
867+
return array_unique(array_merge(
868+
$this->middleware(), $this->controllerMiddleware()
869+
), SORT_REGULAR);
870+
}
871+
872+
/**
873+
* Get or set the middlewares attached to the route.
874+
*
875+
* @param array|string|null $middleware
876+
* @return $this|array
877+
*/
878+
public function middleware($middleware = null)
879+
{
880+
if (is_null($middleware)) {
881+
return (array) Arr::get($this->action, 'middleware', []);
882+
}
883+
884+
if (is_string($middleware)) {
885+
$middleware = func_get_args();
886+
}
887+
888+
$this->action['middleware'] = array_merge(
889+
(array) Arr::get($this->action, 'middleware', []), $middleware
890+
);
891+
892+
return $this;
893+
}
894+
895+
/**
896+
* Get the middleware for the route's controller.
897+
*
898+
* @return array
899+
*/
900+
public function controllerMiddleware()
901+
{
902+
if (! $this->isControllerAction()) {
903+
return [];
904+
}
905+
906+
return ControllerDispatcher::getMiddleware(
907+
$this->getController(), $this->getControllerMethod()
908+
);
909+
}
910+
952911
/**
953912
* Get the compiled version of the route.
954913
*

src/Illuminate/Routing/RouteCollection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public function add(Route $route)
6464
*/
6565
protected function addToCollections($route)
6666
{
67-
$domainAndUri = $route->domain().$route->getUri();
67+
$domainAndUri = $route->domain().$route->uri();
6868

6969
foreach ($route->methods() as $method) {
7070
$this->routes[$method][$domainAndUri] = $route;

0 commit comments

Comments
 (0)