1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Lenevor Framework |
5
|
|
|
* |
6
|
|
|
* LICENSE |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the new BSD license that is bundled |
9
|
|
|
* with this package in the file license.md. |
10
|
|
|
* It is also available through the world-wide-web at this URL: |
11
|
|
|
* https://lenevor.com/license |
12
|
|
|
* If you did not receive a copy of the license and are unable to |
13
|
|
|
* obtain it through the world-wide-web, please send an email |
14
|
|
|
* to [email protected] so we can send you a copy immediately. |
15
|
|
|
* |
16
|
|
|
* @package Lenevor |
17
|
|
|
* @subpackage Base |
18
|
|
|
* @link https://lenevor.com |
19
|
|
|
* @copyright Copyright (c) 2019 - 2021 Alexander Campo <[email protected]> |
20
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause New BSD license or see https://lenevor.com/license or see /license.md |
21
|
|
|
*/ |
22
|
|
|
|
23
|
|
|
namespace Syscodes\Controller; |
24
|
|
|
|
25
|
|
|
use Syscodes\Routing\Route; |
26
|
|
|
use Syscodes\Contracts\Container\Container; |
27
|
|
|
use Syscodes\Routing\Concerns\RouteDependencyResolver; |
28
|
|
|
use Syscodes\Controller\Contracts\ControllerDispatcher as ControllerDispatcherContract; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Dispatch requests when called a given controller and method. |
32
|
|
|
* |
33
|
|
|
* @author Alexander Campo <[email protected]> |
34
|
|
|
*/ |
35
|
|
|
class ControllerDispatcher implements ControllerDispatcherContract |
36
|
|
|
{ |
37
|
|
|
use RouteDependencyResolver; |
|
|
|
|
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* The container instance. |
41
|
|
|
* |
42
|
|
|
* @var \Syscodes\Contracts\Container\Container $container |
43
|
|
|
*/ |
44
|
|
|
protected $container; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Constructor. The ControllerDispatcher class instance. |
48
|
|
|
* |
49
|
|
|
* @param \Syscodes\Contracts\Container\Container $container |
50
|
|
|
* |
51
|
|
|
* @return void |
52
|
|
|
*/ |
53
|
|
|
public function __construct(Container $container) |
54
|
|
|
{ |
55
|
|
|
$this->container = $container; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Dispatch a request to a given controller and method. |
60
|
|
|
* |
61
|
|
|
* @param \Syscodes\Routing\Route $route |
62
|
|
|
* @param mixed $controller |
63
|
|
|
* @param string $method |
64
|
|
|
* |
65
|
|
|
* @return mixed |
66
|
|
|
*/ |
67
|
|
|
public function dispatch(Route $route, $controller, $method) |
68
|
|
|
{ |
69
|
|
|
$parameters = $this->resolveObjectMethodDependencies( |
70
|
|
|
$route->parametersWithouNulls(), $controller, $method |
71
|
|
|
); |
72
|
|
|
|
73
|
|
|
if (method_exists($controller, 'callAction')) { |
74
|
|
|
return $controller->callAction($method, $parameters); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $controller->{$method}(...array_values($parameters)); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Get the middleware for the controller instance. |
82
|
|
|
* |
83
|
|
|
* @param \Syscodes\Controller\Controller $controller |
84
|
|
|
* @param string $method |
85
|
|
|
* |
86
|
|
|
* @return array |
87
|
|
|
*/ |
88
|
|
|
public function getMiddleware($controller, $method) |
89
|
|
|
{ |
90
|
|
|
if ( ! method_exists($controller, 'getMiddleware')) |
91
|
|
|
{ |
92
|
|
|
return []; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$middleware = $controller->getMiddleware(); |
96
|
|
|
|
97
|
|
|
return collect($middleware)->filter(function ($data) use ($method) { |
98
|
|
|
return static::methodExcludedByOptions($method, $data['options']); |
|
|
|
|
99
|
|
|
})->all(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Determine if the given options exclude a particular method. |
104
|
|
|
* |
105
|
|
|
* @param string $method |
106
|
|
|
* @param array $options |
107
|
|
|
* |
108
|
|
|
* @return bool |
109
|
|
|
*/ |
110
|
|
|
protected function methodExcludedByOptions($method, array $options) |
111
|
|
|
{ |
112
|
|
|
return (isset($options['only']) && ! in_array($method, (array) $options['only'])) || |
113
|
|
|
( ! empty($options['except']) && in_array($method, (array) $options['except'])); |
114
|
|
|
} |
115
|
|
|
} |