Completed
Pull Request — develop (#10)
by
unknown
09:36
created

HandlebarsExtension::addContainerPath()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 28
Code Lines 18

Duplication

Lines 14
Ratio 50 %

Code Coverage

Tests 19
CRAP Score 4

Importance

Changes 0
Metric Value
dl 14
loc 28
ccs 19
cts 19
cp 1
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 18
nc 5
nop 1
crap 4
1
<?php
2
/**
3
 * @author @jayS-de <[email protected]>
4
 */
5
6
7
namespace JaySDe\HandlebarsBundle\DependencyInjection;
8
9
use LightnCandy\LightnCandy;
10
use Symfony\Component\Config\FileLocator;
11
use Symfony\Component\Config\Loader\LoaderInterface;
12
use Symfony\Component\Config\Resource\FileExistenceResource;
13
use Symfony\Component\DependencyInjection\ContainerBuilder;
14
use Symfony\Component\DependencyInjection\Definition;
15
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
16
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
17
18
class HandlebarsExtension extends Extension
19
{
20 6
    public function load(array $configs, ContainerBuilder $container)
21
    {
22 6
        $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
23 6
        $loader->load('handlebars.xml');
24
25 6
        $configuration = new Configuration();
26
27 6
        $config = $this->processConfiguration($configuration, $configs);
28
29 6
        $config['flags'] = $this->getFlags($config);
30
31
32 6
        foreach ($config['translation'] as $key => $value) {
33 6
            $container->setParameter('handlebars.translation.'.$key, $value);
34
        }
35 6
        foreach ($config['cms'] as $key => $value) {
36 6
            $container->setParameter('handlebars.cms.'.$key, $value);
37
        }
38
39 6
        $this->setupAssetic($loader, $config, $container);
40 6
        $this->configurePath($config, $container);
41
42 6
        $container->getDefinition('handlebars.cache')->replaceArgument(0, $config['cache']);
43 6
        $container->getDefinition('handlebars.cache')->replaceArgument(1, $config['debug']);
44
45 6
        $container->getDefinition('handlebars')->replaceArgument(2, $config);
46 6
    }
47
48 6
    private function getFlags($config) {
49 6
        $flags = 0;
50 6 View Code Duplication
        if (isset($config['flags'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
51 6
            foreach ($config['flags'] as $flag) {
52 2
                $flags = $flags | constant('LightnCandy\LightnCandy::'.$flag);
53
            }
54
        }
55 6 View Code Duplication
        if (isset($config['excludeFlags'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
56 6
            foreach ($config['excludeFlags'] as $flag) {
57 1
                $flags = $flags & ~constant('LightnCandy\LightnCandy::'.$flag);
58
            }
59 6
            unset($config['excludeFlags']);
60
        }
61
        // ensure base functionality with flag standalone disabled
62 6
        $flags = ($flags | LightnCandy::FLAG_BESTPERFORMANCE |
63 6
                LightnCandy::FLAG_HANDLEBARSJS |
64 6
                LightnCandy::FLAG_RUNTIMEPARTIAL |
65 6
                LightnCandy::FLAG_HANDLEBARSLAMBDA |
66 6
                LightnCandy::FLAG_EXTHELPER |
67 6
                LightnCandy::FLAG_ERROR_EXCEPTION) & ~LightnCandy::FLAG_STANDALONEPHP;
68
69 6
        return $flags;
70
    }
71
72 6
    private function setupAssetic(LoaderInterface $loader, $config, ContainerBuilder $container)
73
    {
74
        // Enable AsseticExtension if undefined
75 6
        if (!isset($config['assetic'])) {
76 5
            $config['assetic'] = array_key_exists('AsseticBundle', $container->getParameter('kernel.bundles'));
77
        }
78
        // Assetic Extension
79 6
        if (true === $config['assetic']) {
80 1
            $loader->load('assetic.xml');
81
        }
82 6
        $container->setParameter('handlebars.assetic', $config['assetic']);
83
84 6
    }
85
86 6
    private function configurePath($config, ContainerBuilder $container)
87
    {
88 6
        $this->addConfigPath($config, $container);
89 6
        $this->addContainerPath($container);
90 6
    }
91
92 6
    private function addContainerPath(ContainerBuilder $container)
93
    {
94 6
        $handlebarsFilesystemLoaderDefinition = $container->getDefinition('handlebars.loader.filesystem');
95
96
        // register bundles as Handlebars namespaces
97 6
        foreach ($container->getParameter('kernel.bundles') as $bundle => $class) {
98 1
            $dir = $container->getParameter('kernel.root_dir').'/Resources/'.$bundle.'/views';
99 1 View Code Duplication
            if (is_dir($dir)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
100 1
                $handlebarsFilesystemLoaderDefinition->addMethodCall('addPath', array($dir));
101 1
                $handlebarsFilesystemLoaderDefinition->addMethodCall('addPath', array(
102 1
                    $dir,
103 1
                    $this->normalizeBundleNameForLoaderNamespace($bundle)
104
                ));
105
            }
106 1
            $container->addResource(new FileExistenceResource($dir));
107
108 1
            $reflection = new \ReflectionClass($class);
109 1
            $dir = dirname($reflection->getFileName()).'/Resources/views';
110 1 View Code Duplication
            if (is_dir($dir)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
111 1
                $handlebarsFilesystemLoaderDefinition->addMethodCall('addPath', array($dir));
112 1
                $handlebarsFilesystemLoaderDefinition->addMethodCall('addPath', array(
113 1
                    $dir,
114 1
                    $this->normalizeBundleNameForLoaderNamespace($bundle)
115
                ));
116
            }
117 1
            $container->addResource(new FileExistenceResource($dir));
118
        }
119 6
    }
120
121 1
    private function normalizeBundleNameForLoaderNamespace($bundle)
122
    {
123 1
        if ('Bundle' === substr($bundle, -6)) {
124 1
            $bundle = substr($bundle, 0, -6);
125
        }
126
127 1
        return $bundle;
128
    }
129
130 6
    private function addConfigPath($config, ContainerBuilder $container)
131
    {
132 6
        $handlebarsFilesystemLoaderDefinition = $container->getDefinition('handlebars.loader.filesystem');
133
134
        // register user-configured paths
135 6
        foreach ($config['paths'] as $path => $namespace) {
136 1
            if (!$namespace) {
137 1
                $handlebarsFilesystemLoaderDefinition->addMethodCall('addPath', array($path));
138
            } else {
139 1
                $handlebarsFilesystemLoaderDefinition->addMethodCall('addPath', array($path, $namespace));
140
            }
141
        }
142 6
        $dir = $container->getParameter('kernel.root_dir').'/Resources/views';
143 6
        if (is_dir($dir)) {
144 6
            $handlebarsFilesystemLoaderDefinition->addMethodCall('addPath', array($dir));
145
        }
146 6
        $container->addResource(new FileExistenceResource($dir));
147 6
    }
148
}
149