| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace Illuminate\Routing;
|
|
|
4 |
|
|
|
5 |
use Illuminate\Container\Container;
|
|
|
6 |
use Illuminate\Routing\Contracts\ControllerDispatcher as ControllerDispatcherContract;
|
|
|
7 |
|
|
|
8 |
class ControllerDispatcher implements ControllerDispatcherContract
|
|
|
9 |
{
|
|
|
10 |
use RouteDependencyResolverTrait;
|
|
|
11 |
|
|
|
12 |
/**
|
|
|
13 |
* The container instance.
|
|
|
14 |
*
|
|
|
15 |
* @var \Illuminate\Container\Container
|
|
|
16 |
*/
|
|
|
17 |
protected $container;
|
|
|
18 |
|
|
|
19 |
/**
|
|
|
20 |
* Create a new controller dispatcher instance.
|
|
|
21 |
*
|
|
|
22 |
* @param \Illuminate\Container\Container $container
|
|
|
23 |
* @return void
|
|
|
24 |
*/
|
|
|
25 |
public function __construct(Container $container)
|
|
|
26 |
{
|
|
|
27 |
$this->container = $container;
|
|
|
28 |
}
|
|
|
29 |
|
|
|
30 |
/**
|
|
|
31 |
* Dispatch a request to a given controller and method.
|
|
|
32 |
*
|
|
|
33 |
* @param \Illuminate\Routing\Route $route
|
|
|
34 |
* @param mixed $controller
|
|
|
35 |
* @param string $method
|
|
|
36 |
* @return mixed
|
|
|
37 |
*/
|
|
|
38 |
public function dispatch(Route $route, $controller, $method)
|
|
|
39 |
{
|
|
|
40 |
$parameters = $this->resolveParameters($route, $controller, $method);
|
|
|
41 |
|
|
|
42 |
if (method_exists($controller, 'callAction')) {
|
|
|
43 |
return $controller->callAction($method, $parameters);
|
|
|
44 |
}
|
|
|
45 |
|
|
|
46 |
return $controller->{$method}(...array_values($parameters));
|
|
|
47 |
}
|
|
|
48 |
|
|
|
49 |
/**
|
|
|
50 |
* Resolve the parameters for the controller.
|
|
|
51 |
*
|
|
|
52 |
* @param \Illuminate\Routing\Route $route
|
|
|
53 |
* @param mixed $controller
|
|
|
54 |
* @param string $method
|
|
|
55 |
* @return array
|
|
|
56 |
*/
|
|
|
57 |
protected function resolveParameters(Route $route, $controller, $method)
|
|
|
58 |
{
|
|
|
59 |
return $this->resolveClassMethodDependencies(
|
|
|
60 |
$route->parametersWithoutNulls(), $controller, $method
|
|
|
61 |
);
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
/**
|
|
|
65 |
* Get the middleware for the controller instance.
|
|
|
66 |
*
|
|
|
67 |
* @param \Illuminate\Routing\Controller $controller
|
|
|
68 |
* @param string $method
|
|
|
69 |
* @return array
|
|
|
70 |
*/
|
|
|
71 |
public function getMiddleware($controller, $method)
|
|
|
72 |
{
|
|
|
73 |
if (! method_exists($controller, 'getMiddleware')) {
|
|
|
74 |
return [];
|
|
|
75 |
}
|
|
|
76 |
|
|
|
77 |
return collect($controller->getMiddleware())->reject(function ($data) use ($method) {
|
|
|
78 |
return static::methodExcludedByOptions($method, $data['options']);
|
|
|
79 |
})->pluck('middleware')->all();
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
/**
|
|
|
83 |
* Determine if the given options exclude a particular method.
|
|
|
84 |
*
|
|
|
85 |
* @param string $method
|
|
|
86 |
* @param array $options
|
|
|
87 |
* @return bool
|
|
|
88 |
*/
|
|
|
89 |
public static function methodExcludedByOptions($method, array $options)
|
|
|
90 |
{
|
|
|
91 |
return (isset($options['only']) && ! in_array($method, (array) $options['only'])) ||
|
|
|
92 |
(! empty($options['except']) && in_array($method, (array) $options['except']));
|
|
|
93 |
}
|
|
|
94 |
}
|