Completed
Pull Request — master (#14)
by Jeff
09:20
created

Kernel::configureRoutes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 7
rs 10
1
<?php
2
3
declare(strict_types=1);
4
/**
5
 * This file is part of the mailserver-admin package.
6
 * (c) Jeffrey Boehm <https://github.com/jeboehm/mailserver-admin>
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace App;
12
13
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
14
use Symfony\Component\Config\Loader\LoaderInterface;
15
use Symfony\Component\Config\Resource\FileResource;
16
use Symfony\Component\DependencyInjection\ContainerBuilder;
17
use Symfony\Component\HttpKernel\Kernel as BaseKernel;
18
use Symfony\Component\Routing\RouteCollectionBuilder;
19
20
class Kernel extends BaseKernel
21
{
22
    use MicroKernelTrait;
23
24
    private const CONFIG_EXTS = '.{php,xml,yaml,yml}';
25
26
    public function getCacheDir(): string
27
    {
28
        return $this->getProjectDir() . '/var/cache/' . $this->environment;
29
    }
30
31
    public function getLogDir(): string
32
    {
33
        return $this->getProjectDir() . '/var/log';
34
    }
35
36
    public function registerBundles()
37
    {
38
        $contents = require $this->getProjectDir() . '/config/bundles.php';
39
        foreach ($contents as $class => $envs) {
40
            if (isset($envs['all']) || isset($envs[$this->environment])) {
41
                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...
42
            }
43
        }
44
    }
45
46
    protected function configureContainer(ContainerBuilder $container, LoaderInterface $loader): void
47
    {
48
        $container->addResource(new FileResource($this->getProjectDir() . '/config/bundles.php'));
49
        // Feel free to remove the "container.autowiring.strict_mode" parameter
50
        // if you are using symfony/dependency-injection 4.0+ as it's the default behavior
51
        $container->setParameter('container.autowiring.strict_mode', true);
52
        $container->setParameter('container.dumper.inline_class_loader', true);
53
54
        $confDir = $this->getProjectDir() . '/config';
55
56
        $loader->load($confDir . '/{packages}/*' . self::CONFIG_EXTS, 'glob');
57
        $loader->load($confDir . '/{packages}/' . $this->environment . '/**/*' . self::CONFIG_EXTS, 'glob');
58
        $loader->load($confDir . '/{services}' . self::CONFIG_EXTS, 'glob');
59
        $loader->load($confDir . '/{services}_' . $this->environment . self::CONFIG_EXTS, 'glob');
60
    }
61
62
    protected function configureRoutes(RouteCollectionBuilder $routes): void
63
    {
64
        $confDir = $this->getProjectDir() . '/config';
65
66
        $routes->import($confDir . '/{routes}/*' . self::CONFIG_EXTS, '/', 'glob');
67
        $routes->import($confDir . '/{routes}/' . $this->environment . '/**/*' . self::CONFIG_EXTS, '/', 'glob');
68
        $routes->import($confDir . '/{routes}' . self::CONFIG_EXTS, '/', 'glob');
69
    }
70
}
71