/
ConfigBuilder.php
76 lines (66 loc) · 2.08 KB
/
ConfigBuilder.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
namespace ICanBoogie\Binding\Routing;
use ICanBoogie\Binding\Routing\Attribute\Delete;
use ICanBoogie\Binding\Routing\Attribute\Get;
use ICanBoogie\Binding\Routing\Attribute\Post;
use ICanBoogie\Binding\Routing\Attribute\Put;
use ICanBoogie\Binding\Routing\Attribute\Route;
use ICanBoogie\Config\Builder;
use ICanBoogie\Routing\RouteCollector;
use ICanBoogie\Routing\RouteProvider;
use LogicException;
use olvlvl\ComposerAttributeCollector\Attributes;
use function class_exists;
use function sprintf;
/**
* A config builder for 'routes' fragments.
*
* @implements Builder<RouteProvider>
*/
final class ConfigBuilder extends RouteCollector implements Builder
{
public static function get_fragment_filename(): string
{
return 'routes';
}
public function build(): RouteProvider
{
return $this->collect();
}
/**
* Builds configuration from the {@link Route} attribute.
*
* @return $this
*/
public function from_attributes(): self
{
if (!class_exists(Attributes::class)) {
throw new LogicException(
sprintf(
"Unable to build from attributes, the class %s is not available",
Attributes::class
)
);
}
$target_methods = [
...Attributes::findTargetMethods(Get::class),
...Attributes::findTargetMethods(Post::class),
...Attributes::findTargetMethods(Put::class),
...Attributes::findTargetMethods(Delete::class),
...Attributes::findTargetMethods(Route::class),
];
foreach ($target_methods as $method) {
/** @var Route $attribute */
$attribute = $method->attribute;
$action = $attribute->action
?? ActionResolver::resolve_action($method->class, $method->name);
$this->route(
pattern: $attribute->pattern,
action: $action,
methods: $attribute->methods,
id: $attribute->id
);
}
return $this;
}
}