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