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