|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Conia\Chuck\Routing; |
|
6
|
|
|
|
|
7
|
|
|
use Closure; |
|
8
|
|
|
use Conia\Chuck\Exception\RuntimeException; |
|
9
|
|
|
use Conia\Chuck\Routing\RouteAdderInterface; |
|
10
|
|
|
|
|
11
|
|
|
class Group implements RouteAdderInterface |
|
12
|
|
|
{ |
|
13
|
|
|
use AddsRoutes; |
|
14
|
|
|
use AddsMiddleware; |
|
15
|
|
|
|
|
16
|
|
|
/** @psalm-var list<Group> */ |
|
17
|
|
|
protected array $subgroups = []; |
|
18
|
|
|
protected ?RouteAdderInterface $routeAdder = null; |
|
19
|
|
|
protected ?string $renderer = null; |
|
20
|
|
|
protected ?string $controller = null; |
|
21
|
|
|
protected bool $created = false; |
|
22
|
|
|
|
|
23
|
14 |
|
public function __construct( |
|
24
|
|
|
protected string $patternPrefix, |
|
25
|
|
|
protected Closure $createClosure, |
|
26
|
|
|
protected string $namePrefix = '', |
|
27
|
|
|
) { |
|
28
|
14 |
|
} |
|
29
|
|
|
|
|
30
|
3 |
|
public function controller(string $controller): static |
|
31
|
|
|
{ |
|
32
|
3 |
|
$this->controller = $controller; |
|
33
|
|
|
|
|
34
|
3 |
|
return $this; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
1 |
|
public function render(string $renderer): static |
|
38
|
|
|
{ |
|
39
|
1 |
|
$this->renderer = $renderer; |
|
40
|
|
|
|
|
41
|
1 |
|
return $this; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
14 |
|
public function addRoute(Route $route): Route |
|
45
|
|
|
{ |
|
46
|
14 |
|
$route->prefix($this->patternPrefix, $this->namePrefix); |
|
47
|
|
|
|
|
48
|
14 |
|
if ($this->renderer && empty($route->getRenderer())) { |
|
49
|
1 |
|
$route->render($this->renderer); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
14 |
|
if ($this->controller) { |
|
53
|
3 |
|
$route->controller($this->controller); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
12 |
|
if (!empty($this->middleware)) { |
|
57
|
2 |
|
$route->replaceMiddleware(array_merge($this->middleware, $route->getMiddleware())); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
12 |
|
if ($this->routeAdder) { |
|
61
|
11 |
|
$this->routeAdder->addRoute($route); |
|
62
|
|
|
|
|
63
|
11 |
|
return $route; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
1 |
|
throw new RuntimeException('RouteAdder not set'); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
1 |
|
public function addGroup(Group $group): void |
|
70
|
|
|
{ |
|
71
|
1 |
|
$group->create($this); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
1 |
|
public function group( |
|
75
|
|
|
string $patternPrefix, |
|
76
|
|
|
Closure $createClosure, |
|
77
|
|
|
string $namePrefix = '', |
|
78
|
|
|
): Group { |
|
79
|
1 |
|
$group = new Group($patternPrefix, $createClosure, $namePrefix); |
|
80
|
1 |
|
$this->subgroups[] = $group; |
|
81
|
|
|
|
|
82
|
1 |
|
return $group; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
13 |
|
public function create(RouteAdderInterface $adder): void |
|
86
|
|
|
{ |
|
87
|
13 |
|
if ($this->created) { |
|
88
|
1 |
|
return; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
13 |
|
$this->created = true; |
|
92
|
13 |
|
$this->routeAdder = $adder; |
|
93
|
13 |
|
($this->createClosure)($this); |
|
94
|
|
|
|
|
95
|
11 |
|
foreach ($this->subgroups as $subgroup) { |
|
96
|
1 |
|
$subgroup->create($this); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|