1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Conia\Chuck\Routing; |
6
|
|
|
|
7
|
|
|
class Endpoint |
8
|
|
|
{ |
9
|
|
|
use AddsMiddleware; |
10
|
|
|
|
11
|
|
|
protected array $attrs = []; |
12
|
|
|
protected string $name = ''; |
13
|
|
|
protected string $renderer = 'json'; |
14
|
|
|
protected array $rendererArgs = []; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @psalm-param class-string $controller |
18
|
|
|
*/ |
19
|
11 |
|
public function __construct( |
20
|
|
|
protected readonly RouteAdderInterface $adder, |
21
|
|
|
protected readonly array|string $path, |
22
|
|
|
protected readonly string $controller, |
23
|
|
|
protected readonly string|array $args |
24
|
|
|
) { |
25
|
11 |
|
} |
26
|
|
|
|
27
|
11 |
|
public function add(): void |
28
|
|
|
{ |
29
|
11 |
|
if (is_array($this->args)) { |
30
|
2 |
|
$args = '/' . implode('/', array_map(fn ($arg) => '{' . (string)$arg . '}', $this->args)); |
31
|
|
|
} else { |
32
|
9 |
|
$args = '/{' . $this->args . '}'; |
33
|
|
|
} |
34
|
|
|
|
35
|
11 |
|
if (is_array($this->path)) { |
36
|
1 |
|
assert(is_string($this->path[0])); |
37
|
1 |
|
assert(is_string($this->path[1])); |
38
|
1 |
|
$plural = $this->path[0]; |
39
|
1 |
|
$singular = $this->path[1] . $args; |
40
|
|
|
} else { |
41
|
10 |
|
$plural = $this->path; |
42
|
10 |
|
$singular = $this->path . $args; |
43
|
|
|
} |
44
|
|
|
|
45
|
11 |
|
$this->addRoutes($plural, $singular); |
46
|
|
|
} |
47
|
|
|
|
48
|
4 |
|
public function name(string $name): static |
49
|
|
|
{ |
50
|
4 |
|
$this->name = $name; |
51
|
|
|
|
52
|
4 |
|
return $this; |
53
|
|
|
} |
54
|
|
|
|
55
|
1 |
|
public function render(string $renderer, mixed ...$args): static |
56
|
|
|
{ |
57
|
1 |
|
$this->renderer = $renderer; |
58
|
1 |
|
$this->rendererArgs = $args; |
59
|
|
|
|
60
|
1 |
|
return $this; |
61
|
|
|
} |
62
|
|
|
|
63
|
1 |
|
public function attrs(mixed ...$attrs): static |
64
|
|
|
{ |
65
|
1 |
|
$this->attrs = $attrs; |
66
|
|
|
|
67
|
1 |
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
11 |
|
protected function addRoutes( |
71
|
|
|
string $plural, |
72
|
|
|
string $singular, |
73
|
|
|
): void { |
74
|
11 |
|
$this->addRoute('DELETE', $plural, 'deleteList'); |
75
|
10 |
|
$this->addRoute('DELETE', $singular, 'delete'); |
76
|
10 |
|
$this->addRoute('GET', $plural, 'list'); |
77
|
10 |
|
$this->addRoute('GET', $singular, 'get'); |
78
|
10 |
|
$this->addRoute('HEAD', $plural, 'headList'); |
79
|
10 |
|
$this->addRoute('HEAD', $singular, 'head'); |
80
|
10 |
|
$this->addRoute('OPTIONS', $plural, 'optionsList'); |
81
|
10 |
|
$this->addRoute('OPTIONS', $singular, 'options'); |
82
|
10 |
|
$this->addRoute('PATCH', $singular, 'patch'); |
83
|
10 |
|
$this->addRoute('POST', $plural, 'post'); |
84
|
10 |
|
$this->addRoute('PUT', $singular, 'put'); |
85
|
|
|
} |
86
|
|
|
|
87
|
11 |
|
protected function addRoute(string $httpMethod, string $path, string $controllerMethod): void |
88
|
|
|
{ |
89
|
11 |
|
if (method_exists($this->controller, $controllerMethod)) { |
90
|
11 |
|
$name = $this->name ? $this->name . '-' . $controllerMethod : ''; |
91
|
|
|
|
92
|
11 |
|
$this->adder->addRoute( |
93
|
11 |
|
(new Route($path, [$this->controller, $controllerMethod], $name)) |
94
|
11 |
|
->method($httpMethod) |
95
|
11 |
|
->middleware(...$this->middleware) |
96
|
11 |
|
->render($this->renderer, ...$this->rendererArgs) |
97
|
11 |
|
->attrs(...$this->attrs) |
98
|
11 |
|
); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|