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

ICanBoogie / Module / 4256942650

pending completion
4256942650

push

github

Olivier Laviale
Replace descriptor files with configuration fragments

135 of 448 relevant lines covered (30.13%)

0.77 hits per line

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

0.0
/lib/Module/Hooks.php
1
<?php
2

3
/*
4
 * This file is part of the ICanBoogie package.
5
 *
6
 * (c) Olivier Laviale <olivier.laviale@gmail.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

12
namespace ICanBoogie\Module;
13

14
use ICanBoogie\Application;
15
use ICanBoogie\Binding\Routing\BeforeSynthesizeRoutesEvent;
16
use ICanBoogie\HTTP\RequestDispatcher;
17
use ICanBoogie\Operation\OperationRouteDispatcher;
18
use ICanBoogie\Routing\Route;
19

20
use LogicException;
21

22
use RegexIterator;
23

24
use function ICanBoogie\app;
25
use function ICanBoogie\format;
26

27
/**
28
 * Hook callbacks.
29
 */
30
class Hooks
31
{
32
    /*
33
     * Events
34
     */
35

36
    /**
37
     * @var array<string, string>
38
     *     Where _key_ is a route id (or action) and _value_ is a module identifier.
39
     *
40
     * @internal
41
     *
42
     * @deprecated
43
     */
44
    static public array $route_module_mapping;
45

46
    /**
47
     * Alter routes defined by modules by adding a `module` key that holds the identifier of the
48
     * module that defines the route.
49
     *
50
     * @param BeforeSynthesizeRoutesEvent $event
51
     *
52
     * @deprecated
53
     */
54
    static public function before_synthesize_routes(BeforeSynthesizeRoutesEvent $event): void
55
    {
56
        $module_roots = [];
×
57

58
        foreach (self::get_app_modules()->descriptors as $module_id => $descriptor) {
×
59
            $module_roots[$descriptor[Descriptor::PATH]] = $module_id;
×
60
        }
61

62
        foreach ($event->fragments as $module_root => &$fragment) {
×
63
            $module_root = dirname(dirname($module_root)) . DIRECTORY_SEPARATOR;
×
64

65
            if (empty($module_roots[$module_root])) {
×
66
                continue;
×
67
            }
68

69
            $module_id = $module_roots[$module_root];
×
70
            $route_module_mapping = [];
×
71

72
            foreach ($fragment as $route) {
×
73
                assert($route instanceof Route);
×
74

75
                $route_id = $route->id ?? $route->action;
×
76

77
                $route_module_mapping[$route_id] = $module_id;
×
78
            }
79
        }
80

81
        self::$route_module_mapping = $route_module_mapping;
×
82
    }
83

84
    /**
85
     * @param RequestDispatcher\AlterEvent $event
86
     * @param RequestDispatcher $target
87
     */
88
    static public function on_alter_request_dispatcher(
89
        RequestDispatcher\AlterEvent $event,
90
        RequestDispatcher $target
91
    ): void {
92
        $event->chain(function () use ($event, $target) {
×
93
            $routing = $target['routing'];
×
94

95
            self::assert_routing_dispatcher_is_valid($routing);
×
96

97
            $modules = self::get_app_modules();
×
98

99
            $target['routing'] = new ModuleOperationDispatcher($routing->routes, $modules);
×
100
            $event->insert_before('forwarded_operation', new ForwardedOperationDispatcher($modules), 'routing');
×
101
        });
×
102
    }
103

104
    /*
105
     * Support
106
     */
107

108
    /**
109
     * Returns the application's module collection.
110
     */
111
    static private function get_app_modules(): ModuleCollection
112
    {
113
        static $modules;
×
114

115
        return $modules ??= app()->modules;
×
116
    }
117

118
    /**
119
     * Asserts that a dispatcher is an instance of {@link \ICanBoogie\Routing\RouteDispacther}.
120
     *
121
     * @param mixed $dispatcher
122
     *
123
     * @throws LogicException if the dispatcher is not an instance of
124
     * {@link \ICanBoogie\Routing\RouteDispacther}.
125
     */
126
    static private function assert_routing_dispatcher_is_valid($dispatcher): void
127
    {
128
        if (!$dispatcher instanceof OperationRouteDispatcher) {
×
129
            throw new LogicException(
×
130
                format("Expected `routing` dispatcher to be an instance of %expected, got %actual instead.", [
×
131

132
                    'expected' => OperationRouteDispatcher::class,
×
133
                    'actual' => get_class($dispatcher)
×
134

135
                ])
×
136
            );
×
137
        }
138
    }
139
}
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