1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nip\Dispatcher; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Nip\Container\Container; |
7
|
|
|
use Nip\Container\ContainerAwareTrait; |
8
|
|
|
use Nip\Dispatcher\Commands\Command; |
9
|
|
|
use Nip\Dispatcher\Commands\CommandFactory; |
10
|
|
|
use Nip\Dispatcher\Exceptions\ForwardException; |
11
|
|
|
use Nip\Dispatcher\Resolver\HasResolverPipelineTrait; |
12
|
|
|
use Nip\Dispatcher\Resolver\Pipeline\InstanceBuilder; |
13
|
|
|
use Nip\Dispatcher\Traits\HasCommandsCollection; |
14
|
|
|
use Nip\Dispatcher\Traits\HasRequestTrait; |
15
|
|
|
use Nip\Request; |
16
|
|
|
use Psr\Http\Message\ResponseInterface; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class Dispatcher. |
20
|
|
|
*/ |
21
|
|
|
class Dispatcher |
22
|
|
|
{ |
23
|
|
|
use ContainerAwareTrait; |
24
|
|
|
use HasResolverPipelineTrait; |
25
|
|
|
use HasRequestTrait; |
26
|
|
|
use HasCommandsCollection; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Create a new controller dispatcher instance. |
30
|
|
|
* |
31
|
|
|
* @param Container $container |
32
|
|
|
* |
33
|
|
|
* |
34
|
|
|
* @return void |
35
|
|
|
*/ |
36
|
4 |
|
public function __construct(Container $container = null) |
37
|
|
|
{ |
38
|
4 |
|
if ($container instanceof Container) { |
39
|
|
|
$this->setContainer($container); |
40
|
|
|
} |
41
|
4 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param Request|null $request |
45
|
|
|
* @return ResponseInterface |
46
|
|
|
* @throws Exception |
47
|
|
|
* |
48
|
|
|
* @return null|Response |
49
|
|
|
*/ |
50
|
|
|
public function dispatch(Request $request = null) |
51
|
|
|
{ |
52
|
|
|
if ($request) { |
53
|
|
|
$this->setRequest($request); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$command = CommandFactory::createFromRequest($request); |
|
|
|
|
57
|
|
|
return $this->dispatchCommand($command)->getReturn(); |
|
|
|
|
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param $action |
63
|
|
|
* @param array $params |
64
|
|
|
* @return mixed |
65
|
|
|
* @throws Exception |
66
|
|
|
*/ |
67
|
|
|
public function call($action, $params = []) |
68
|
|
|
{ |
69
|
|
|
$action = is_array($action) ? $action : ['controller' => $action]; |
70
|
|
|
$command = CommandFactory::createFromAction($action); |
71
|
|
|
if (isset($params['_request'])) { |
72
|
|
|
$command->setRequest($params['_request']); |
73
|
|
|
unset($params['_request']); |
74
|
|
|
} |
75
|
|
|
$command->setActionParam('params', $params); |
76
|
|
|
return $this->getResolverPipeline(InstanceBuilder::class) |
77
|
|
|
->process($command) |
78
|
|
|
->getReturn(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param Request|null $request |
83
|
|
|
* @param array $params |
84
|
|
|
* @return |
85
|
|
|
* @throws Exception |
86
|
|
|
*/ |
87
|
|
|
public function callFromRequest(Request $request = null, $params = []) |
88
|
|
|
{ |
89
|
|
|
$command = CommandFactory::createFromRequest($request); |
|
|
|
|
90
|
|
|
$command->setActionParam('params', $params); |
91
|
|
|
return $this->getResolverPipeline(InstanceBuilder::class) |
92
|
|
|
->process($command) |
93
|
|
|
->getReturn(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param Command $command |
98
|
|
|
* @return Command |
99
|
|
|
*/ |
100
|
3 |
|
public function dispatchCommand(Command $command) |
101
|
|
|
{ |
102
|
3 |
|
$this->getCommandsCollection()[] = $command; |
103
|
|
|
|
104
|
|
|
|
105
|
|
|
try { |
106
|
3 |
|
return $this->processCommand($command); |
107
|
1 |
|
} catch (ForwardException $exception) { |
108
|
|
|
$command = CommandFactory::createFromForwardException($exception); |
109
|
|
|
$command->setRequest($this->getRequest()); |
110
|
|
|
return $this->dispatchCommand($command); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param bool $params |
116
|
|
|
* @throws ForwardException |
117
|
|
|
*/ |
118
|
|
|
public function throwError($params = false) |
|
|
|
|
119
|
|
|
{ |
120
|
|
|
// $this->getFrontController()->getTrace()->add($params); |
121
|
|
|
$this->setErrorController(); |
122
|
|
|
$this->forward('index'); |
|
|
|
|
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @return $this |
127
|
|
|
*/ |
128
|
|
|
public function setErrorController() |
129
|
|
|
{ |
130
|
|
|
$this->getRequest()->setActionName('index'); |
131
|
|
|
$this->getRequest()->setControllerName('error'); |
132
|
|
|
$this->getRequest()->setModuleName('default'); |
133
|
|
|
|
134
|
|
|
return $this; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param bool $action |
139
|
|
|
* @param bool $controller |
140
|
|
|
* @param bool $module |
141
|
|
|
* @param array $params |
142
|
|
|
* |
143
|
|
|
* @throws ForwardException |
144
|
|
|
*/ |
145
|
|
|
public function forward($action = false, $controller = false, $module = false, $params = []) |
146
|
|
|
{ |
147
|
|
|
$this->getRequest()->setActionName($action); |
148
|
|
|
|
149
|
|
|
if ($controller) { |
150
|
|
|
$this->getRequest()->setControllerName($controller); |
|
|
|
|
151
|
|
|
} |
152
|
|
|
if ($module) { |
153
|
|
|
$this->getRequest()->setModuleName($module); |
|
|
|
|
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
if (is_array($params)) { |
|
|
|
|
157
|
|
|
$this->getRequest()->attributes->add($params); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
throw new ForwardException(); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @param $module |
165
|
|
|
* @param $controller |
166
|
|
|
* |
167
|
|
|
* @return string |
168
|
|
|
*/ |
169
|
|
|
protected function generateFullControllerNameNamespace($module, $controller) |
170
|
|
|
{ |
171
|
|
|
$name = app()->get('kernel')->getRootNamespace().'Modules\\'; |
|
|
|
|
172
|
|
|
$module = $module == 'Default' ? 'Frontend' : $module; |
173
|
|
|
$name .= $module.'\Controllers\\'; |
174
|
|
|
$name .= str_replace('_', '\\', $controller).'Controller'; |
175
|
|
|
|
176
|
|
|
return $name; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @param string $namespaceClass |
181
|
|
|
* |
182
|
|
|
* @return bool |
183
|
|
|
*/ |
184
|
|
|
protected function isValidControllerNamespace($namespaceClass) |
185
|
|
|
{ |
186
|
|
|
return class_exists($namespaceClass); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @return \Nip\AutoLoader\AutoLoader |
|
|
|
|
191
|
|
|
*/ |
192
|
|
|
protected function getAutoloader() |
193
|
|
|
{ |
194
|
|
|
return app('autoloader'); |
|
|
|
|
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @param $module |
199
|
|
|
* @param $controller |
200
|
|
|
* |
201
|
|
|
* @return string |
202
|
|
|
*/ |
203
|
|
|
protected function generateFullControllerNameString($module, $controller) |
204
|
|
|
{ |
205
|
|
|
return $module.'_'.$controller.'Controller'; |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|