Passed
Push — master ( 6534fd...eb8cff )
by Caen
12:05 queued 13s
created

RouteCollection::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Hyde\Framework\Foundation;
4
5
use Hyde\Framework\Contracts\HydeKernelContract;
6
use Hyde\Framework\Contracts\PageContract;
7
use Hyde\Framework\Contracts\RouteContract;
8
use Hyde\Framework\Models\Route;
9
use Illuminate\Support\Collection;
10
11
/**
12
 * Pseudo-Router for Hyde.
13
 *
14
 * @see \Hyde\Framework\Foundation\PageCollection
15
 * @see \Hyde\Framework\Testing\Feature\RouteTest
16
 *
17
 * This is not a router in the traditional sense that it decides where to go.
18
 * Instead, it creates a pre-generated object encapsulating the Hyde autodiscovery.
19
 *
20
 * This not only let us emulate Laravel route helpers, but also serve as the
21
 * canonical source of truth for the vital HydePHP autodiscovery process.
22
 *
23
 * The routes defined can then also be used to power the RealtimeCompiler without
24
 * having to reverse-engineer the source file mapping.
25
 *
26
 * Routes are not intended to be added manually, instead the route index is created using
27
 * the exact same rules as the current autodiscovery process and compiled file output.
28
 *
29
 * The route index serves as a multidimensional mapping allowing you to
30
 * determine where a source file will be compiled to, and where a compiled
31
 * file was generated from.
32
 */
33
final class RouteCollection extends Collection
34
{
35
    protected HydeKernelContract $kernel;
36
37
    public static function boot(HydeKernelContract $kernel): self
38
    {
39
        return (new self())->setKernel($kernel)->discoverRoutes();
40
    }
41
42
    protected function __construct($items = [])
43
    {
44
        parent::__construct($items);
45
    }
46
47
    protected function setKernel(HydeKernelContract $kernel): self
48
    {
49
        $this->kernel = $kernel;
50
51
        return $this;
52
    }
53
54
    public function getRoutes(?string $pageClass = null): self
55
    {
56
        return ! $pageClass ? $this : $this->filter(function (RouteContract $route) use ($pageClass) {
57
            return $route->getSourceModel() instanceof $pageClass;
58
        });
59
    }
60
61
    /**
62
     * This internal method adds the specified route to the route index.
63
     * It's made public so package developers can hook into the routing system.
64
     */
65
    public function addRoute(RouteContract $route): self
66
    {
67
        $this->put($route->getRouteKey(), $route);
0 ignored issues
show
Bug introduced by
$route->getRouteKey() of type string is incompatible with the type Illuminate\Support\TKey expected by parameter $key of Illuminate\Support\Collection::put(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

67
        $this->put(/** @scrutinizer ignore-type */ $route->getRouteKey(), $route);
Loading history...
68
69
        return $this;
70
    }
71
72
    protected function discover(PageContract $page): self
73
    {
74
        // Create a new route for the given page, and add it to the index.
75
        $this->addRoute(new Route($page));
76
77
        return $this;
78
    }
79
80
    protected function discoverRoutes(): self
81
    {
82
        $this->kernel->pages()->each(function (PageContract $page) {
0 ignored issues
show
Bug introduced by
The method pages() does not exist on Hyde\Framework\Contracts\HydeKernelContract. Since it exists in all sub-types, consider adding an abstract or default implementation to Hyde\Framework\Contracts\HydeKernelContract. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

82
        $this->kernel->/** @scrutinizer ignore-call */ 
83
                       pages()->each(function (PageContract $page) {
Loading history...
83
            $this->discover($page);
84
        });
85
86
        return $this;
87
    }
88
}
89