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\Element; |
15
|
|
|
|
16
|
|
|
use Romm\ConfigurationObject\Service\Items\DataPreProcessor\DataPreProcessor; |
17
|
|
|
use Romm\ConfigurationObject\Service\Items\DataPreProcessor\DataPreProcessorInterface; |
18
|
|
|
use Romm\Formz\Exceptions\InvalidArgumentValueException; |
19
|
|
|
use Romm\Formz\Exceptions\InvalidEntryException; |
20
|
|
|
use Romm\Formz\Exceptions\MissingArgumentException; |
21
|
|
|
use Romm\Formz\Exceptions\SignalNotFoundException; |
22
|
|
|
use Romm\Formz\Form\FormObject\FormObject; |
23
|
|
|
use Romm\Formz\Middleware\Element\MiddlewareInterface; |
24
|
|
|
use Romm\Formz\Middleware\MiddlewareFactory; |
25
|
|
|
use Romm\Formz\Middleware\Option\OptionDefinitionInterface; |
26
|
|
|
use Romm\Formz\Middleware\Processor\MiddlewareProcessor; |
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\MiddlewareSignalInterface; |
32
|
|
|
use Romm\Formz\Middleware\Signal\SendsMiddlewareSignal; |
33
|
|
|
use Romm\Formz\Middleware\Signal\SignalObject; |
34
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
35
|
|
|
use TYPO3\CMS\Extbase\Mvc\Controller\Arguments; |
36
|
|
|
use TYPO3\CMS\Extbase\Mvc\Web\Request; |
37
|
|
|
use TYPO3\CMS\Extbase\Reflection\ReflectionService; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Abstract class that must be extended by middlewares. |
41
|
|
|
* |
42
|
|
|
* Child middleware must implement their own signals. |
43
|
|
|
*/ |
44
|
|
|
abstract class AbstractMiddleware implements MiddlewareInterface, DataPreProcessorInterface |
45
|
|
|
{ |
46
|
|
|
/** |
47
|
|
|
* @var MiddlewareProcessor |
48
|
|
|
*/ |
49
|
|
|
private $processor; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* This is the default option class, this property can be overridden in |
53
|
|
|
* children classes to be mapped to another option definition. |
54
|
|
|
* |
55
|
|
|
* @var \Romm\Formz\Middleware\Option\DefaultOptionDefinition |
56
|
|
|
*/ |
57
|
|
|
protected $options; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Can be overridden in child class with custom priority value. |
61
|
|
|
* |
62
|
|
|
* The higher the priority is, the earlier the middleware is called. |
63
|
|
|
* |
64
|
|
|
* Note that you can also override the method `getPriority()` for advanced |
65
|
|
|
* priority calculation. |
66
|
|
|
* |
67
|
|
|
* @var int |
68
|
|
|
*/ |
69
|
|
|
protected $priority = 0; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @var ReflectionService |
73
|
|
|
*/ |
74
|
|
|
protected $reflectionService; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param OptionDefinitionInterface $options |
78
|
|
|
*/ |
79
|
|
|
final public function __construct(OptionDefinitionInterface $options) |
80
|
|
|
{ |
81
|
|
|
$this->options = $options; |
|
|
|
|
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Abstraction for processing the middleware initialization. |
86
|
|
|
* |
87
|
|
|
* For own initialization, @see initializeMiddleware() |
88
|
|
|
*/ |
89
|
|
|
final public function initialize() |
90
|
|
|
{ |
91
|
|
|
$this->initializeMiddleware(); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* You can override this method in your child class to initialize your |
96
|
|
|
* middleware correctly. |
97
|
|
|
*/ |
98
|
|
|
protected function initializeMiddleware() |
99
|
|
|
{ |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @see \Romm\Formz\Middleware\Signal\SendsMiddlewareSignal::beforeSignal() |
104
|
|
|
* |
105
|
|
|
* @param string $signal |
106
|
|
|
* @return SignalObject |
107
|
|
|
*/ |
108
|
|
|
final public function beforeSignal($signal = null) |
109
|
|
|
{ |
110
|
|
|
return $this->getSignalObject($signal, Before::class); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @see \Romm\Formz\Middleware\Signal\SendsMiddlewareSignal::afterSignal() |
115
|
|
|
* |
116
|
|
|
* @param string $signal |
117
|
|
|
* @return SignalObject |
118
|
|
|
*/ |
119
|
|
|
final public function afterSignal($signal = null) |
120
|
|
|
{ |
121
|
|
|
return $this->getSignalObject($signal, After::class); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @return OptionDefinitionInterface |
126
|
|
|
*/ |
127
|
|
|
public function getOptions() |
128
|
|
|
{ |
129
|
|
|
return $this->options; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @return string |
134
|
|
|
*/ |
135
|
|
|
public static function getOptionsClassName() |
136
|
|
|
{ |
137
|
|
|
return MiddlewareFactory::get()->getOptionsClassNameFromProperty(self::class); |
|
|
|
|
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Returns a new forward dispatcher, on which you can add options by calling |
142
|
|
|
* its fluent methods. |
143
|
|
|
* |
144
|
|
|
* You must call the method `dispatch()` to actually dispatch the forward |
145
|
|
|
* signal. |
146
|
|
|
* |
147
|
|
|
* @return Forward |
148
|
|
|
*/ |
149
|
|
|
final protected function forward() |
150
|
|
|
{ |
151
|
|
|
return new Forward($this->getRequest()); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Returns a new redirect dispatcher, on which you can add options by |
156
|
|
|
* calling its fluent methods. |
157
|
|
|
* |
158
|
|
|
* You must call the method `dispatch()` to actually dispatch the redirect |
159
|
|
|
* signal. |
160
|
|
|
* |
161
|
|
|
* @return Redirect |
162
|
|
|
*/ |
163
|
|
|
final protected function redirect() |
164
|
|
|
{ |
165
|
|
|
return new Redirect($this->getRequest()); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @return FormObject |
170
|
|
|
*/ |
171
|
|
|
final protected function getFormObject() |
172
|
|
|
{ |
173
|
|
|
return $this->processor->getFormObject(); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @return Request |
178
|
|
|
*/ |
179
|
|
|
final protected function getRequest() |
180
|
|
|
{ |
181
|
|
|
return $this->processor->getRequest(); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @return Arguments |
186
|
|
|
*/ |
187
|
|
|
final protected function getRequestArguments() |
188
|
|
|
{ |
189
|
|
|
return $this->processor->getRequestArguments(); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @return int |
194
|
|
|
*/ |
195
|
|
|
public function getPriority() |
196
|
|
|
{ |
197
|
|
|
return (int)$this->priority; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @param MiddlewareProcessor $middlewareProcessor |
202
|
|
|
*/ |
203
|
|
|
final public function bindMiddlewareProcessor(MiddlewareProcessor $middlewareProcessor) |
204
|
|
|
{ |
205
|
|
|
$this->processor = $middlewareProcessor; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Returns the name of the signal on which this middleware is bound. |
210
|
|
|
* |
211
|
|
|
* @return string |
212
|
|
|
* @throws SignalNotFoundException |
213
|
|
|
*/ |
214
|
|
|
final public function getBoundSignalName() |
215
|
|
|
{ |
216
|
|
|
$interfaces = class_implements($this); |
217
|
|
|
|
218
|
|
|
foreach ($interfaces as $interface) { |
219
|
|
|
if (in_array(MiddlewareSignalInterface::class, class_implements($interface))) { |
220
|
|
|
return $interface; |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
throw SignalNotFoundException::signalNotFoundInMiddleware($this); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Will inject empty options if no option has been defined at all. |
229
|
|
|
* |
230
|
|
|
* @param DataPreProcessor $processor |
231
|
|
|
*/ |
232
|
|
|
public static function dataPreProcessor(DataPreProcessor $processor) |
233
|
|
|
{ |
234
|
|
|
$data = $processor->getData(); |
235
|
|
|
|
236
|
|
|
if (false === isset($data['options'])) { |
237
|
|
|
$data['options'] = []; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
$processor->setData($data); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Returns a signal object, that will be used to dispatch a signal coming |
245
|
|
|
* from this middleware. |
246
|
|
|
* |
247
|
|
|
* @param string $signal |
248
|
|
|
* @param string $type |
249
|
|
|
* @return SignalObject |
250
|
|
|
* @throws InvalidArgumentValueException |
251
|
|
|
* @throws InvalidEntryException |
252
|
|
|
* @throws MissingArgumentException |
253
|
|
|
*/ |
254
|
|
|
private function getSignalObject($signal, $type) |
255
|
|
|
{ |
256
|
|
|
if (false === $this instanceof SendsMiddlewareSignal) { |
257
|
|
|
throw InvalidEntryException::middlewareNotSendingSignals($this); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** @var SendsMiddlewareSignal $this */ |
261
|
|
|
if (null === $signal) { |
262
|
|
|
if (count($this->getAllowedSignals()) > 1) { |
|
|
|
|
263
|
|
|
throw MissingArgumentException::signalNameArgumentMissing($this); |
|
|
|
|
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
$signal = reset($this->getAllowedSignals()); |
|
|
|
|
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
if (false === in_array($signal, $this->getAllowedSignals())) { |
|
|
|
|
270
|
|
|
throw InvalidArgumentValueException::signalNotAllowed($this); |
|
|
|
|
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** @var SignalObject $signalObject */ |
274
|
|
|
$signalObject = GeneralUtility::makeInstance(SignalObject::class, $this->processor, $signal, $type); |
275
|
|
|
|
276
|
|
|
return $signalObject; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* @return array |
281
|
|
|
*/ |
282
|
|
|
public function __sleep() |
283
|
|
|
{ |
284
|
|
|
return ['options']; |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* @param ReflectionService $reflectionService |
289
|
|
|
*/ |
290
|
|
|
public function injectReflectionService(ReflectionService $reflectionService) |
291
|
|
|
{ |
292
|
|
|
$this->reflectionService = $reflectionService; |
293
|
|
|
} |
294
|
|
|
} |
295
|
|
|
|
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.