|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Yiisoft\Router; |
|
4
|
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
|
6
|
|
|
use Psr\Container\ContainerInterface; |
|
7
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
|
8
|
|
|
use Yiisoft\Router\Middleware\Callback; |
|
9
|
|
|
|
|
10
|
|
|
class Group implements RouteCollectorInterface |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var Group[]|Route[] |
|
14
|
|
|
*/ |
|
15
|
|
|
protected array $items = []; |
|
16
|
|
|
protected ?string $prefix; |
|
17
|
|
|
protected array $middlewares = []; |
|
18
|
|
|
private ?ContainerInterface $container = null; |
|
19
|
|
|
|
|
20
|
10 |
|
public function __construct(?string $prefix = null, ?callable $callback = null, ContainerInterface $container = null) |
|
21
|
|
|
{ |
|
22
|
10 |
|
$this->container = $container; |
|
23
|
10 |
|
$this->prefix = $prefix; |
|
24
|
|
|
|
|
25
|
10 |
|
if ($callback !== null) { |
|
26
|
7 |
|
$callback($this); |
|
27
|
|
|
} |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
3 |
|
final public static function create(?string $prefix, array $routes = [], ContainerInterface $container = null): self |
|
31
|
|
|
{ |
|
32
|
|
|
return new self($prefix, static function (Group $group) use ($routes) { |
|
33
|
3 |
|
foreach ($routes as $route) { |
|
34
|
3 |
|
if ($route instanceof Route) { |
|
35
|
3 |
|
$group->addRoute($route); |
|
36
|
3 |
|
} elseif ($route instanceof Group) { |
|
37
|
3 |
|
$group->addGroup($route); |
|
38
|
|
|
} else { |
|
39
|
|
|
throw new InvalidArgumentException('Routes should be either instances of Route or Group'); |
|
40
|
|
|
} |
|
41
|
|
|
} |
|
42
|
3 |
|
}, $container); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
3 |
|
final public function withContainer(ContainerInterface $container): self |
|
46
|
|
|
{ |
|
47
|
3 |
|
$group = clone $this; |
|
48
|
3 |
|
$group->container = $container; |
|
49
|
3 |
|
foreach ($group->items as $index => $item) { |
|
50
|
3 |
|
if (!$item->hasContainer()) { |
|
51
|
3 |
|
$item = $item->withContainer($container); |
|
52
|
3 |
|
$group->items[$index] = $item; |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
3 |
|
return $group; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
9 |
|
final public function hasContainer(): bool |
|
60
|
|
|
{ |
|
61
|
9 |
|
return $this->container !== null; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
9 |
|
final public function addRoute(Route $route): self |
|
65
|
|
|
{ |
|
66
|
9 |
|
if (!$route->hasContainer() && $this->hasContainer()) { |
|
67
|
3 |
|
$route = $route->withContainer($this->container); |
|
68
|
|
|
} |
|
69
|
9 |
|
$this->items[] = $route; |
|
70
|
9 |
|
return $this; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
5 |
|
final public function addGroup(Group $group): self |
|
74
|
|
|
{ |
|
75
|
5 |
|
if (!$group->hasContainer() && $this->hasContainer()) { |
|
76
|
3 |
|
$group = $group->withContainer($this->container); |
|
77
|
|
|
} |
|
78
|
5 |
|
$this->items[] = $group; |
|
79
|
5 |
|
return $this; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @param callable|MiddlewareInterface $middleware |
|
84
|
|
|
* @return $this |
|
85
|
|
|
*/ |
|
86
|
5 |
|
final public function addMiddleware($middleware): self |
|
87
|
|
|
{ |
|
88
|
5 |
|
if (is_callable($middleware)) { |
|
89
|
|
|
$middleware = new Callback($middleware, $this->container); |
|
|
|
|
|
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
5 |
|
if (!$middleware instanceof MiddlewareInterface) { |
|
93
|
|
|
throw new InvalidArgumentException('Parameter should be either a PSR middleware or a callable.'); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
5 |
|
array_unshift($this->middlewares, $middleware); |
|
97
|
|
|
|
|
98
|
5 |
|
return $this; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @return Route|Group[] |
|
103
|
|
|
*/ |
|
104
|
9 |
|
final public function getItems(): array |
|
105
|
|
|
{ |
|
106
|
9 |
|
return $this->items; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
5 |
|
final public function getPrefix(): ?string |
|
110
|
|
|
{ |
|
111
|
5 |
|
return $this->prefix; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
6 |
|
final public function getMiddlewares(): array |
|
115
|
|
|
{ |
|
116
|
6 |
|
return $this->middlewares; |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|