|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright 2014 Simon Mönch. |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace APL\Dispatcher; |
|
12
|
|
|
|
|
13
|
|
|
use APL\Event; |
|
14
|
|
|
use APL\Exception; |
|
15
|
|
|
use APL\Command\CommandInterface; |
|
16
|
|
|
use APL\Response\ResponseInterface; |
|
17
|
|
|
use APL\UseCase\UseCaseInterface; |
|
18
|
|
|
use APL\UseCase\UseCaseCollectionInterface; |
|
19
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* |
|
23
|
|
|
* @author Simon Mönch |
|
24
|
|
|
* @author David Badura <[email protected]> |
|
25
|
|
|
*/ |
|
26
|
|
|
class Dispatcher implements DispatcherInterface |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* |
|
30
|
|
|
* @var $eventDispatcherInterface |
|
31
|
|
|
*/ |
|
32
|
|
|
private $eventDispatcher; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* |
|
36
|
|
|
* @var UseCaseInterface[] |
|
37
|
|
|
*/ |
|
38
|
|
|
private $useCases = array(); |
|
39
|
|
|
|
|
40
|
|
|
/** @var CommandStack */ |
|
41
|
|
|
private $commandStack; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* |
|
45
|
|
|
* @param EventDispatcherInterface $eventDispatcher |
|
46
|
|
|
* @param CommandStack $commandStack |
|
47
|
|
|
*/ |
|
48
|
|
|
public function __construct(EventDispatcherInterface $eventDispatcher, CommandStack $commandStack = null) |
|
49
|
|
|
{ |
|
50
|
|
|
$this->eventDispatcher = $eventDispatcher; |
|
51
|
|
|
$this->commandStack = $commandStack ?: new CommandStack(); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* |
|
56
|
|
|
* @param string $commandClass |
|
57
|
|
|
* @param UseCaseInterface $useCase |
|
58
|
|
|
*/ |
|
59
|
|
|
public function registerCommand($commandClass, UseCaseInterface $useCase) |
|
60
|
|
|
{ |
|
61
|
|
|
if (isset($this->useCases[$commandClass])) { |
|
62
|
|
|
throw new Exception\DuplicateUseCaseException($this->useCases[$commandClass], $useCase, $commandClass); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
$this->useCases[$commandClass] = $useCase; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* |
|
70
|
|
|
* @param UseCaseCollectionInterface $useCase |
|
71
|
|
|
*/ |
|
72
|
|
|
public function registerUseCaseCollection(UseCaseCollectionInterface $useCase) |
|
73
|
|
|
{ |
|
74
|
|
|
foreach (array_keys($useCase->register()) as $class) { |
|
75
|
|
|
$this->registerCommand($class, $useCase); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* |
|
81
|
|
|
* @param CommandInterface $command |
|
82
|
|
|
* @return ResponseInterface |
|
83
|
|
|
* @throws \Exception |
|
84
|
|
|
*/ |
|
85
|
|
|
public function execute(CommandInterface $command) |
|
86
|
|
|
{ |
|
87
|
|
|
$this->commandStack->push($command); |
|
88
|
|
|
|
|
89
|
|
|
try { |
|
90
|
|
|
|
|
91
|
|
|
$command = $this->preCommand($command); |
|
92
|
|
|
$response = $this->doExecute($command); |
|
93
|
|
|
$response = $this->postCommand($command, $response); |
|
94
|
|
|
|
|
95
|
|
|
} catch (\Exception $exception) { |
|
96
|
|
|
|
|
97
|
|
|
$response = $this->exception($command, $exception); |
|
98
|
|
|
|
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
return $this->terminate($command, $response); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* |
|
106
|
|
|
* @param CommandInterface $command |
|
107
|
|
|
* @return ResponseInterface |
|
108
|
|
|
*/ |
|
109
|
|
|
protected function doExecute(CommandInterface $command) |
|
110
|
|
|
{ |
|
111
|
|
|
$useCase = $this->resolveUseCase($command); |
|
112
|
|
|
$response = $useCase->run($command); |
|
113
|
|
|
|
|
114
|
|
|
if (!$response instanceof ResponseInterface) { |
|
115
|
|
|
throw new Exception\InvalidResponseException($command, $response); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
return $response; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* |
|
123
|
|
|
* @param CommandInterface $command |
|
124
|
|
|
* @return CommandInterface |
|
125
|
|
|
*/ |
|
126
|
|
|
protected function preCommand(CommandInterface $command) |
|
127
|
|
|
{ |
|
128
|
|
|
$event = new Event\PreCommandEvent($command); |
|
129
|
|
|
$this->eventDispatcher->dispatch(Event\Events::PRE_COMMAND, $event); |
|
130
|
|
|
|
|
131
|
|
|
return $event->getCommand(); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* |
|
136
|
|
|
* @param CommandInterface $command |
|
137
|
|
|
* @param ResponseInterface $response |
|
138
|
|
|
* @return ResponseInterface |
|
139
|
|
|
*/ |
|
140
|
|
View Code Duplication |
protected function postCommand(CommandInterface $command, ResponseInterface $response) |
|
141
|
|
|
{ |
|
142
|
|
|
$event = new Event\PostCommandEvent($command, $response); |
|
143
|
|
|
$this->eventDispatcher->dispatch(Event\Events::POST_COMMAND, $event); |
|
144
|
|
|
|
|
145
|
|
|
return $event->getResponse(); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* |
|
150
|
|
|
* @param CommandInterface $command |
|
151
|
|
|
* @param \Exception $exception |
|
152
|
|
|
* @return ResponseInterface |
|
153
|
|
|
*/ |
|
154
|
|
|
protected function exception(CommandInterface $command, \Exception $exception) |
|
155
|
|
|
{ |
|
156
|
|
|
$event = new Event\ExceptionEvent($command, $exception); |
|
157
|
|
|
$this->eventDispatcher->dispatch(Event\Events::EXCEPTION, $event); |
|
158
|
|
|
|
|
159
|
|
|
if (!($response = $event->getResponse())) { |
|
160
|
|
|
throw $exception; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
return $response; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* |
|
168
|
|
|
* @param CommandInterface $command |
|
169
|
|
|
* @param ResponseInterface $response |
|
170
|
|
|
* @return ResponseInterface |
|
171
|
|
|
*/ |
|
172
|
|
View Code Duplication |
protected function terminate(CommandInterface $command, ResponseInterface $response) |
|
173
|
|
|
{ |
|
174
|
|
|
$event = new Event\TerminateEvent($command, $response); |
|
175
|
|
|
$this->eventDispatcher->dispatch(Event\Events::TERMINATE, $event); |
|
176
|
|
|
|
|
177
|
|
|
$this->commandStack->pop(); |
|
178
|
|
|
|
|
179
|
|
|
return $event->getResponse(); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* |
|
184
|
|
|
* @param CommandInterface $command |
|
185
|
|
|
* @return UseCaseInterface |
|
186
|
|
|
* @throws Exception\UseCaseNotFoundException |
|
187
|
|
|
*/ |
|
188
|
|
|
protected function resolveUseCase(CommandInterface $command) |
|
189
|
|
|
{ |
|
190
|
|
|
$class = get_class($command); |
|
191
|
|
|
|
|
192
|
|
|
if (!isset($this->useCases[$class])) { |
|
193
|
|
|
throw new Exception\UseCaseNotFoundException($command); |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
return $this->useCases[$class]; |
|
197
|
|
|
} |
|
198
|
|
|
} |
|
199
|
|
|
|