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

ICanBoogie / bind-routing / 4501100496

pending completion
4501100496

push

github

Olivier Laviale
Set tag 'action_responder' automatically

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

89 of 146 relevant lines covered (60.96%)

0.92 hits per line

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

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

3
namespace ICanBoogie\Binding\Routing;
4

5
use ICanBoogie\Binding\Routing\Attribute\Connect;
6
use ICanBoogie\Binding\Routing\Attribute\Delete;
7
use ICanBoogie\Binding\Routing\Attribute\Get;
8
use ICanBoogie\Binding\Routing\Attribute\Head;
9
use ICanBoogie\Binding\Routing\Attribute\Options;
10
use ICanBoogie\Binding\Routing\Attribute\Patch;
11
use ICanBoogie\Binding\Routing\Attribute\Post;
12
use ICanBoogie\Binding\Routing\Attribute\Put;
13
use ICanBoogie\Binding\Routing\Attribute\Route;
14
use ICanBoogie\Binding\Routing\Attribute\Trace;
15
use ICanBoogie\Config\Builder;
16
use ICanBoogie\Routing\RouteCollector;
17
use ICanBoogie\Routing\RouteProvider;
18
use LogicException;
19
use olvlvl\ComposerAttributeCollector\Attributes;
20
use olvlvl\ComposerAttributeCollector\TargetMethod;
21

22
use function class_exists;
23
use function sprintf;
24

25
/**
26
 * A config builder for 'routes' fragments.
27
 *
28
 * @implements Builder<RouteProvider>
29
 */
30
final class ConfigBuilder extends RouteCollector implements Builder
31
{
32
    public static function get_fragment_filename(): string
33
    {
34
        return 'routes';
1✔
35
    }
36

37
    public function build(): RouteProvider
38
    {
39
        return $this->collect();
2✔
40
    }
41

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

58
        $route_by_class = $this->build_route_by_class();
2✔
59

60
        /** @var TargetMethod<Route>[] $target_methods */
61
        $target_methods = [
2✔
62
            ...Attributes::findTargetMethods(Get::class),
2✔
63
            ...Attributes::findTargetMethods(Head::class),
2✔
64
            ...Attributes::findTargetMethods(Post::class),
2✔
65
            ...Attributes::findTargetMethods(Put::class),
2✔
66
            ...Attributes::findTargetMethods(Delete::class),
2✔
67
            ...Attributes::findTargetMethods(Connect::class),
2✔
68
            ...Attributes::findTargetMethods(Options::class),
2✔
69
            ...Attributes::findTargetMethods(Trace::class),
2✔
70
            ...Attributes::findTargetMethods(Patch::class),
2✔
71
            ...Attributes::findTargetMethods(Route::class),
2✔
72
        ];
2✔
73

74
        foreach ($target_methods as $method) {
2✔
75
            $prefix = $route_by_class[$method->class]->pattern ?? '';
2✔
76

77
            $attribute = $method->attribute;
2✔
78
            $pattern = $prefix . $attribute->pattern;
2✔
79
            $action = $attribute->action
2✔
80
                ?? ActionResolver::resolve_action($method->class, $method->name);
2✔
81

82
            $this->route(
2✔
83
                pattern: $pattern,
2✔
84
                action:  $action,
2✔
85
                methods:  $attribute->methods,
2✔
86
                id:  $attribute->id
2✔
87
            );
2✔
88
        }
89

90
        return $this;
2✔
91
    }
92

93
    /**
94
     * @return array<class-string, Route>
95
     */
96
    private function build_route_by_class(): array
97
    {
98
        $by_class = [];
2✔
99

100
        foreach (Attributes::findTargetClasses(Route::class) as $target_class) {
2✔
101
            $by_class[$target_class->name] = $target_class->attribute;
2✔
102
        }
103

104
        return $by_class;
2✔
105
    }
106
}
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

© 2025 Coveralls, Inc