Completed
Push — master ( 123852...550d67 )
by
unknown
09:16
created

GravitonBundleExtension::getConfiguration()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 5
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 2
crap 6
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
     * Load config files, xml or yml.
87
     * If will only include the config file if it's in the allowed array.
88
     *
89
     * @param string           $dir       folder
90
     * @param ContainerBuilder $container Sf container
91
     * @param array            $allowed   array of files to load
92
     *
93
     * @return void
94
     */
95 2
    private function loadFiles($dir, ContainerBuilder $container, array $allowed)
96
    {
97 2
        $locator = new FileLocator($dir);
98 2
        $xmlLoader = new Loader\XmlFileLoader($container, $locator);
99 2
        $ymlLoader = new Loader\YamlFileLoader($container, $locator);
100
101 2
        $finder = new Finder();
102 2
        $finder->files()->in($dir);
103
104
        /** @var SplFileInfo $file */
105 2
        foreach ($finder as $file) {
106 2
            if (!in_array($file->getFilename(), $allowed)) {
107 2
                continue;
108
            }
109 2
            if ('xml' == $file->getExtension()) {
110 2
                $xmlLoader->load($file->getRealPath());
111 1
            } elseif ('yml' == $file->getExtension()) {
112 1
                $ymlLoader->load($file->getRealPath());
113
            }
114 1
        }
115 2
    }
116
}
117