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

valkyrjaio / valkyrja / 15738627666

18 Jun 2025 04:39PM UTC coverage: 46.073% (-1.2%) from 47.32%
15738627666

push

github

MelechMizrachi
Http/Routing: Updated to use Dispatcher2. Deprecating old way of doing things. Forcing attributes.

63 of 449 new or added lines in 18 files covered. (14.03%)

30 existing lines in 8 files now uncovered.

5109 of 11089 relevant lines covered (46.07%)

15.32 hits per line

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

67.74
/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\Container\Contract\Container;
17
use Valkyrja\Http\Routing\Attribute\Contract\Collector;
18
use Valkyrja\Http\Routing\Config;
19
use Valkyrja\Http\Routing\Config\Cache;
20
use Valkyrja\Http\Routing\Data\Contract\Route;
21
use Valkyrja\Http\Routing\Exception\InvalidRoutePathException;
22

23
/**
24
 * Class CacheableCollection.
25
 *
26
 * @author Melech Mizrachi
27
 */
28
class CacheableCollection extends Collection
29
{
30
    /**
31
     * Has setup already completed? Used to avoid duplicate setup.
32
     *
33
     * @var bool
34
     */
35
    protected bool $setup = false;
36

37
    /**
38
     * CacheableCollection constructor.
39
     */
40
    public function __construct(
41
        protected Container $container,
42
        protected Config $config
43
    ) {
44
    }
6✔
45

46
    /**
47
     * Setup the collection.
48
     */
49
    public function setup(bool $force = false, bool $useCache = true): void
50
    {
51
        // If route's have already been setup, no need to do it again
52
        if ($this->setup && ! $force) {
6✔
53
            return;
×
54
        }
55

56
        $this->setup = true;
6✔
57

58
        $cache = $this->config->cache;
6✔
59

60
        // If the application should use the routes cache
61
        if ($useCache && $cache !== null) {
6✔
NEW
62
            $this->setupFromCache($cache);
×
63

64
            // Then return out of setup
65
            return;
×
66
        }
67

68
        $this->setupNotCached();
6✔
69
    }
70

71
    /**
72
     * Get a cacheable representation of the collection.
73
     */
74
    public function getCacheable(): Cache
75
    {
76
        $this->setup(true, false);
2✔
77

78
        $config          = new Cache();
2✔
79
        $config->routes  = [];
2✔
80
        $config->static  = $this->static;
2✔
81
        $config->dynamic = $this->dynamic;
2✔
82
        $config->named   = $this->named;
2✔
83

84
        foreach ($this->routes as $id => $route) {
2✔
UNCOV
85
            $config->routes[$id] = serialize($route);
×
86
        }
87

88
        return $config;
2✔
89
    }
90

91
    /**
92
     * Setup from cache.
93
     */
94
    protected function setupFromCache(Cache $cache): void
95
    {
96
        $this->routes  = $cache->routes;
×
UNCOV
97
        $this->static  = $cache->static;
×
98
        $this->dynamic = $cache->dynamic;
×
99
        $this->named   = $cache->named;
×
100
    }
101

102
    /**
103
     * Setup not cache.
104
     */
105
    protected function setupNotCached(): void
106
    {
107
        $this->setupAttributedControllers();
6✔
108
        $this->afterSetup();
6✔
109
    }
110

111
    /**
112
     * Get attributed controllers.
113
     *
114
     * @throws InvalidRoutePathException
115
     */
116
    protected function setupAttributedControllers(): void
117
    {
118
        /** @var Collector $routeAttributes */
119
        $routeAttributes = $this->container->getSingleton(Collector::class);
6✔
120
        $controllers     = $this->config->controllers;
6✔
121

122
        // Get all the attributes routes from the list of controllers
123
        // Iterate through the routes
124
        foreach ($routeAttributes->getRoutes(...$controllers) as $route) {
6✔
125
            // Set the route
UNCOV
126
            $this->add($route);
×
127
        }
128
    }
129

130
    /**
131
     * Do after setup.
132
     *
133
     * @throws InvalidRoutePathException
134
     */
135
    protected function afterSetup(): void
136
    {
137
        $this->dynamic = [];
6✔
138

139
        /** @var Route $route */
140
        foreach ($this->routes as $route) {
6✔
UNCOV
141
            $this->setRouteToRequestMethods($route);
×
142
        }
143
    }
144
}
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