1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace League\Route; |
6
|
|
|
|
7
|
|
|
use League\Route\Middleware\{MiddlewareAwareInterface, MiddlewareAwareTrait}; |
8
|
|
|
use League\Route\Strategy\{StrategyAwareInterface, StrategyAwareTrait}; |
9
|
|
|
|
10
|
|
|
class RouteGroup implements |
11
|
|
|
MiddlewareAwareInterface, |
12
|
|
|
RouteCollectionInterface, |
13
|
|
|
RouteConditionHandlerInterface, |
14
|
|
|
StrategyAwareInterface |
15
|
|
|
{ |
16
|
|
|
use MiddlewareAwareTrait; |
17
|
|
|
use RouteCollectionTrait; |
18
|
|
|
use RouteConditionHandlerTrait; |
19
|
|
|
use StrategyAwareTrait; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var callable |
23
|
|
|
*/ |
24
|
|
|
protected $callback; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var RouteCollectionInterface |
28
|
|
|
*/ |
29
|
|
|
protected $collection; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected $prefix; |
35
|
|
|
|
36
|
21 |
|
public function __construct(string $prefix, callable $callback, RouteCollectionInterface $collection) |
37
|
|
|
{ |
38
|
21 |
|
$this->callback = $callback; |
39
|
21 |
|
$this->collection = $collection; |
40
|
21 |
|
$this->prefix = sprintf('/%s', ltrim($prefix, '/')); |
41
|
21 |
|
} |
42
|
|
|
|
43
|
18 |
|
public function __invoke(): void |
44
|
|
|
{ |
45
|
18 |
|
($this->callback)($this); |
46
|
18 |
|
} |
47
|
|
|
|
48
|
15 |
|
public function getPrefix(): string |
49
|
|
|
{ |
50
|
15 |
|
return $this->prefix; |
51
|
|
|
} |
52
|
|
|
|
53
|
18 |
|
public function map(string $method, string $path, $handler): Route |
54
|
|
|
{ |
55
|
18 |
|
$path = ($path === '/') ? $this->prefix : $this->prefix . sprintf('/%s', ltrim($path, '/')); |
56
|
18 |
|
$route = $this->collection->map($method, $path, $handler); |
57
|
|
|
|
58
|
18 |
|
$route->setParentGroup($this); |
59
|
|
|
|
60
|
18 |
|
if ($host = $this->getHost()) { |
61
|
3 |
|
$route->setHost($host); |
62
|
|
|
} |
63
|
|
|
|
64
|
18 |
|
if ($scheme = $this->getScheme()) { |
65
|
3 |
|
$route->setScheme($scheme); |
66
|
|
|
} |
67
|
|
|
|
68
|
18 |
|
if ($port = $this->getPort()) { |
69
|
3 |
|
$route->setPort($port); |
70
|
|
|
} |
71
|
|
|
|
72
|
18 |
|
if ($route->getStrategy() === null && $this->getStrategy() !== null) { |
73
|
9 |
|
$route->setStrategy($this->getStrategy()); |
74
|
|
|
} |
75
|
|
|
|
76
|
18 |
|
return $route; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|