1 | <?php |
||
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 public 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 public 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 | * Will stop the propagation of middlewares: the next middlewares wont be |
||
155 | * processed. |
||
156 | * |
||
157 | * Use with caution! |
||
158 | */ |
||
159 | final protected function stopPropagation() |
||
160 | { |
||
161 | throw new StopPropagationException; |
||
162 | } |
||
163 | |||
164 | /** |
||
165 | * @return FormObject |
||
166 | */ |
||
167 | final protected function getFormObject() |
||
168 | { |
||
169 | return $this->processor->getFormObject(); |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * @return Request |
||
174 | */ |
||
175 | final protected function getRequest() |
||
179 | |||
180 | /** |
||
181 | * @return Arguments |
||
182 | */ |
||
183 | final protected function getRequestArguments() |
||
187 | |||
188 | /** |
||
189 | * @return int |
||
190 | */ |
||
191 | public function getPriority() |
||
192 | { |
||
193 | return (int)$this->priority; |
||
194 | } |
||
195 | |||
196 | /** |
||
197 | * @param MiddlewareProcessor $middlewareProcessor |
||
198 | */ |
||
199 | final public function bindMiddlewareProcessor(MiddlewareProcessor $middlewareProcessor) |
||
203 | |||
204 | /** |
||
205 | * Returns the name of the signal on which this middleware is bound. |
||
206 | * |
||
207 | * @return string |
||
208 | * @throws SignalNotFoundException |
||
209 | */ |
||
210 | final public function getBoundSignalName() |
||
222 | |||
223 | /** |
||
224 | * Will inject empty options if no option has been defined at all. |
||
225 | * |
||
226 | * @param DataPreProcessor $processor |
||
227 | */ |
||
228 | public static function dataPreProcessor(DataPreProcessor $processor) |
||
238 | |||
239 | /** |
||
240 | * @param string $signal |
||
241 | * @param string $type |
||
242 | * @return SignalObject |
||
243 | * @throws InvalidArgumentValueException |
||
244 | * @throws InvalidEntryException |
||
245 | * @throws MissingArgumentException |
||
246 | */ |
||
247 | private function getSignalObject($signal, $type) |
||
268 | |||
269 | /** |
||
270 | * @return array |
||
271 | */ |
||
272 | public function __sleep() |
||
276 | } |
||
277 |
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.