|
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
|
|
|
* @param Request|null $request |
|
58
|
|
|
* @return |
|
59
|
|
|
*/ |
|
60
|
|
|
public function callFromRequest(Request $request = null) |
|
61
|
|
|
{ |
|
62
|
|
|
$command = CommandFactory::createFromRequest($request); |
|
63
|
|
|
return $this->getResolverPipeline(InstanceBuilder::class) |
|
64
|
|
|
->process($command) |
|
65
|
|
|
->getReturn(); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @param Command $command |
|
70
|
|
|
* @return Command |
|
71
|
|
|
*/ |
|
72
|
3 |
|
public function dispatchCommand(Command $command) |
|
73
|
|
|
{ |
|
74
|
3 |
|
$this->getCommandsCollection()[] = $command; |
|
75
|
|
|
|
|
76
|
|
|
|
|
77
|
|
|
try { |
|
78
|
3 |
|
return $this->processCommand($command); |
|
79
|
1 |
|
} catch (ForwardException $exception) { |
|
80
|
|
|
$command = CommandFactory::createFromForwardExecption($exception); |
|
81
|
|
|
$return = $this->dispatchCommand($command); |
|
82
|
|
|
|
|
83
|
|
|
return $return; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @param bool $params |
|
89
|
|
|
*/ |
|
90
|
|
|
public function throwError($params = false) |
|
|
|
|
|
|
91
|
|
|
{ |
|
92
|
|
|
// $this->getFrontController()->getTrace()->add($params); |
|
|
|
|
|
|
93
|
|
|
$this->setErrorController(); |
|
94
|
|
|
$this->forward('index'); |
|
|
|
|
|
|
95
|
|
|
|
|
96
|
|
|
return; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @return $this |
|
101
|
|
|
*/ |
|
102
|
|
|
public function setErrorController() |
|
103
|
|
|
{ |
|
104
|
|
|
$this->getRequest()->setActionName('index'); |
|
105
|
|
|
$this->getRequest()->setControllerName('error'); |
|
106
|
|
|
$this->getRequest()->setModuleName('default'); |
|
107
|
|
|
|
|
108
|
|
|
return $this; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @param bool $action |
|
113
|
|
|
* @param bool $controller |
|
114
|
|
|
* @param bool $module |
|
115
|
|
|
* @param array $params |
|
116
|
|
|
* @throws ForwardException |
|
117
|
|
|
*/ |
|
118
|
|
|
public function forward($action = false, $controller = false, $module = false, $params = []) |
|
119
|
|
|
{ |
|
120
|
|
|
$this->getRequest()->setActionName($action); |
|
|
|
|
|
|
121
|
|
|
|
|
122
|
|
|
if ($controller) { |
|
123
|
|
|
$this->getRequest()->setControllerName($controller); |
|
|
|
|
|
|
124
|
|
|
} |
|
125
|
|
|
if ($module) { |
|
126
|
|
|
$this->getRequest()->setModuleName($module); |
|
|
|
|
|
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
if (is_array($params)) { |
|
130
|
|
|
$this->getRequest()->attributes->add($params); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
throw new ForwardException; |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|
Adding a
@returnannotation 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.