• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ICanBoogie / bind-routing / 4517953600

pending completion
4517953600

push

github

Olivier Laviale
Automatically register controllers as services

33 of 33 new or added lines in 1 file covered. (100.0%)

82 of 139 relevant lines covered (58.99%)

0.87 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

81.25
/lib/ConfigBuilder.php
1
<?php
2

3
namespace ICanBoogie\Binding\Routing;
4

5
use ICanBoogie\Binding\Routing\Attribute\Route;
6
use ICanBoogie\Config\Builder;
7
use ICanBoogie\Routing\RouteCollector;
8
use ICanBoogie\Routing\RouteProvider;
9
use LogicException;
10
use olvlvl\ComposerAttributeCollector\Attributes;
11
use olvlvl\ComposerAttributeCollector\TargetClass;
12
use olvlvl\ComposerAttributeCollector\TargetMethod;
13

14
use function class_exists;
15
use function ICanBoogie\iterable_to_dictionary;
16
use function sprintf;
17

18
/**
19
 * A config builder for 'routes' fragments.
20
 *
21
 * @implements Builder<RouteProvider>
22
 */
23
final class ConfigBuilder extends RouteCollector implements Builder
24
{
25
    public static function get_fragment_filename(): string
26
    {
27
        return 'routes';
1✔
28
    }
29

30
    public function build(): RouteProvider
31
    {
32
        return $this->collect();
2✔
33
    }
34

35
    /**
36
     * Builds configuration from the {@link Route} attribute.
37
     *
38
     * @return $this
39
     */
40
    public function from_attributes(): self
41
    {
42
        if (!class_exists(Attributes::class)) {
2✔
43
            throw new LogicException(
×
44
                sprintf(
×
45
                    "Unable to build from attributes, the class %s is not available",
×
46
                    Attributes::class
×
47
                )
×
48
            );
×
49
        }
50

51
        $route_by_class = $this->build_route_by_class();
2✔
52
        $target_methods = $this->get_target_methods();
2✔
53

54
        foreach ($target_methods as $method) {
2✔
55
            $prefix = $route_by_class[$method->class]->pattern ?? '';
2✔
56

57
            $attribute = $method->attribute;
2✔
58
            $pattern = $prefix . $attribute->pattern;
2✔
59
            $action = $attribute->action
2✔
60
                ?? ActionResolver::resolve_action($method->class, $method->name);
2✔
61

62
            $this->route(
2✔
63
                pattern: $pattern,
2✔
64
                action: $action,
2✔
65
                methods: $attribute->methods,
2✔
66
                id: $attribute->id
2✔
67
            );
2✔
68
        }
69

70
        return $this;
2✔
71
    }
72

73
    /**
74
     * @return array<TargetMethod<Route>>
75
     */
76
    private function get_target_methods(): array
77
    {
78
        /** @phpstan-ignore-next-line */
79
        return Attributes::filterTargetMethods(
2✔
80
            Attributes::predicateForAttributeInstanceOf(Route::class)
2✔
81
        );
2✔
82
    }
83

84
    /**
85
     * @return array<class-string, Route>
86
     */
87
    private function build_route_by_class(): array
88
    {
89
        return iterable_to_dictionary(
2✔
90
            Attributes::findTargetClasses(Route::class),
2✔
91
            fn(TargetClass $t): string => $t->name,
2✔
92
            fn(TargetClass $t): Route => $t->attribute,
2✔
93
        );
2✔
94
    }
95
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc