Completed
Push — master ( 9bdeb9...0e1984 )
by Craig
11:49 queued 06:20
created

Kernel::configureContainer()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 28
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 14
nc 4
nop 2
dl 0
loc 28
rs 9.7998
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Zikula package.
7
 *
8
 * Copyright Zikula Foundation - https://ziku.la/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
15
use Symfony\Component\Config\Loader\LoaderInterface;
16
use Symfony\Component\Config\Resource\FileResource;
17
use Symfony\Component\DependencyInjection\ContainerBuilder;
18
//use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
19
//use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
20
use Symfony\Component\Routing\RouteCollectionBuilder;
21
use Zikula\Bundle\CoreBundle\DynamicConfigDumper;
22
use Zikula\Bundle\CoreBundle\Helper\PersistedBundleHelper;
23
use Zikula\Bundle\CoreBundle\HttpKernel\ZikulaKernel;
24
25
class Kernel extends ZikulaKernel
26
{
27
    use MicroKernelTrait;
28
29
    public function registerBundles(): iterable
30
    {
31
        $bundleHelper = new PersistedBundleHelper();
32
        $bundles = require $this->getProjectDir() . '/config/bundles.php';
33
        $bundleHelper->getPersistedBundles($this, $bundles);
34
        foreach ($bundles as $class => $envs) {
35
            if ($envs[$this->environment] ?? $envs['all'] ?? false) {
36
                yield new $class();
0 ignored issues
show
Bug Best Practice introduced by
The expression yield new $class() returns the type Generator which is incompatible with the return type mandated by Symfony\Component\HttpKe...face::registerBundles() of Symfony\Component\HttpKe...dleInterface[]|iterable.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
37
            }
38
        }
39
    }
40
41
    // could be remove when upgrading Symfony to 5.1.0 (wanted?)
42
    public function getProjectDir(): string
43
    {
44
        return dirname(__DIR__);
45
    }
46
47
    // use new signature when upgrading Symfony to 5.1.0
48
    //protected function configureContainer(ContainerConfigurator $container): void
49
    protected function configureContainer(ContainerBuilder $container, LoaderInterface $loader): void
50
    {
51
        /** use with Symfony 5.1.0
52
        $container->import('../config/{packages}/*.yaml');
53
        $container->import('../config/{packages}/' . $this->environment . '/*.yaml');
54
        $container->import('../config/{services}.yaml');
55
        $container->import('../config/{services}_' . $this->environment . '.yaml');
56
         */
57
        $container->addResource(new FileResource($this->getProjectDir() . '/config/bundles.php'));
58
        $container->setParameter('container.dumper.inline_class_loader', $this->debug);
59
        $container->setParameter('container.dumper.inline_factories', true);
60
        $configDir = $this->getProjectDir() . '/config/';
61
62
        $loader->load($configDir . '{packages}/*.yaml', 'glob');
63
        $loader->load($configDir . '{packages}/' . $this->environment . '/*.yaml', 'glob');
64
        $loader->load($configDir . '{services}.yaml', 'glob');
65
        $loader->load($configDir . '{services}_' . $this->environment . '.yaml', 'glob');
66
67
        if (is_readable($configDir . 'services_custom.yaml')) {
68
            $loader->load($configDir . 'services_custom.yaml');
69
        }
70
71
        if (!is_readable($configDir . DynamicConfigDumper::CONFIG_GENERATED)) {
72
            // There is no generated configuration (yet), load default values.
73
            // This only happens at the very first time Symfony is started.
74
            $loader->load($configDir . DynamicConfigDumper::CONFIG_DEFAULT);
75
        } else {
76
            $loader->load($configDir . DynamicConfigDumper::CONFIG_GENERATED);
77
        }
78
    }
79
80
    // use new signature when upgrading Symfony to 5.1.0
81
    //protected function configureRoutes(RoutingConfigurator $routes): void
82
    protected function configureRoutes(RouteCollectionBuilder $routes): void
83
    {
84
        $configDir = $this->getProjectDir() . '/config/';
85
86
        /** use with Symfony 5.1.0
87
        $routes->import($configDir . '{routes}/' . $this->environment . '/*.yaml');
88
        $routes->import($configDir . '{routes}/*.yaml');
89
        $routes->import($configDir . '{routes}.yaml');
90
         */
91
92
        $routes->import($configDir . '{routes}/' . $this->environment . '/*.yaml', '/', 'glob');
93
        $routes->import($configDir . '{routes}/*.yaml', '/', 'glob');
94
        $routes->import($configDir . '{routes}.yaml', '/', 'glob');
95
    }
96
}
97