Completed
Pull Request — master (#15)
by Martin
61:47 queued 54:36
created

GoetasTwitalExtension::load()   B

Complexity

Conditions 8
Paths 48

Size

Total Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 8

Importance

Changes 0
Metric Value
dl 0
loc 37
ccs 24
cts 24
cp 1
rs 8.0835
c 0
b 0
f 0
cc 8
nc 48
nop 2
crap 8
1
<?php
2
3
namespace Goetas\TwitalBundle\DependencyInjection;
4
5
use Goetas\TwitalBundle\Assetic\DirectoryResourceDefinition;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Goetas\TwitalBundle\Depe...ctoryResourceDefinition.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
6
use Symfony\Component\Config\FileLocator;
7
use Symfony\Component\DependencyInjection\ContainerBuilder;
8
use Symfony\Component\DependencyInjection\Loader;
9
use Symfony\Component\DependencyInjection\Reference;
10
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
11
12
/**
13
 *
14
 * @author Asmir Mustafic <[email protected]>
15
 *
16
 */
17
class GoetasTwitalExtension extends Extension
18
{
19
    /**
20
     * {@inheritDoc}
21
     */
22 6
    public function load(array $configs, ContainerBuilder $container)
23
    {
24 6
        $configuration = new Configuration();
25 6
        $config = $this->processConfiguration($configuration, $configs);
26
27 6
        $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
28 6
        $loader->load('twital.xml');
29
30 6
        $regex = array();
31 6
        $loaderDefinition = $container->getDefinition("twital.loader");
32 6
        foreach ($config["source_adapters"] as $id => $regs) {
33 6
            foreach ($regs["pattern"] as $reg) {
34 6
                $regex[] = $reg;
35 6
                $loaderDefinition->addMethodCall('addSourceAdapter', array($reg, new Reference($id)));
36
            }
37
        }
38 6
        $container->setParameter("twital.loader_regexs", array_unique($regex));
39
40 6
        $bundles = $container->getParameter("kernel.bundles");
41 6
        if (isset($bundles["AsseticBundle"])) {
42 1
            $loader->load('assetic.xml');
43
        }
44
45 6
        if (!empty($config["full_twig_compatibility"])) {
46 1
            $twitalDefinitioin = $container->getDefinition("twital");
47 1
            $twitalDefinitioin->addMethodCall('addExtension', array(new Reference('twital.extension.full_twig_compatibility')));
48
        }
49
50 6
        if (!class_exists('Symfony\Bundle\FrameworkBundle\CacheWarmer\TemplateFinderInterface')) {
51 6
            $container->removeDefinition('twital.cache_warmer');
52
        }
53
54 6
        if (!interface_exists('Symfony\Component\Templating\EngineInterface')
55 6
            || !class_exists('Symfony\Bundle\TwigBundle\TwigEngine')) {
56 6
            $container->removeDefinition('templating.engine.twital');
57
        }
58 6
    }
59
}
60