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; |
15
|
|
|
|
16
|
|
|
use Romm\Formz\Exceptions\ExecutionDoneException; |
17
|
|
|
|
18
|
|
|
abstract class AbstractMiddleware implements MiddlewareInterface |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var MiddlewareInterface |
22
|
|
|
*/ |
23
|
|
|
private $nextMiddleware; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var bool |
27
|
|
|
*/ |
28
|
|
|
private $wasExecuted = false; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param MiddlewareInterface $nextMiddleware |
32
|
|
|
*/ |
33
|
|
|
final public function __construct(MiddlewareInterface $nextMiddleware) |
34
|
|
|
{ |
35
|
|
|
$this->nextMiddleware = $nextMiddleware; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @todo |
40
|
|
|
*/ |
41
|
|
|
final protected function initialize() |
42
|
|
|
{ |
43
|
|
|
// @todo add options handling |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Executes the life-workflow of the middleware: first it is initialized, |
48
|
|
|
* then processed, and at the end the next middleware is executed if it was |
49
|
|
|
* not done previously. |
50
|
|
|
*/ |
51
|
|
|
final public function execute() |
52
|
|
|
{ |
53
|
|
|
if (true === $this->wasExecuted) { |
54
|
|
|
throw ExecutionDoneException::middlewareAlreadyExecuted($this); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$this->initialize(); |
58
|
|
|
$this->process(); |
59
|
|
|
$this->wasExecuted = true; |
60
|
|
|
|
61
|
|
|
if (false === $this->nextMiddleware->wasExecuted()) { |
62
|
|
|
$this->nextMiddleware->execute(); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Wrapper used by this abstract class to give more flexibility to the |
68
|
|
|
* function `execute`. |
69
|
|
|
* |
70
|
|
|
* Implement it in own child classes to execute the main logic of the |
71
|
|
|
* middleware. |
72
|
|
|
* |
73
|
|
|
* @return void |
74
|
|
|
*/ |
75
|
|
|
abstract protected function process(); |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return MiddlewareInterface |
79
|
|
|
*/ |
80
|
|
|
final public function getNextMiddleware() |
81
|
|
|
{ |
82
|
|
|
return $this->nextMiddleware; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return bool |
87
|
|
|
*/ |
88
|
|
|
final public function wasExecuted() |
89
|
|
|
{ |
90
|
|
|
return $this->wasExecuted; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
|