Completed
Push — master ( d9d7ff...fc66c8 )
by ANTHONIUS
19s queued 11s
created

ModuleLoader::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 4
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the ModularBundle project.
5
 *
6
 * (c) Anthonius Munthi <https://itstoni.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
declare(strict_types=1);
13
14
namespace Doyo\Bundle\Modular\Routing;
15
16
use Doyo\Bundle\Modular\Application\ModuleInterface;
17
use Symfony\Component\Config\Loader\Loader;
18
use Symfony\Component\HttpKernel\KernelInterface;
19
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
20
use Symfony\Component\Routing\Loader\ContainerLoader;
21
use Symfony\Component\Routing\Loader\PhpFileLoader as RoutingPhpFileLoader;
22
use Symfony\Component\Routing\RouteCollection;
23
24
class ModuleLoader extends Loader
25
{
26
    private bool $loaded = false;
27
    private KernelInterface $kernel;
28
29
    /**
30
     * @var iterable<array-key,ModuleInterface>|ModuleInterface[]
31
     */
32
    private iterable $modules;
33
    private ContainerLoader $loader;
34
35
    /**
36
     * @param iterable<array-key,ModuleInterface> $modules
37
     */
38
    public function __construct(
39
        string $env,
40
        KernelInterface $kernel,
41
        ContainerLoader $loader,
42
        iterable $modules
43
    ) {
44
        parent::__construct($env);
45
46
        $this->kernel  = $kernel;
47
        $this->loader  = $loader;
48
        $this->modules = $modules;
49
    }
50
51
    /**
52
     * @psalm-suppress PossiblyFalseArgument
53
     * @psalm-suppress ArgumentTypeCoercion
54
     * @psalm-suppress UndefinedInterfaceMethod
55
     * @psalm-suppress PossiblyFalseReference
56
     */
57
    public function load($resource, string $type = null): RouteCollection
58
    {
59
        // @codeCoverageIgnoreStart
60
        if (true === $this->loaded) {
61
            throw new \RuntimeException('Do not add the "extra" loader twice');
62
        }
63
        /** @codeCoverageIgnoreEnd */
64
        $loader = $this->loader;
65
        $file   = (new \ReflectionObject($this->kernel))->getFileName();
66
        /** @psalm-var RoutingPhpFileLoader $kernelLoader */
67
        $kernelLoader = $loader->getResolver()->resolve($file, 'php');
68
        $kernelLoader->setCurrentDir(\dirname($file));
0 ignored issues
show
Bug introduced by
The method setCurrentDir() does not exist on Symfony\Component\Config\Loader\LoaderInterface. It seems like you code against a sub-type of Symfony\Component\Config\Loader\LoaderInterface such as Symfony\Component\Config\Loader\FileLoader. ( Ignorable by Annotation )

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

68
        $kernelLoader->/** @scrutinizer ignore-call */ 
69
                       setCurrentDir(\dirname($file));
Loading history...
69
        $collection   = new RouteCollection();
70
        $configurator = new RoutingConfigurator($collection, $kernelLoader, $file, $file, $this->env);
0 ignored issues
show
Bug introduced by
It seems like $kernelLoader can also be of type false; however, parameter $loader of Symfony\Component\Routin...igurator::__construct() does only seem to accept Symfony\Component\Routing\Loader\PhpFileLoader, maybe add an additional type check? ( Ignorable by Annotation )

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

70
        $configurator = new RoutingConfigurator($collection, /** @scrutinizer ignore-type */ $kernelLoader, $file, $file, $this->env);
Loading history...
71
72
        foreach ($this->modules as $module) {
73
            $this->loadRoute($configurator, $module);
74
        }
75
76
        $this->loaded = true;
77
78
        return $collection;
79
    }
80
81
    public function supports($resource, string $type = null): bool
82
    {
83
        return 'doyo_modular' === $type;
84
    }
85
86
    private function loadRoute(RoutingConfigurator $configurator, ModuleInterface $module): void
87
    {
88
        $routePath = $module->getBasePath().'/Resources/routes';
89
        if (is_dir($routePath)) {
90
            $configurator->import($routePath.'/*.yaml');
91
            $configurator->import($routePath.'/*.xml');
92
        }
93
    }
94
}
95