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
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class Dispatcher |
19
|
|
|
* @package Nip\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
|
|
|
* @return void |
|
|
|
|
33
|
|
|
*/ |
34
|
4 |
|
public function __construct(Container $container = null) |
35
|
|
|
{ |
36
|
4 |
|
if ($container instanceof Container) { |
37
|
|
|
$this->setContainer($container); |
38
|
|
|
} |
39
|
4 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param Request|null $request |
43
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
44
|
|
|
* @throws Exception |
45
|
|
|
*/ |
46
|
|
|
public function dispatch(Request $request = null) |
47
|
|
|
{ |
48
|
|
|
if ($request) { |
49
|
|
|
$this->setRequest($request); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$command = CommandFactory::createFromRequest($request); |
53
|
|
|
return $this->dispatchCommand($command)->getReturn(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param $action |
59
|
|
|
* @param array $params |
60
|
|
|
* @return |
61
|
|
|
*/ |
62
|
|
|
public function call($action, $params = []) |
63
|
|
|
{ |
64
|
|
|
$action = is_array($action) ? $action : ['controller' => $action]; |
65
|
|
|
$action['params'] = $params; |
66
|
|
|
$command = CommandFactory::createFromAction($action); |
67
|
|
|
return $this->getResolverPipeline(InstanceBuilder::class) |
68
|
|
|
->process($command) |
69
|
|
|
->getReturn(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param Request|null $request |
74
|
|
|
* @return |
75
|
|
|
*/ |
76
|
|
|
public function callFromRequest(Request $request = null) |
77
|
|
|
{ |
78
|
|
|
$command = CommandFactory::createFromRequest($request); |
79
|
|
|
return $this->getResolverPipeline(InstanceBuilder::class) |
80
|
|
|
->process($command) |
81
|
|
|
->getReturn(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param Command $command |
86
|
|
|
* @return Command |
87
|
|
|
*/ |
88
|
3 |
|
public function dispatchCommand(Command $command) |
89
|
|
|
{ |
90
|
3 |
|
$this->getCommandsCollection()[] = $command; |
91
|
|
|
|
92
|
|
|
|
93
|
|
|
try { |
94
|
3 |
|
return $this->processCommand($command); |
95
|
1 |
|
} catch (ForwardException $exception) { |
96
|
|
|
$command = CommandFactory::createFromForwardExecption($exception); |
97
|
|
|
$return = $this->dispatchCommand($command); |
98
|
|
|
|
99
|
|
|
return $return; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param bool $params |
105
|
|
|
*/ |
106
|
|
|
public function throwError($params = false) |
|
|
|
|
107
|
|
|
{ |
108
|
|
|
// $this->getFrontController()->getTrace()->add($params); |
|
|
|
|
109
|
|
|
$this->setErrorController(); |
110
|
|
|
$this->forward('index'); |
|
|
|
|
111
|
|
|
|
112
|
|
|
return; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @return $this |
117
|
|
|
*/ |
118
|
|
|
public function setErrorController() |
119
|
|
|
{ |
120
|
|
|
$this->getRequest()->setActionName('index'); |
121
|
|
|
$this->getRequest()->setControllerName('error'); |
122
|
|
|
$this->getRequest()->setModuleName('default'); |
123
|
|
|
|
124
|
|
|
return $this; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param bool $action |
129
|
|
|
* @param bool $controller |
130
|
|
|
* @param bool $module |
131
|
|
|
* @param array $params |
132
|
|
|
* @throws ForwardException |
133
|
|
|
*/ |
134
|
|
|
public function forward($action = false, $controller = false, $module = false, $params = []) |
135
|
|
|
{ |
136
|
|
|
$this->getRequest()->setActionName($action); |
|
|
|
|
137
|
|
|
|
138
|
|
|
if ($controller) { |
139
|
|
|
$this->getRequest()->setControllerName($controller); |
|
|
|
|
140
|
|
|
} |
141
|
|
|
if ($module) { |
142
|
|
|
$this->getRequest()->setModuleName($module); |
|
|
|
|
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
if (is_array($params)) { |
146
|
|
|
$this->getRequest()->attributes->add($params); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
throw new ForwardException; |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.