Completed
Push — develop ( c17c7b...45216f )
by Jens
03:41
created

HandlebarsExtension::configurePath()   C

Complexity

Conditions 7
Paths 30

Size

Total Lines 34
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 34
ccs 0
cts 29
cp 0
rs 6.7272
cc 7
eloc 21
nc 30
nop 2
crap 56
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\HttpKernel\DependencyInjection\Extension;
15
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
16
17
class HandlebarsExtension extends Extension
18
{
19
    public function load(array $configs, ContainerBuilder $container)
20
    {
21
        $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
22
        $loader->load('handlebars.xml');
23
24
        $configuration = new Configuration();
25
26
        $config = $this->processConfiguration($configuration, $configs);
27
28
        $config['flags'] = $this->getFlags($config);
29
30
31
        foreach ($config['translation'] as $key => $value) {
32
            $container->setParameter('handlebars.translation.' . $key, $value);
33
        }
34
35
        $this->setupAssetic($loader, $config, $container);
36
        $this->configurePath($config, $container);
37
38
        $container->getDefinition('handlebars.cache')->replaceArgument(0, $config['cache']);
39
        $container->getDefinition('handlebars.cache')->replaceArgument(1, $config['debug']);
40
41
        $container->getDefinition('handlebars')->replaceArgument(2, $config);
42
    }
43
44
    private function getFlags($config) {
45
        $flags = 0;
46 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...
47
            foreach ($config['flags'] as $flag) {
48
                $flags = $flags | constant('LightnCandy\LightnCandy::' . $flag);
49
            }
50
        }
51 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...
52
            foreach ($config['excludeFlags'] as $flag) {
53
                $flags = $flags & ~constant('LightnCandy\LightnCandy::' . $flag);
54
            }
55
            unset($config['excludeFlags']);
56
        }
57
        // ensure base functionality with flag standalone disabled
58
        $flags = ($flags | LightnCandy::FLAG_BESTPERFORMANCE |
59
                LightnCandy::FLAG_HANDLEBARSJS |
60
                LightnCandy::FLAG_RUNTIMEPARTIAL |
61
                LightnCandy::FLAG_HANDLEBARSLAMBDA |
62
                LightnCandy::FLAG_EXTHELPER |
63
                LightnCandy::FLAG_ERROR_EXCEPTION) & ~LightnCandy::FLAG_STANDALONEPHP;
64
65
        return $flags;
66
    }
67
68
    private function setupAssetic(LoaderInterface $loader, $config, ContainerBuilder $container)
69
    {
70
        // Enable AsseticExtension if undefined
71
        if (!isset($config['assetic'])) {
72
            $config['assetic'] = array_key_exists('AsseticBundle', $container->getParameter('kernel.bundles'));
73
        }
74
        // Assetic Extension
75
        if (true === $config['assetic']) {
76
            $loader->load('assetic.xml');
77
        }
78
        $container->setParameter('handlebars.assetic', $config['assetic']);
79
80
    }
81
82
    private function configurePath($config, ContainerBuilder $container)
83
    {
84
        $handlebarsFilesystemLoaderDefinition = $container->getDefinition('handlebars.loader.filesystem');
85
86
        // register user-configured paths
87
        foreach ($config['paths'] as $path => $namespace) {
88
            if (!$namespace) {
89
                $handlebarsFilesystemLoaderDefinition->addMethodCall('addPath', array($path));
90
            } else {
91
                $handlebarsFilesystemLoaderDefinition->addMethodCall('addPath', array($path, $namespace));
92
            }
93
        }
94
        $dir = $container->getParameter('kernel.root_dir').'/Resources/views';
95
        if (is_dir($dir)) {
96
            $handlebarsFilesystemLoaderDefinition->addMethodCall('addPath', array($dir));
97
        }
98
        $container->addResource(new FileExistenceResource($dir));
99
100
        // register bundles as Handlebars namespaces
101
        foreach ($container->getParameter('kernel.bundles') as $bundle => $class) {
102
            $dir = $container->getParameter('kernel.root_dir').'/Resources/'.$bundle.'/views';
103
            if (is_dir($dir)) {
104
                $handlebarsFilesystemLoaderDefinition->addMethodCall('addPath', array($dir));
105
            }
106
            $container->addResource(new FileExistenceResource($dir));
107
108
            $reflection = new \ReflectionClass($class);
109
            $dir = dirname($reflection->getFileName()).'/Resources/views';
110
            if (is_dir($dir)) {
111
                $handlebarsFilesystemLoaderDefinition->addMethodCall('addPath', array($dir));
112
            }
113
            $container->addResource(new FileExistenceResource($dir));
114
        }
115
    }
116
}
117