Completed
Push — 1.4-autowiring-for-resource-bu... ( cfe966...176999 )
by Kamil
09:09
created

Kernel::cleanupContainer()   B

Complexity

Conditions 8
Paths 14

Size

Total Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 37
rs 8.0835
c 0
b 0
f 0
cc 8
nc 14
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
use ProxyManager\Proxy\VirtualProxyInterface;
15
use PSS\SymfonyMockerContainer\DependencyInjection\MockerContainer;
16
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
17
use Symfony\Component\Config\Loader\DelegatingLoader;
18
use Symfony\Component\Config\Loader\LoaderInterface;
19
use Symfony\Component\Config\Loader\LoaderResolver;
20
use Symfony\Component\Config\Resource\FileResource;
21
use Symfony\Component\DependencyInjection\ContainerBuilder;
22
use Symfony\Component\DependencyInjection\ContainerInterface;
23
use Symfony\Component\DependencyInjection\Loader\ClosureLoader;
24
use Symfony\Component\DependencyInjection\Loader\DirectoryLoader;
25
use Symfony\Component\DependencyInjection\Loader\GlobFileLoader;
26
use Symfony\Component\DependencyInjection\Loader\IniFileLoader;
27
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
28
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
29
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
30
use Symfony\Component\HttpKernel\Config\FileLocator;
31
use Symfony\Component\HttpKernel\Kernel as BaseKernel;
32
use Symfony\Component\Routing\RouteCollectionBuilder;
33
use Webmozart\Assert\Assert;
34
35
/** @final */
36
class Kernel extends BaseKernel
37
{
38
    use MicroKernelTrait;
39
40
    private const CONFIG_EXTS = '.{php,xml,yaml,yml}';
41
42
    public function getCacheDir(): string
43
    {
44
        return $this->getProjectDir() . '/var/cache/' . $this->environment;
45
    }
46
47
    public function getLogDir(): string
48
    {
49
        return $this->getProjectDir() . '/var/log';
50
    }
51
52
    public function registerBundles(): iterable
53
    {
54
        $contents = require $this->getProjectDir() . '/config/bundles.php';
55
        foreach ($contents as $class => $envs) {
56
            if (isset($envs['all']) || isset($envs[$this->environment])) {
57
                yield new $class();
58
            }
59
        }
60
    }
61
62
    protected function configureContainer(ContainerBuilder $container, LoaderInterface $loader): void
63
    {
64
        $container->addResource(new FileResource($this->getProjectDir() . '/config/bundles.php'));
65
        $container->setParameter('container.dumper.inline_class_loader', true);
66
        $confDir = $this->getProjectDir() . '/config';
67
68
        $loader->load($confDir . '/{packages}/*' . self::CONFIG_EXTS, 'glob');
69
        $loader->load($confDir . '/{packages}/' . $this->environment . '/**/*' . self::CONFIG_EXTS, 'glob');
70
        $loader->load($confDir . '/{services}' . self::CONFIG_EXTS, 'glob');
71
        $loader->load($confDir . '/{services}_' . $this->environment . self::CONFIG_EXTS, 'glob');
72
    }
73
74
    protected function configureRoutes(RouteCollectionBuilder $routes): void
75
    {
76
        $confDir = $this->getProjectDir() . '/config';
77
78
        $routes->import($confDir . '/{routes}/*' . self::CONFIG_EXTS, '/', 'glob');
79
        $routes->import($confDir . '/{routes}/' . $this->environment . '/**/*' . self::CONFIG_EXTS, '/', 'glob');
80
        $routes->import($confDir . '/{routes}' . self::CONFIG_EXTS, '/', 'glob');
81
    }
82
83
    protected function getContainerBaseClass(): string
84
    {
85
        if ($this->isTestEnvironment()) {
86
            return MockerContainer::class;
87
        }
88
89
        return parent::getContainerBaseClass();
90
    }
91
92
    protected function getContainerLoader(ContainerInterface $container): LoaderInterface
93
    {
94
        /** @var ContainerBuilder $container */
95
        Assert::isInstanceOf($container, ContainerBuilder::class);
96
97
        $locator = new FileLocator($this, $this->getRootDir() . '/Resources');
98
        $resolver = new LoaderResolver([
99
            new XmlFileLoader($container, $locator),
100
            new YamlFileLoader($container, $locator),
101
            new IniFileLoader($container, $locator),
102
            new PhpFileLoader($container, $locator),
103
            new GlobFileLoader($container, $locator),
104
            new DirectoryLoader($container, $locator),
105
            new ClosureLoader($container),
106
        ]);
107
108
        return new DelegatingLoader($resolver);
109
    }
110
111
    private function isTestEnvironment(): bool
112
    {
113
        return 0 === strpos($this->getEnvironment(), 'test');
114
    }
115
}
116