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

valkyrjaio / valkyrja / 13047041070

30 Jan 2025 06:43AM UTC coverage: 47.621% (+0.2%) from 47.422%
13047041070

push

github

MelechMizrachi
PHPStan level 7 and 8.

168 of 1038 new or added lines in 111 files covered. (16.18%)

444 existing lines in 45 files now uncovered.

5195 of 10909 relevant lines covered (47.62%)

18.83 hits per line

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

44.44
/src/Valkyrja/Http/Routing/Collection/CacheableCollection.php
1
<?php
2

3
declare(strict_types=1);
4

5
/*
6
 * This file is part of the Valkyrja Framework package.
7
 *
8
 * (c) Melech Mizrachi <melechmizrachi@gmail.com>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13

14
namespace Valkyrja\Http\Routing\Collection;
15

16
use Valkyrja\Config\Config;
17
use Valkyrja\Container\Contract\Container;
18
use Valkyrja\Http\Routing\Attribute\Contract\Attributes;
19
use Valkyrja\Http\Routing\Collector\Contract\Collector;
20
use Valkyrja\Http\Routing\Config as RoutingConfig;
21
use Valkyrja\Http\Routing\Config\Cache;
22
use Valkyrja\Http\Routing\Exception\InvalidRoutePathException;
23
use Valkyrja\Support\Cacheable\Cacheable;
24

25
use function is_file;
26

27
/**
28
 * Class CacheableCollection.
29
 *
30
 * @author Melech Mizrachi
31
 */
32
class CacheableCollection extends Collection
33
{
34
    /**
35
     * @use Cacheable<RoutingConfig, array<string, mixed>, Cache>
36
     */
37
    use Cacheable;
38

39
    /**
40
     * CacheableCollection constructor.
41
     *
42
     * @param Container                          $container
43
     * @param RoutingConfig|array<string, mixed> $config
44
     */
45
    public function __construct(
46
        protected Container $container,
47
        protected RoutingConfig|array $config
48
    ) {
49
    }
6✔
50

51
    /**
52
     * @inheritDoc
53
     */
54
    public function getCacheable(): Config
55
    {
56
        $this->setup(true, false);
×
57

58
        $config          = new Cache();
×
59
        $config->routes  = [];
×
60
        $config->static  = $this->static;
×
61
        $config->dynamic = $this->dynamic;
×
62
        $config->named   = $this->named;
×
63

64
        foreach ($this->routes as $id => $route) {
×
65
            $config->routes[$id] = $route->asArray();
×
66
        }
67

68
        return $config;
×
69
    }
70

71
    /**
72
     * @inheritDoc
73
     *
74
     * @return RoutingConfig|array<string, mixed> $config The config
75
     */
76
    protected function getConfig(): Config|array
77
    {
78
        return $this->config;
6✔
79
    }
80

81
    /**
82
     * @inheritDoc
83
     *
84
     * @param RoutingConfig|array<string, mixed> $config The config
85
     */
86
    protected function beforeSetup(Config|array $config): void
87
    {
88
    }
6✔
89

90
    /**
91
     * @inheritDoc
92
     *
93
     * @param RoutingConfig|array<string, mixed> $config The config
94
     */
95
    protected function setupNotCached(Config|array $config): void
96
    {
97
    }
6✔
98

99
    /**
100
     * @inheritDoc
101
     *
102
     * @param RoutingConfig|array<string, mixed> $config The config
103
     */
104
    protected function setupFromCache(Config|array $config): void
105
    {
NEW
106
        $cache = $config['cache'] ?? null;
×
107

NEW
108
        if ($cache === null) {
×
NEW
109
            $cache         = [];
×
NEW
110
            $cacheFilePath = $config['cacheFilePath'];
×
111

NEW
112
            if (is_file($cacheFilePath)) {
×
NEW
113
                $cache = require $cacheFilePath;
×
114
            }
115
        }
116

NEW
117
        $this->routes  = $cache['routes'] ?? [];
×
NEW
118
        $this->static  = $cache['static'] ?? [];
×
NEW
119
        $this->dynamic = $cache['dynamic'] ?? [];
×
NEW
120
        $this->named   = $cache['named'] ?? [];
×
121
    }
122

123
    /**
124
     * @inheritDoc
125
     *
126
     * @param RoutingConfig|array<string, mixed> $config The config
127
     *
128
     * @throws InvalidRoutePathException
129
     */
130
    protected function setupAttributes(Config|array $config): void
131
    {
132
        /** @var Attributes $routeAttributes */
133
        $routeAttributes = $this->container->getSingleton(Attributes::class);
6✔
134
        /** @var class-string[] $controllers */
135
        $controllers = $config['controllers'];
6✔
136

137
        // Get all the attributes routes from the list of controllers
138
        // Iterate through the routes
139
        foreach ($routeAttributes->getRoutes(...$controllers) as $route) {
6✔
140
            // Set the route
141
            $this->add($route);
×
142
        }
143
    }
144

145
    /**
146
     * @inheritDoc
147
     *
148
     * @param RoutingConfig|array<string, mixed> $config The config
149
     *
150
     * @throws InvalidRoutePathException
151
     */
152
    protected function afterSetup(Config|array $config): void
153
    {
154
        $this->dynamic = [];
6✔
155

156
        foreach ($this->routes as $route) {
6✔
157
            $this->setRouteToRequestMethods($route);
4✔
158
        }
159
    }
160

161
    /**
162
     * @inheritDoc
163
     *
164
     * @param RoutingConfig|array<string, mixed> $config The config
165
     */
166
    protected function requireFilePath(Config|array $config): void
167
    {
168
        $filePath = $config['filePath'] ?? null;
6✔
169

170
        if ($filePath === null) {
6✔
171
            return;
2✔
172
        }
173

174
        $collector = $this->container->getSingleton(Collector::class);
4✔
175

176
        if (is_file($filePath)) {
4✔
177
            require $filePath;
4✔
178
        }
179
    }
180
}
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