AbstractResourceExtension::load()   C
last analyzed

Complexity

Conditions 8
Paths 48

Size

Total Lines 31
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 1
Metric Value
c 4
b 0
f 1
dl 0
loc 31
rs 5.3846
cc 8
eloc 17
nc 48
nop 2
1
<?php
2
3
namespace DoS\ResourceBundle\DependencyInjection;
4
5
use Sylius\Bundle\ResourceBundle\DependencyInjection\Extension\AbstractResourceExtension as BaseAbstractResourceExtension;
6
use Symfony\Component\Config\Definition\ConfigurationInterface;
7
use Symfony\Component\Config\FileLocator;
8
use Symfony\Component\Config\Loader\DelegatingLoader;
9
use Symfony\Component\Config\Loader\LoaderResolver;
10
use Symfony\Component\DependencyInjection\ContainerBuilder;
11
use Symfony\Component\DependencyInjection\Definition;
12
use Symfony\Component\DependencyInjection\Exception\BadMethodCallException;
13
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
14
use Symfony\Component\DependencyInjection\Reference;
15
16
abstract class AbstractResourceExtension extends BaseAbstractResourceExtension
17
{
18
    protected $applicationName = 'dos';
19
20
    protected $configFiles = array(
21
        'parameters.yml',
22
        'services.yml',
23
        'providers.yml',
24
        'controllers.yml',
25
        'templatings.yml',
26
        'listeners.yml',
27
        'settings.yml',
28
        'callbacks.yml',
29
        'forms.yml',
30
        'mails.yml',
31
        'menus.yml',
32
        'twigs.yml',
33
        'workers.yml',
34
    );
35
36
    /**
37
     * DoS translate to Dos preventing Container::underscore => do_s_.
38
     *
39
     * {@inheritdoc}
40
     */
41
    public function getAlias()
42
    {
43
        $className = get_class($this);
44
45
        if (substr($className, -9) != 'Extension') {
46
            throw new BadMethodCallException('This extension does not follow the naming convention; you must overwrite the getAlias() method.');
47
        }
48
49
        return AbstractResourceBundle::expectedAlias(substr(strrchr($className, '\\'), 1, -9));
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function load(array $config, ContainerBuilder $container)
56
    {
57
        $config = $this->processConfiguration($this->getBundleConfiguration(), $config);
58
        $loader = new YamlFileLoader($container, new FileLocator($this->getConfigDir()));
59
60
        if (empty($config['resources'])) {
61
            $config['resources'] = array();
62
        }
63
64
        if (isset($config['driver'])) {
65
            $this->registerResources($this->applicationName, $config['driver'], $config['resources'], $container);
66
        }
67
68
        foreach($config['resources'] as $model => $resource) {
69
            foreach($resource['classes'] as $key => $class) {
70
                if ($key === 'provider') {
71
                    $name = sprintf('%s.%s.%s.class', $this->applicationName, $key, $model);
72
                    $container->setParameter($name, $class);
73
                    $this->addProvider($container, $class, $model);
74
                }
75
            }
76
        }
77
78
        foreach ($this->configFiles as $configFile) {
79
            if (file_exists(sprintf('%s/%s', $this->getConfigDir(), $configFile))) {
80
                $loader->load($configFile);
81
            }
82
        }
83
84
        return $config;
85
    }
86
87
    protected function addProvider(ContainerBuilder $container, $providerClass, $modelName)
88
    {
89
        $providerReflection = new \ReflectionClass($providerClass);
90
        $definition = new Definition($providerClass);
91
92
        $definition->setArguments([
93
            new Reference(sprintf('%s.repository.%s', $this->applicationName, $modelName)),
94
            new Reference(sprintf('%s.factory.%s', $this->applicationName, $modelName)),
95
        ]);
96
97
        $definition->setLazy(!$providerReflection->isFinal());
98
        $container->setDefinition(sprintf('%s.provider.%s', $this->applicationName, $modelName), $definition);
99
    }
100
101
    /**
102
     * @return string
103
     */
104
    protected function getConfigDir()
105
    {
106
        $reflector = new \ReflectionClass($this);
107
        $fileName = $reflector->getFileName();
108
109
        return sprintf('%s/../Resources/config', dirname($fileName));
110
    }
111
112
    /**
113
     * @return ConfigurationInterface
114
     */
115
    abstract protected function getBundleConfiguration();
116
}
117