Passed
Pull Request — master (#2)
by ANTHONIUS
03:55
created

ModuleLoader::supports()   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 2
dl 0
loc 3
rs 10
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\Config\Loader\LoaderResolver;
19
use Symfony\Component\HttpKernel\KernelInterface;
20
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
21
use Symfony\Component\Routing\Loader\ContainerLoader;
22
use Symfony\Component\Routing\Loader\PhpFileLoader as RoutingPhpFileLoader;
23
use Symfony\Component\Routing\RouteCollection;
24
25
class ModuleLoader extends Loader
26
{
27
    private bool $loaded = false;
28
    private KernelInterface $kernel;
29
30
    /**
31
     * @var iterable<array-key,ModuleInterface>|ModuleInterface[]
32
     */
33
    private iterable $modules;
34
    private ContainerLoader $loader;
35
36
    /**
37
     * @param iterable<array-key,ModuleInterface> $modules
38
     */
39
    public function __construct(
40
        string $env,
41
        KernelInterface $kernel,
42
        ContainerLoader $loader,
43
        iterable $modules
44
    )
45
    {
46
        parent::__construct($env);
47
48
        $this->kernel = $kernel;
49
        $this->loader = $loader;
50
        $this->modules = $modules;
51
    }
52
53
    /**
54
     * @psalm-suppress PossiblyFalseArgument
55
     * @psalm-suppress ArgumentTypeCoercion
56
     * @psalm-suppress UndefinedInterfaceMethod
57
     * @psalm-suppress PossiblyFalseReference
58
     */
59
    public function load($resource, string $type = null): RouteCollection
60
    {
61
        if (true === $this->loaded) {
62
            throw new \RuntimeException('Do not add the "extra" loader twice');
63
        }
64
65
        $loader = $this->loader;
66
        $file = (new \ReflectionObject($this->kernel))->getFileName();
67
        /* @psalm-var RoutingPhpFileLoader $kernelLoader */
68
        $kernelLoader = $loader->getResolver()->resolve($file, 'php');
69
        assert($kernelLoader instanceof RoutingPhpFileLoader);
70
        $kernelLoader->setCurrentDir(\dirname($file));
71
        $collection = new RouteCollection();
72
        $configurator = new RoutingConfigurator($collection, $kernelLoader, $file, $file, $this->env);
73
74
75
        foreach($this->modules as $module){
76
            $this->loadRoute($configurator, $module);
77
        }
78
79
        return $collection;
80
    }
81
82
    public function supports($resource, string $type = null): bool
83
    {
84
        return 'modular' === $type;
85
    }
86
87
    private function loadRoute(RoutingConfigurator $configurator, ModuleInterface $module): void
88
    {
89
        $routePath = $module->getBasePath().'/Resources/routes';
90
        if(is_dir($routePath)){
91
            $configurator->import($routePath.'/*.yaml');
92
            $configurator->import($routePath.'/*.xml');
93
        }
94
    }
95
96
}
97