Completed
Push — 1.4-env-changes ( 8d9e7c )
by Kamil
32:55
created

Kernel::getContainerLoader()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.6666
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
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 App;
15
16
use PSS\SymfonyMockerContainer\DependencyInjection\MockerContainer;
17
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
18
use Symfony\Component\Config\Loader\DelegatingLoader;
19
use Symfony\Component\Config\Loader\LoaderInterface;
20
use Symfony\Component\Config\Loader\LoaderResolver;
21
use Symfony\Component\Config\Resource\FileResource;
22
use Symfony\Component\DependencyInjection\ContainerBuilder;
23
use Symfony\Component\DependencyInjection\ContainerInterface;
24
use Symfony\Component\DependencyInjection\Loader\ClosureLoader;
25
use Symfony\Component\DependencyInjection\Loader\DirectoryLoader;
26
use Symfony\Component\DependencyInjection\Loader\GlobFileLoader;
27
use Symfony\Component\DependencyInjection\Loader\IniFileLoader;
28
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
29
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
30
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
31
use Symfony\Component\HttpKernel\Config\FileLocator;
32
use Symfony\Component\HttpKernel\Kernel as BaseKernel;
33
use Symfony\Component\Routing\RouteCollectionBuilder;
34
use Webmozart\Assert\Assert;
35
36
/** @final */
37
class Kernel extends BaseKernel
38
{
39
    use MicroKernelTrait;
40
41
    private const CONFIG_EXTS = '.{php,xml,yaml,yml}';
42
43
    public function getCacheDir(): string
44
    {
45
        return $this->getProjectDir() . '/var/cache/' . $this->environment;
46
    }
47
48
    public function getLogDir(): string
49
    {
50
        return $this->getProjectDir() . '/var/log';
51
    }
52
53
    public function registerBundles(): iterable
54
    {
55
        $contents = require $this->getProjectDir() . '/config/bundles.php';
56
        foreach ($contents as $class => $envs) {
57
            if (isset($envs['all']) || isset($envs[$this->environment])) {
58
                yield new $class();
59
            }
60
        }
61
    }
62
63
    protected function configureContainer(ContainerBuilder $container, LoaderInterface $loader): void
64
    {
65
        $container->addResource(new FileResource($this->getProjectDir() . '/config/bundles.php'));
66
        $container->setParameter('container.dumper.inline_class_loader', true);
67
        $confDir = $this->getProjectDir() . '/config';
68
69
        $loader->load($confDir . '/{packages}/*' . self::CONFIG_EXTS, 'glob');
70
        $loader->load($confDir . '/{packages}/' . $this->environment . '/**/*' . self::CONFIG_EXTS, 'glob');
71
        $loader->load($confDir . '/{services}' . self::CONFIG_EXTS, 'glob');
72
        $loader->load($confDir . '/{services}_' . $this->environment . self::CONFIG_EXTS, 'glob');
73
    }
74
75
    protected function configureRoutes(RouteCollectionBuilder $routes): void
76
    {
77
        $confDir = $this->getProjectDir() . '/config';
78
79
        $routes->import($confDir . '/{routes}/*' . self::CONFIG_EXTS, '/', 'glob');
80
        $routes->import($confDir . '/{routes}/' . $this->environment . '/**/*' . self::CONFIG_EXTS, '/', 'glob');
81
        $routes->import($confDir . '/{routes}' . self::CONFIG_EXTS, '/', 'glob');
82
    }
83
84
    protected function getContainerBaseClass(): string
85
    {
86
        if ($this->isTestEnvironment()) {
87
            return MockerContainer::class;
88
        }
89
90
        return parent::getContainerBaseClass();
91
    }
92
93
    protected function getContainerLoader(ContainerInterface $container): LoaderInterface
94
    {
95
        /** @var ContainerBuilder $container */
96
        Assert::isInstanceOf($container, ContainerBuilder::class);
97
98
        $locator = new FileLocator($this, $this->getRootDir() . '/Resources');
0 ignored issues
show
Deprecated Code introduced by
The method Symfony\Component\HttpKernel\Kernel::getRootDir() has been deprecated with message: since Symfony 4.2, use getProjectDir() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
99
        $resolver = new LoaderResolver([
100
            new XmlFileLoader($container, $locator),
101
            new YamlFileLoader($container, $locator),
102
            new IniFileLoader($container, $locator),
103
            new PhpFileLoader($container, $locator),
104
            new GlobFileLoader($container, $locator),
105
            new DirectoryLoader($container, $locator),
106
            new ClosureLoader($container),
107
        ]);
108
109
        return new DelegatingLoader($resolver);
110
    }
111
112
    private function isTestEnvironment(): bool
113
    {
114
        return 0 === strpos($this->getEnvironment(), 'test');
115
    }
116
}
117