Completed
Push — master ( d2fb93...a24186 )
by Narcotic
26:10 queued 11:13
created

GravitonBundleExtension::loadFiles()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 5.009

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 13
cts 14
cp 0.9286
rs 8.7624
c 0
b 0
f 0
cc 5
eloc 13
nc 5
nop 3
crap 5.009
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
0 ignored issues
show
Documentation introduced by
Should the return type not be ConfigurationInterface|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
72
     */
73 2
    public function getConfiguration(array $config, ContainerBuilder $container)
74
    {
75 2
        return parent::getConfiguration($config, $container);
76
    }
77
78
    /**
79
     * Load config files, xml or yml.
80
     * If will only include the config file if it's in the allowed array.
81
     *
82
     * @param string           $dir       folder
83
     * @param ContainerBuilder $container Sf container
84
     * @param array            $allowed   array of files to load
85
     *
86
     * @return void
87
     */
88 2
    private function loadFiles($dir, ContainerBuilder $container, array $allowed)
89
    {
90 2
        $locator = new FileLocator($dir);
91 2
        $xmlLoader = new Loader\XmlFileLoader($container, $locator);
92 2
        $ymlLoader = new Loader\YamlFileLoader($container, $locator);
93
94 2
        $finder = new Finder();
95 2
        $finder->files()->in($dir);
96
97
        /** @var SplFileInfo $file */
98 2
        foreach ($finder as $file) {
99 2
            if (!in_array($file->getFilename(), $allowed)) {
100 2
                continue;
101
            }
102 2
            if ('xml' == $file->getExtension()) {
103 2
                $xmlLoader->load($file->getRealPath());
104
            } elseif ('yml' == $file->getExtension()) {
105 1
                $ymlLoader->load($file->getRealPath());
106
            }
107
        }
108 2
    }
109
}
110