1 | <?php |
||
2 | declare(strict_types=1); |
||
3 | |||
4 | namespace Nip\Dispatcher; |
||
5 | |||
6 | use Exception; |
||
7 | use Nip\Container\Container; |
||
8 | use Nip\Container\ContainerAwareTrait; |
||
9 | use Nip\Dispatcher\Commands\Command; |
||
10 | use Nip\Dispatcher\Commands\CommandFactory; |
||
11 | use Nip\Dispatcher\Configuration\HasConfigurationTrait; |
||
12 | use Nip\Dispatcher\Exceptions\ForwardException; |
||
13 | use Nip\Dispatcher\Resolver\ClassResolver\NameGenerator; |
||
14 | use Nip\Dispatcher\Resolver\HasResolverPipelineTrait; |
||
15 | use Nip\Dispatcher\Resolver\Pipeline\InstanceBuilder; |
||
16 | use Nip\Dispatcher\Traits\HasCommandsCollection; |
||
17 | use Nip\Dispatcher\Traits\HasRequestTrait; |
||
18 | use Nip\Http\Request; |
||
19 | use Psr\Http\Message\ResponseInterface; |
||
20 | use Symfony\Component\HttpFoundation\Response; |
||
21 | |||
22 | /** |
||
23 | * Class Dispatcher. |
||
24 | */ |
||
25 | class Dispatcher |
||
26 | { |
||
27 | use ContainerAwareTrait; |
||
28 | use HasConfigurationTrait; |
||
29 | use HasResolverPipelineTrait; |
||
30 | use HasRequestTrait; |
||
31 | use HasCommandsCollection; |
||
32 | |||
33 | /** |
||
34 | * Create a new controller dispatcher instance. |
||
35 | * |
||
36 | 4 | * @param Container $container |
|
37 | * |
||
38 | 4 | * |
|
39 | * @return void |
||
40 | */ |
||
41 | 4 | public function __construct(Container $container = null) |
|
42 | { |
||
43 | if ($container instanceof Container) { |
||
44 | $this->setContainer($container); |
||
45 | } |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * @param Request|null $request |
||
50 | * @return ResponseInterface |
||
51 | * @throws Exception |
||
52 | * |
||
53 | * @return null|Response |
||
54 | */ |
||
55 | public function dispatch(Request $request = null) |
||
56 | { |
||
57 | NameGenerator::instance()->addNamespaces($this->getConfiguration()->getNamespaces()); |
||
58 | if ($request) { |
||
59 | $this->setRequest($request); |
||
60 | } |
||
61 | |||
62 | $command = CommandFactory::createFromRequest($request); |
||
63 | return $this->dispatchCommand($command)->getReturn(); |
||
64 | } |
||
65 | |||
66 | |||
67 | /** |
||
68 | * @param $action |
||
69 | * @param array $params |
||
70 | * @return mixed |
||
71 | * @throws Exception |
||
72 | */ |
||
73 | public function call($action, $params = []) |
||
74 | { |
||
75 | $action = is_array($action) ? $action : ['controller' => $action]; |
||
76 | $command = CommandFactory::createFromAction($action); |
||
77 | if (isset($params['_request'])) { |
||
78 | $command->setRequest($params['_request']); |
||
79 | unset($params['_request']); |
||
80 | } |
||
81 | $command->setActionParam('params', $params); |
||
82 | return $this->getResolverPipeline(InstanceBuilder::class) |
||
83 | ->process($command) |
||
84 | ->getReturn(); |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * @param Request|null $request |
||
89 | * @param array $params |
||
90 | * @return |
||
91 | * @throws Exception |
||
92 | */ |
||
93 | public function callFromRequest(Request $request = null, $params = []) |
||
94 | { |
||
95 | $command = CommandFactory::createFromRequest($request); |
||
96 | $command->setActionParam('params', $params); |
||
97 | return $this->getResolverPipeline(InstanceBuilder::class) |
||
98 | ->process($command) |
||
99 | ->getReturn(); |
||
100 | 3 | } |
|
101 | |||
102 | 3 | /** |
|
103 | * @param Command $command |
||
104 | * @return Command |
||
105 | */ |
||
106 | 3 | public function dispatchCommand(Command $command) |
|
107 | 1 | { |
|
108 | $this->getCommandsCollection()[] = $command; |
||
109 | |||
110 | |||
111 | try { |
||
112 | return $this->processCommand($command); |
||
113 | } catch (ForwardException $exception) { |
||
114 | $command = CommandFactory::createFromForwardException($exception); |
||
115 | $command->setRequest($this->getRequest()); |
||
116 | return $this->dispatchCommand($command); |
||
117 | } |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * @param bool $params |
||
122 | * @throws ForwardException |
||
123 | */ |
||
124 | public function throwError($params = false) |
||
0 ignored issues
–
show
|
|||
125 | { |
||
126 | // $this->getFrontController()->getTrace()->add($params); |
||
127 | $this->setErrorController(); |
||
128 | $this->forward('index'); |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * @return $this |
||
133 | */ |
||
134 | public function setErrorController() |
||
135 | { |
||
136 | $this->getRequest()->setActionName('index'); |
||
137 | $this->getRequest()->setControllerName('error'); |
||
138 | $this->getRequest()->setModuleName('default'); |
||
139 | |||
140 | return $this; |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * @param bool $action |
||
145 | * @param bool $controller |
||
146 | * @param bool $module |
||
147 | * @param array $params |
||
148 | * |
||
149 | * @throws ForwardException |
||
150 | */ |
||
151 | public function forward($action = false, $controller = false, $module = false, $params = []) |
||
152 | { |
||
153 | $this->getRequest()->setActionName($action); |
||
154 | |||
155 | if ($controller) { |
||
156 | $this->getRequest()->setControllerName($controller); |
||
157 | } |
||
158 | if ($module) { |
||
159 | $this->getRequest()->setModuleName($module); |
||
160 | } |
||
161 | |||
162 | if (is_array($params)) { |
||
163 | $this->getRequest()->attributes->add($params); |
||
164 | } |
||
165 | |||
166 | throw new ForwardException(); |
||
167 | } |
||
168 | } |
||
169 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.