1 | <?php |
||
16 | class Janitor |
||
17 | { |
||
18 | /** |
||
19 | * List of watchers. |
||
20 | * |
||
21 | * @var array |
||
22 | */ |
||
23 | protected $watchers = []; |
||
24 | |||
25 | /** |
||
26 | * List of excluders. |
||
27 | * |
||
28 | * @var array |
||
29 | */ |
||
30 | protected $excluders = []; |
||
31 | |||
32 | /** |
||
33 | * Resolve handler. |
||
34 | * |
||
35 | * @var callable |
||
36 | */ |
||
37 | protected $handler; |
||
38 | |||
39 | /** |
||
40 | * Request attribute name to store currently active watcher. |
||
41 | * |
||
42 | * @var string |
||
43 | */ |
||
44 | protected $attributeName; |
||
45 | |||
46 | /** |
||
47 | * @param array $watchers |
||
48 | * @param array $excluders |
||
49 | * @param callable|null $handler |
||
50 | * @param string $attributeName |
||
51 | */ |
||
52 | public function __construct( |
||
69 | |||
70 | /** |
||
71 | * Add maintenance watcher. |
||
72 | * |
||
73 | * @param \Janitor\Watcher $watcher |
||
74 | */ |
||
75 | public function addWatcher(Watcher $watcher) |
||
81 | |||
82 | /** |
||
83 | * Add excluder condition. |
||
84 | * |
||
85 | * @param \Janitor\Excluder $excluder |
||
86 | */ |
||
87 | public function addExcluder(Excluder $excluder) |
||
93 | |||
94 | /** |
||
95 | * Set handler. |
||
96 | * |
||
97 | * @param callable $handler |
||
98 | */ |
||
99 | public function setHandler(callable $handler) |
||
105 | |||
106 | /** |
||
107 | * Set request attribute name to store active watcher. |
||
108 | * |
||
109 | * @param string $attributeName |
||
110 | */ |
||
111 | public function setAttributeName($attributeName) |
||
117 | |||
118 | /** |
||
119 | * Retrieve request attribute name storing active watcher. |
||
120 | * |
||
121 | * @return string |
||
122 | */ |
||
123 | public function getAttributeName() |
||
127 | |||
128 | /** |
||
129 | * Get next scheduled time spans. |
||
130 | * |
||
131 | * Returns an array of ['start' => \DateTime, 'end' => \DateTime] |
||
132 | * |
||
133 | * @param int $count |
||
134 | * |
||
135 | * @return array |
||
136 | */ |
||
137 | public function getScheduledTimes($count = 5) |
||
160 | |||
161 | /** |
||
162 | * Run middleware. |
||
163 | * |
||
164 | * @param \Psr\Http\Message\ServerRequestInterface $request |
||
165 | * @param \Psr\Http\Message\ResponseInterface $response |
||
166 | * @param callable $next |
||
167 | * |
||
168 | * @return \Psr\Http\Message\ResponseInterface |
||
169 | */ |
||
170 | public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) |
||
184 | |||
185 | /** |
||
186 | * Get currenlty active watcher. |
||
187 | * |
||
188 | * @return \Janitor\Watcher|null |
||
189 | */ |
||
190 | protected function getActiveWatcher() |
||
200 | |||
201 | /** |
||
202 | * Whether excluding conditions are met. |
||
203 | * |
||
204 | * @param \Psr\Http\Message\ServerRequestInterface $request |
||
205 | * |
||
206 | * @return bool |
||
207 | */ |
||
208 | protected function isExcluded(ServerRequestInterface $request) |
||
218 | |||
219 | /** |
||
220 | * Retrieve handler. |
||
221 | * |
||
222 | * @return callable |
||
223 | */ |
||
224 | protected function getHandler() |
||
232 | } |
||
233 |