Completed
Push — develop ( 45216f...b85a0a )
by Jens
02:57
created

HandlebarsExtension::addContainerPath()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

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