1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of web-stack |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Slick\WebStack\Infrastructure\FrontController; |
13
|
|
|
|
14
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
15
|
|
|
use IteratorAggregate; |
16
|
|
|
use Slick\ModuleApi\Infrastructure\FrontController\MiddlewareHandlerInterface; |
17
|
|
|
use Slick\ModuleApi\Infrastructure\FrontController\Position; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* MiddlewareList |
21
|
|
|
* |
22
|
|
|
* @package Slick\WebStack\Infrastructure\FrontController |
23
|
|
|
* @implements IteratorAggregate<string, MiddlewareHandlerInterface> |
24
|
|
|
*/ |
25
|
|
|
final class MiddlewareList implements IteratorAggregate |
26
|
|
|
{ |
27
|
|
|
|
28
|
|
|
/** @var ArrayCollection<string, MiddlewareHandlerInterface> */ |
29
|
|
|
private ArrayCollection $middlewares; |
30
|
|
|
|
31
|
|
|
|
32
|
|
|
public function __construct() |
33
|
|
|
{ |
34
|
|
|
$this->middlewares = new ArrayCollection(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @inheritDoc |
39
|
|
|
* @return ArrayCollection<string, MiddlewareHandlerInterface> |
40
|
|
|
*/ |
41
|
|
|
public function getIterator(): ArrayCollection |
42
|
|
|
{ |
43
|
|
|
return $this->middlewares; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function add(MiddlewareHandlerInterface $middleware): self |
47
|
|
|
{ |
48
|
|
|
$newList = []; |
49
|
|
|
|
50
|
|
|
$position = $middleware->middlewarePosition(); |
51
|
|
|
if ($position->position() === Position::Top) { |
52
|
|
|
$newList[$middleware->name()] = $middleware; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
foreach ($this->middlewares as $key => $current) { |
56
|
|
|
if ($position->reference() === $key) { |
57
|
|
|
if ($position->position() === Position::Before) { |
58
|
|
|
$newList[$middleware->name()] = $middleware; |
59
|
|
|
$newList[$key] = $current; |
60
|
|
|
continue; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
if ($position->position() === Position::After) { |
64
|
|
|
$newList[$key] = $current; |
65
|
|
|
$newList[$middleware->name()] = $middleware; |
66
|
|
|
continue; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$newList[$key] = $current; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
if ($position->position() === Position::Bottom) { |
74
|
|
|
$newList[$middleware->name()] = $middleware; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$this->middlewares = new ArrayCollection($newList); |
78
|
|
|
return $this; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|