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

ICanBoogie / Module / 4245494566

pending completion
4245494566

push

github

Olivier Laviale
Resolve templates using Config instead of Collection

115 of 432 relevant lines covered (26.62%)

0.76 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
     * Clears modules cache.
106
     *
107
     * @param Application\ClearCacheEvent $event
108
     * @param Application $app
109
     */
110
    static public function on_app_clear_cache(Application\ClearCacheEvent $event, Application $app): void
111
    {
112
        $vars = $app->vars;
×
113
        $iterator = new RegexIterator($vars->getIterator(), '/^cached_modules_/');
×
114

115
        foreach ($iterator as $key) {
×
116
            $vars->eliminate($key);
×
117
        }
118
    }
119

120
    /*
121
     * Support
122
     */
123

124
    /**
125
     * Returns the application's module collection.
126
     */
127
    static private function get_app_modules(): ModuleCollection
128
    {
129
        static $modules;
×
130

131
        return $modules ??= app()->modules;
×
132
    }
133

134
    /**
135
     * Asserts that a dispatcher is an instance of {@link \ICanBoogie\Routing\RouteDispacther}.
136
     *
137
     * @param mixed $dispatcher
138
     *
139
     * @throws LogicException if the dispatcher is not an instance of
140
     * {@link \ICanBoogie\Routing\RouteDispacther}.
141
     */
142
    static private function assert_routing_dispatcher_is_valid($dispatcher): void
143
    {
144
        if (!$dispatcher instanceof OperationRouteDispatcher) {
×
145
            throw new LogicException(
×
146
                format("Expected `routing` dispatcher to be an instance of %expected, got %actual instead.", [
×
147

148
                    'expected' => OperationRouteDispatcher::class,
×
149
                    'actual' => get_class($dispatcher)
×
150

151
                ])
×
152
            );
×
153
        }
154
    }
155
}
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