Completed
Push — feature/EVO-7278-security-and-... ( 0e0414...52054d )
by
unknown
65:56
created

GravitonBundleExtension::loadFiles()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 0
cts 0
cp 0
rs 8.7972
c 0
b 0
f 0
cc 4
eloc 15
nc 4
nop 3
crap 20
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 2
     * @return void
35
     */
36 2
    public function load(array $configs, ContainerBuilder $container)
37 1
    {
38 2
        $this->loadFiles($this->getConfigDir(), $container, ['services.xml','services.yml']);
39 1
    }
40 2
41 2
    /**
42
     * get path to bundles Resources/config dir
43
     *
44
     * @return string
45
     */
46
    public function getConfigDir()
47
    {
48 2
        return __DIR__ . '/../Resources/config';
49
    }
50 2
51
    /**
52
     * {@inheritDoc}
53
     *
54
     * Load additional config into the container.
55
     *
56
     * @param ContainerBuilder $container instance
57
     *
58
     * @return void
59
     */
60
    public function prepend(ContainerBuilder $container)
61
    {
62 2
        $this->loadFiles($this->getConfigDir(), $container, ['config.xml','config.yml','parameters.yml']);
63
    }
64 2
65 1
    /**
66 2
     * Returns extension configuration.
67 1
     *
68 2
     * @param array                                                   $config    An array of configuration values
69 2
     * @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
    private function loadFiles($dir, $container, $allowed)
93
    {
94
        $locator = new FileLocator($dir);
95
        $xmlLoader = new Loader\XmlFileLoader($container, $locator);
96
        $ymlLoader = new Loader\YamlFileLoader($container, $locator);
97
98
        $finder = new Finder();
99
        $finder->files()->in($dir);
100
101
        /** @var SplFileInfo $file */
102
        foreach ($finder as $file) {
103
            $name = $file->getFilename();
104
            $path = $file->getRealPath();
105
            if (!in_array($name, $allowed)) {
106
                continue;
107
            }
108
            try {
109
                $xmlLoader->load($path);
110
            } catch (\InvalidArgumentException $e) {
111
                $ymlLoader->load($path);
112
            }
113
        }
114
    }
115
}
116