EnumExtension::load()   A
last analyzed

Complexity

Conditions 5
Paths 12

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 9.2088
c 0
b 0
f 0
cc 5
nc 12
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yokai\EnumBundle\DependencyInjection;
6
7
use Symfony\Bundle\TwigBundle\TwigBundle;
8
use Symfony\Component\Config\FileLocator;
9
use Symfony\Component\DependencyInjection\ContainerBuilder;
10
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
11
use Symfony\Component\Form\FormInterface;
12
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
13
use Symfony\Component\Validator\Validator\ValidatorInterface;
14
use Yokai\EnumBundle\EnumInterface;
15
16
/**
17
 * @author Yann Eugoné <[email protected]>
18
 */
19
class EnumExtension extends Extension
20
{
21
    /**
22
     * @inheritdoc
23
     */
24
    public function load(array $configs, ContainerBuilder $container): void
25
    {
26
        $xmlLoader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
27
        $xmlLoader->load('enum.xml');
28
29
        $requiresForm = interface_exists(FormInterface::class);
30
        $requiresValidator = interface_exists(ValidatorInterface::class);
31
        $requiresTwig = class_exists(TwigBundle::class);
32
33
        if ($requiresForm) {
34
            $xmlLoader->load('form.xml');
35
            if (!$requiresValidator) {
36
                $container->removeDefinition('form_extention.type_guesser.enum');
37
            }
38
        }
39
        if ($requiresValidator) {
40
            $xmlLoader->load('validator.xml');
41
        }
42
        if ($requiresTwig) {
43
            $xmlLoader->load('twig.xml');
44
        }
45
46
        $container->registerForAutoconfiguration(EnumInterface::class)
47
            ->addTag('enum');
48
    }
49
}
50