1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* It's free open-source software released under the MIT License. |
5
|
|
|
* |
6
|
|
|
* @author Anatoly Fenric <[email protected]> |
7
|
|
|
* @copyright Copyright (c) 2018, Anatoly Fenric |
8
|
|
|
* @license https://github.com/sunrise-php/http-router/blob/master/LICENSE |
9
|
|
|
* @link https://github.com/sunrise-php/http-router |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Sunrise\Http\Router; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Import classes |
16
|
|
|
*/ |
17
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Import functions |
21
|
|
|
*/ |
22
|
|
|
use function array_merge; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* RouteCollectorGroupAction |
26
|
|
|
*/ |
27
|
|
|
class RouteCollectorGroupAction |
28
|
|
|
{ |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Route collection for group activities |
32
|
|
|
* |
33
|
|
|
* @var RouteCollectionInterface |
34
|
|
|
*/ |
35
|
|
|
private $collection; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Constructor of the class |
39
|
|
|
* |
40
|
|
|
* @param RouteCollectionInterface $collection |
41
|
|
|
*/ |
42
|
8 |
|
public function __construct(RouteCollectionInterface $collection) |
43
|
|
|
{ |
44
|
8 |
|
$this->collection = $collection; |
45
|
8 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Sets the given host to all routes in the collection |
49
|
|
|
* |
50
|
|
|
* @param string $host |
51
|
|
|
* |
52
|
|
|
* @return self |
53
|
|
|
* |
54
|
|
|
* @since 2.6.0 |
55
|
|
|
*/ |
56
|
1 |
|
public function setHost(string $host) : self |
57
|
|
|
{ |
58
|
1 |
|
foreach ($this->collection->all() as $route) { |
59
|
1 |
|
$route->setHost($host); |
60
|
|
|
} |
61
|
|
|
|
62
|
1 |
|
return $this; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Adds the given path prefix to all routes in the collection |
67
|
|
|
* |
68
|
|
|
* @param string $prefix |
69
|
|
|
* |
70
|
|
|
* @return self |
71
|
|
|
*/ |
72
|
2 |
|
public function addPrefix(string $prefix) : self |
73
|
|
|
{ |
74
|
2 |
|
foreach ($this->collection->all() as $route) { |
75
|
2 |
|
$route->addPrefix($prefix); |
76
|
|
|
} |
77
|
|
|
|
78
|
2 |
|
return $this; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Adds the given path suffix to all routes in the collection |
83
|
|
|
* |
84
|
|
|
* @param string $suffix |
85
|
|
|
* |
86
|
|
|
* @return self |
87
|
|
|
*/ |
88
|
1 |
|
public function addSuffix(string $suffix) : self |
89
|
|
|
{ |
90
|
1 |
|
foreach ($this->collection->all() as $route) { |
91
|
1 |
|
$route->addSuffix($suffix); |
92
|
|
|
} |
93
|
|
|
|
94
|
1 |
|
return $this; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Adds the given method(s) to all routes in the collection |
99
|
|
|
* |
100
|
|
|
* @param string ...$methods |
101
|
|
|
* |
102
|
|
|
* @return self |
103
|
|
|
*/ |
104
|
1 |
|
public function addMethod(string ...$methods) : self |
105
|
|
|
{ |
106
|
1 |
|
foreach ($this->collection->all() as $route) { |
107
|
1 |
|
$route->addMethod(...$methods); |
108
|
|
|
} |
109
|
|
|
|
110
|
1 |
|
return $this; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Adds the given middleware(s) to all routes in the collection |
115
|
|
|
* |
116
|
|
|
* @param MiddlewareInterface ...$middlewares |
117
|
|
|
* |
118
|
|
|
* @return self |
119
|
|
|
*/ |
120
|
1 |
|
public function addMiddleware(MiddlewareInterface ...$middlewares) : self |
121
|
|
|
{ |
122
|
1 |
|
foreach ($this->collection->all() as $route) { |
123
|
1 |
|
$route->addMiddleware(...$middlewares); |
124
|
|
|
} |
125
|
|
|
|
126
|
1 |
|
return $this; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Adds the given middleware(s) to the beginning of all routes in the collection |
131
|
|
|
* |
132
|
|
|
* @param MiddlewareInterface ...$middlewares |
133
|
|
|
* |
134
|
|
|
* @return self |
135
|
|
|
*/ |
136
|
1 |
|
public function unshiftMiddleware(MiddlewareInterface ...$middlewares) : self |
137
|
|
|
{ |
138
|
1 |
|
foreach ($this->collection->all() as $route) { |
139
|
1 |
|
$route->setMiddlewares(...array_merge( |
140
|
1 |
|
$middlewares, |
141
|
1 |
|
$route->getMiddlewares() |
142
|
|
|
)); |
143
|
|
|
} |
144
|
|
|
|
145
|
1 |
|
return $this; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|