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