Completed
Push — feature/EVO-7278-security-and-... ( e30f5a...fbbb1a )
by
unknown
09:38
created

GravitonBundleExtension::loadFiles()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 5.0061

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 15
cts 16
cp 0.9375
rs 8.7624
c 0
b 0
f 0
cc 5
eloc 13
nc 5
nop 3
crap 5.0061
1
<?php
2
/**
3
 * Load and manage bundle configuration.
4
 */
5
6
namespace Graviton\BundleBundle\DependencyInjection;
7
8
use Symfony\Component\Config\Definition\ConfigurationInterface;
9
use Symfony\Component\Config\FileLocator;
10
use Symfony\Component\DependencyInjection\ContainerBuilder;
11
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface as PrependInterface;
12
use Symfony\Component\DependencyInjection\Loader;
13
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
14
use Symfony\Component\Finder\Finder;
15
use Symfony\Component\Finder\SplFileInfo;
16
17
/**
18
 * GravitonBundleExtension
19
 *
20
 * To learn more see {@link http://scm.to/004w}
21
 *
22
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
23
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
24
 * @link     http://swisscom.ch
25
 */
26
class GravitonBundleExtension extends Extension implements PrependInterface
27
{
28
    /**
29
     * {@inheritDoc}
30
     *
31
     * @param array            $configs   configs to process
32
     * @param ContainerBuilder $container container to use
33
     *
34
     * @return void
35
     */
36 2
    public function load(array $configs, ContainerBuilder $container)
37
    {
38 2
        $this->loadFiles($this->getConfigDir(), $container, ['services.xml','services.yml']);
39 2
    }
40
41
    /**
42
     * get path to bundles Resources/config dir
43
     *
44
     * @return string
45
     */
46 2
    public function getConfigDir()
47
    {
48 2
        return __DIR__ . '/../Resources/config';
49
    }
50
51
    /**
52
     * {@inheritDoc}
53
     *
54
     * Load additional config into the container.
55
     *
56
     * @param ContainerBuilder $container instance
57
     *
58
     * @return void
59
     */
60 2
    public function prepend(ContainerBuilder $container)
61
    {
62 2
        $this->loadFiles($this->getConfigDir(), $container, ['config.xml','config.yml','parameters.yml']);
63 2
    }
64
65
    /**
66
     * Returns extension configuration.
67
     *
68
     * @param array                                                   $config    An array of configuration values
69
     * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container A ContainerBuilder instance
70
     *
71
     * @return \Symfony\Component\Config\Definition\ConfigurationInterface
72
     * @throws \RuntimeException
73
     */
74
    public function getConfiguration(array $config, ContainerBuilder $container)
75
    {
76
        $configuration = parent::getConfiguration($config, $container);
77
78
        if ($configuration instanceof ConfigurationInterface) {
79
            return $configuration;
80
        }
81
82
        throw new \RuntimeException('Expected configuration class not found!');
83
    }
84
85
    /**
86
     * Made to have in one place the loading
87
     *
88
     * @param string           $dir       folder
89
     * @param ContainerBuilder $container Sf container
90
     * @param array            $allowed   array of files to load
91
     *
92
     * @return void
93
     */
94 2
    private function loadFiles($dir, $container, $allowed)
95
    {
96 2
        $locator = new FileLocator($dir);
97 2
        $xmlLoader = new Loader\XmlFileLoader($container, $locator);
98 2
        $ymlLoader = new Loader\YamlFileLoader($container, $locator);
99
100 2
        $finder = new Finder();
101 2
        $finder->files()->in($dir);
102
103
        /** @var SplFileInfo $file */
104 2
        foreach ($finder as $file) {
105 2
            if (!in_array($file->getFilename(), $allowed)) {
106 2
                continue;
107
            }
108 2
            if ('xml' == $file->getExtension()) {
109 2
                $xmlLoader->load($file->getRealPath());
110 1
            } elseif ('yml' == $file->getExtension()) {
111 1
                $ymlLoader->load($file->getRealPath());
112
            }
113 1
        }
114 2
    }
115
}
116