1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace League\Route; |
4
|
|
|
|
5
|
|
|
use League\Route\Middleware\{MiddlewareAwareInterface, MiddlewareAwareTrait}; |
6
|
|
|
use League\Route\Strategy\{StrategyAwareInterface, StrategyAwareTrait}; |
7
|
|
|
|
8
|
|
|
class RouteGroup implements |
9
|
|
|
MiddlewareAwareInterface, |
10
|
|
|
RouteCollectionInterface, |
11
|
|
|
RouteConditionHandlerInterface, |
12
|
|
|
StrategyAwareInterface |
13
|
|
|
{ |
14
|
|
|
use MiddlewareAwareTrait; |
15
|
|
|
use RouteCollectionTrait; |
16
|
|
|
use RouteConditionHandlerTrait; |
17
|
|
|
use StrategyAwareTrait; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var callable |
21
|
|
|
*/ |
22
|
|
|
protected $callback; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var \League\Route\RouteCollectionInterface |
26
|
|
|
*/ |
27
|
|
|
protected $collection; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
protected $prefix; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Constructor. |
36
|
|
|
* |
37
|
|
|
* @param string $prefix |
38
|
|
|
* @param callable $callback |
39
|
|
|
* @param \League\Route\RouteCollection $collection |
40
|
|
|
*/ |
41
|
9 |
|
public function __construct(string $prefix, callable $callback, RouteCollectionInterface $collection) |
42
|
|
|
{ |
43
|
9 |
|
$this->callback = $callback; |
44
|
9 |
|
$this->collection = $collection; |
45
|
9 |
|
$this->prefix = sprintf('/%s', ltrim($prefix, '/')); |
46
|
9 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Return the prefix of the group. |
50
|
|
|
* |
51
|
|
|
* @return string |
52
|
|
|
*/ |
53
|
|
|
public function getPrefix() : string |
54
|
|
|
{ |
55
|
|
|
return $this->prefix; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Process the group and ensure routes are added to the collection. |
60
|
|
|
* |
61
|
|
|
* @return void |
62
|
|
|
*/ |
63
|
6 |
|
public function __invoke() : void |
64
|
|
|
{ |
65
|
6 |
|
call_user_func_array($this->callback, [$this]); |
66
|
6 |
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* {@inheritdoc} |
70
|
|
|
*/ |
71
|
6 |
|
public function map(string $method, string $path, $handler) : Route |
72
|
|
|
{ |
73
|
6 |
|
$path = ($path === '/') ? $this->prefix : $this->prefix . sprintf('/%s', ltrim($path, '/')); |
74
|
6 |
|
$route = $this->collection->map($method, $path, $handler); |
75
|
|
|
|
76
|
6 |
|
$route->setParentGroup($this); |
77
|
|
|
|
78
|
6 |
|
if ($host = $this->getHost()) { |
79
|
3 |
|
$route->setHost($host); |
80
|
|
|
} |
81
|
|
|
|
82
|
6 |
|
if ($scheme = $this->getScheme()) { |
83
|
3 |
|
$route->setScheme($scheme); |
84
|
|
|
} |
85
|
|
|
|
86
|
6 |
|
if ($port = $this->getPort()) { |
87
|
3 |
|
$route->setPort($port); |
88
|
|
|
} |
89
|
|
|
|
90
|
6 |
|
foreach ($this->getMiddlewareStack() as $middleware) { |
91
|
|
|
$route->middleware($middleware); |
92
|
|
|
} |
93
|
|
|
|
94
|
6 |
|
if (is_null($route->getStrategy()) && ! is_null($this->getStrategy())) { |
95
|
3 |
|
$route->setStrategy($this->getStrategy()); |
96
|
|
|
} |
97
|
|
|
|
98
|
6 |
|
return $route; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|