1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* 2017 Romain CANON <[email protected]> |
4
|
|
|
* |
5
|
|
|
* This file is part of the TYPO3 FormZ project. |
6
|
|
|
* It is free software; you can redistribute it and/or modify it |
7
|
|
|
* under the terms of the GNU General Public License, either |
8
|
|
|
* version 3 of the License, or any later version. |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, see: |
11
|
|
|
* http://www.gnu.org/licenses/gpl-3.0.html |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Romm\Formz\Middleware\Item; |
15
|
|
|
|
16
|
|
|
use Romm\ConfigurationObject\Service\Items\DataPreProcessor\DataPreProcessor; |
17
|
|
|
use Romm\ConfigurationObject\Service\Items\DataPreProcessor\DataPreProcessorInterface; |
18
|
|
|
use Romm\Formz\Error\FormResult; |
19
|
|
|
use Romm\Formz\Exceptions\InvalidArgumentValueException; |
20
|
|
|
use Romm\Formz\Exceptions\InvalidEntryException; |
21
|
|
|
use Romm\Formz\Exceptions\MissingArgumentException; |
22
|
|
|
use Romm\Formz\Exceptions\SignalNotFoundException; |
23
|
|
|
use Romm\Formz\Form\FormObject; |
24
|
|
|
use Romm\Formz\Middleware\Option\AbstractOptionDefinition; |
25
|
|
|
use Romm\Formz\Middleware\Request\Exception\StopPropagationException; |
26
|
|
|
use Romm\Formz\Middleware\MiddlewareInterface; |
27
|
|
|
use Romm\Formz\Middleware\Request\Forward; |
28
|
|
|
use Romm\Formz\Middleware\Request\Redirect; |
29
|
|
|
use Romm\Formz\Middleware\Signal\After; |
30
|
|
|
use Romm\Formz\Middleware\Signal\Before; |
31
|
|
|
use Romm\Formz\Middleware\Signal\MiddlewareSignal; |
32
|
|
|
use Romm\Formz\Middleware\Signal\SendsMiddlewareSignal; |
33
|
|
|
use Romm\Formz\Middleware\Signal\SignalObject; |
34
|
|
|
use Romm\Formz\Middleware\State\MiddlewareState; |
35
|
|
|
use TYPO3\CMS\Extbase\Mvc\Controller\Arguments; |
36
|
|
|
use TYPO3\CMS\Extbase\Mvc\Web\Request; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Abstract class that must be extended by middlewares. |
40
|
|
|
* |
41
|
|
|
* Child middleware must implement their own signals. |
42
|
|
|
*/ |
43
|
|
|
abstract class AbstractMiddleware implements MiddlewareInterface, DataPreProcessorInterface |
44
|
|
|
{ |
45
|
|
|
/** |
46
|
|
|
* @var MiddlewareState |
47
|
|
|
*/ |
48
|
|
|
private $state; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var \Romm\Formz\Middleware\Option\DefaultOptionDefinition |
52
|
|
|
*/ |
53
|
|
|
protected $options; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param AbstractOptionDefinition $options |
57
|
|
|
*/ |
58
|
|
|
final public function __construct(AbstractOptionDefinition $options) |
59
|
|
|
{ |
60
|
|
|
$this->options = $options; |
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Abstraction for processing the middleware initialization. |
65
|
|
|
* |
66
|
|
|
* For own initialization, @see initializeMiddleware() |
67
|
|
|
*/ |
68
|
|
|
final public function initialize() |
69
|
|
|
{ |
70
|
|
|
$this->initializeMiddleware(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* You can override this method in your child class to initialize your |
75
|
|
|
* middleware correctly. |
76
|
|
|
*/ |
77
|
|
|
protected function initializeMiddleware() |
78
|
|
|
{ |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @see \Romm\Formz\Middleware\Signal\SendsMiddlewareSignal::beforeSignal() |
83
|
|
|
* |
84
|
|
|
* @param string $signal |
85
|
|
|
* @return SignalObject |
86
|
|
|
*/ |
87
|
|
|
final public function beforeSignal($signal = null) |
88
|
|
|
{ |
89
|
|
|
return $this->getSignalObject($signal, Before::class); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @see \Romm\Formz\Middleware\Signal\SendsMiddlewareSignal::afterSignal() |
94
|
|
|
* |
95
|
|
|
* @param string $signal |
96
|
|
|
* @return SignalObject |
97
|
|
|
*/ |
98
|
|
|
final public function afterSignal($signal = null) |
99
|
|
|
{ |
100
|
|
|
return $this->getSignalObject($signal, After::class); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Returns a new forward dispatcher, on which you can add options by calling |
105
|
|
|
* its fluent methods. |
106
|
|
|
* |
107
|
|
|
* You must call the method `dispatch()` to actually dispatch the forward |
108
|
|
|
* signal. |
109
|
|
|
* |
110
|
|
|
* @return Forward |
111
|
|
|
*/ |
112
|
|
|
final protected function forward() |
113
|
|
|
{ |
114
|
|
|
return new Forward($this->getRequest()); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Returns a new redirect dispatcher, on which you can add options by |
119
|
|
|
* calling its fluent methods. |
120
|
|
|
* |
121
|
|
|
* You must call the method `dispatch()` to actually dispatch the redirect |
122
|
|
|
* signal. |
123
|
|
|
* |
124
|
|
|
* @return Redirect |
125
|
|
|
*/ |
126
|
|
|
final protected function redirect() { |
127
|
|
|
return new Redirect($this->getRequest()); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Will stop the propagation of middlewares: the next middlewares wont be |
132
|
|
|
* processed. |
133
|
|
|
* |
134
|
|
|
* Use with caution! |
135
|
|
|
*/ |
136
|
|
|
final protected function stopPropagation() |
137
|
|
|
{ |
138
|
|
|
throw new StopPropagationException; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @return FormObject |
143
|
|
|
*/ |
144
|
|
|
final protected function getFormObject() |
145
|
|
|
{ |
146
|
|
|
return $this->state->getFormObject(); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @return FormResult |
151
|
|
|
*/ |
152
|
|
|
final protected function getResult() |
153
|
|
|
{ |
154
|
|
|
return $this->state->getResult(); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @return Request |
159
|
|
|
*/ |
160
|
|
|
final protected function getRequest() |
161
|
|
|
{ |
162
|
|
|
return $this->state->getRequest(); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @return Arguments |
167
|
|
|
*/ |
168
|
|
|
final protected function getRequestArguments() |
169
|
|
|
{ |
170
|
|
|
return $this->state->getRequestArguments(); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @return array |
175
|
|
|
*/ |
176
|
|
|
final protected function getSettings() |
177
|
|
|
{ |
178
|
|
|
return $this->state->getSettings(); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @return int |
183
|
|
|
*/ |
184
|
|
|
public function getPriority() |
185
|
|
|
{ |
186
|
|
|
return 0; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @param MiddlewareState $middlewareState |
191
|
|
|
*/ |
192
|
|
|
final public function bindMiddlewareState(MiddlewareState $middlewareState) |
193
|
|
|
{ |
194
|
|
|
$this->state = $middlewareState; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Returns the name of the signal on which this middleware is bound. |
199
|
|
|
* |
200
|
|
|
* @return string |
201
|
|
|
* @throws SignalNotFoundException |
202
|
|
|
*/ |
203
|
|
|
final public function getBoundSignalName() |
204
|
|
|
{ |
205
|
|
|
$interfaces = class_implements($this); |
206
|
|
|
|
207
|
|
|
foreach ($interfaces as $interface) { |
208
|
|
|
if (in_array(MiddlewareSignal::class, class_implements($interface))) { |
209
|
|
|
return $interface; |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
throw SignalNotFoundException::signalNotFoundInMiddleware($this); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Will inject empty options if no option has been defined at all. |
218
|
|
|
* |
219
|
|
|
* @param DataPreProcessor $processor |
220
|
|
|
*/ |
221
|
|
|
public static function dataPreProcessor(DataPreProcessor $processor) |
222
|
|
|
{ |
223
|
|
|
$data = $processor->getData(); |
224
|
|
|
|
225
|
|
|
if (false === isset($data['options'])) { |
226
|
|
|
$data['options'] = []; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
$processor->setData($data); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* @param string $signal |
234
|
|
|
* @param string $type |
235
|
|
|
* @return SignalObject |
236
|
|
|
* @throws InvalidArgumentValueException |
237
|
|
|
* @throws InvalidEntryException |
238
|
|
|
* @throws MissingArgumentException |
239
|
|
|
*/ |
240
|
|
|
private function getSignalObject($signal, $type) |
241
|
|
|
{ |
242
|
|
|
if (false === $this instanceof SendsMiddlewareSignal) { |
243
|
|
|
throw InvalidEntryException::middlewareNotSendingSignals($this); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** @var SendsMiddlewareSignal $this */ |
247
|
|
|
if (null === $signal) { |
248
|
|
|
if (count($this->getAllowedSignals()) > 1) { |
|
|
|
|
249
|
|
|
throw MissingArgumentException::signalNameArgumentMissing($this); |
|
|
|
|
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
$signal = reset($this->getAllowedSignals()); |
|
|
|
|
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
if (false === in_array($signal, $this->getAllowedSignals())) { |
|
|
|
|
256
|
|
|
throw InvalidArgumentValueException::signalNotAllowed($this); |
|
|
|
|
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
return new SignalObject($this->state, $signal, $type); |
260
|
|
|
} |
261
|
|
|
} |
262
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.