1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author @jenschude <[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
|
1 |
|
public function load(array $configs, ContainerBuilder $container) |
20
|
|
|
{ |
21
|
1 |
|
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
22
|
1 |
|
$loader->load('handlebars.xml'); |
23
|
|
|
|
24
|
1 |
|
$configuration = new Configuration(); |
25
|
|
|
|
26
|
1 |
|
$config = $this->processConfiguration($configuration, $configs); |
27
|
|
|
|
28
|
1 |
|
$config['flags'] = $this->getFlags($config); |
29
|
|
|
|
30
|
|
|
|
31
|
1 |
|
foreach ($config['translation'] as $key => $value) { |
32
|
1 |
|
$container->setParameter('handlebars.translation.'.$key, $value); |
33
|
|
|
} |
34
|
1 |
|
foreach ($config['cms'] as $key => $value) { |
35
|
1 |
|
$container->setParameter('handlebars.cms.'.$key, $value); |
36
|
|
|
} |
37
|
|
|
|
38
|
1 |
|
$this->setupAssetic($loader, $config, $container); |
39
|
1 |
|
$this->configurePath($config, $container); |
40
|
|
|
|
41
|
1 |
|
$container->getDefinition('handlebars.cache')->replaceArgument(0, $config['cache']); |
42
|
1 |
|
$container->getDefinition('handlebars.cache')->replaceArgument(1, $config['debug']); |
43
|
|
|
|
44
|
1 |
|
$container->getDefinition('handlebars')->replaceArgument(2, $config); |
45
|
1 |
|
} |
46
|
|
|
|
47
|
1 |
|
private function getFlags($config) { |
48
|
1 |
|
$flags = 0; |
49
|
1 |
|
if (isset($config['flags'])) { |
50
|
1 |
|
$flags = $this->setFlags($flags, $config['flags'], 'set'); |
51
|
|
|
} |
52
|
1 |
|
if (isset($config['excludeFlags'])) { |
53
|
1 |
|
$flags = $this->setFlags($flags, $config['excludeFlags'], 'unset'); |
54
|
1 |
|
unset($config['excludeFlags']); |
55
|
|
|
} |
56
|
|
|
// ensure base functionality with flag standalone disabled |
57
|
1 |
|
$flags = ($flags | LightnCandy::FLAG_BESTPERFORMANCE | |
58
|
1 |
|
LightnCandy::FLAG_HANDLEBARSJS | |
59
|
1 |
|
LightnCandy::FLAG_RUNTIMEPARTIAL | |
60
|
1 |
|
LightnCandy::FLAG_EXTHELPER | |
61
|
1 |
|
LightnCandy::FLAG_ERROR_EXCEPTION) & ~LightnCandy::FLAG_STANDALONEPHP; |
62
|
|
|
|
63
|
1 |
|
return $flags; |
64
|
|
|
} |
65
|
|
|
|
66
|
1 |
|
private function setFlags($flags, array $configFlags, $op) { |
67
|
1 |
|
foreach ($configFlags as $flag) { |
68
|
|
|
$flags = $this->setFlag($flags, $flag, $op); |
69
|
|
|
} |
70
|
|
|
|
71
|
1 |
|
return $flags; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
private function setFlag($flags, $flag, $op) { |
75
|
|
|
if ($op == 'set') { |
76
|
|
|
return $flags | constant('LightnCandy\LightnCandy::'.$flag); |
77
|
|
|
} |
78
|
|
|
return $flags & ~constant('LightnCandy\LightnCandy::'.$flag); |
79
|
|
|
} |
80
|
|
|
|
81
|
1 |
|
private function setupAssetic(LoaderInterface $loader, $config, ContainerBuilder $container) |
82
|
|
|
{ |
83
|
|
|
// Enable AsseticExtension if undefined |
84
|
1 |
|
if (!isset($config['assetic'])) { |
85
|
1 |
|
$config['assetic'] = array_key_exists('AsseticBundle', $container->getParameter('kernel.bundles')); |
86
|
|
|
} |
87
|
|
|
// Assetic Extension |
88
|
1 |
|
if (true === $config['assetic']) { |
89
|
|
|
$loader->load('assetic.xml'); |
90
|
|
|
} |
91
|
1 |
|
$container->setParameter('handlebars.assetic', $config['assetic']); |
92
|
|
|
|
93
|
1 |
|
} |
94
|
|
|
|
95
|
1 |
|
private function configurePath($config, ContainerBuilder $container) |
96
|
|
|
{ |
97
|
1 |
|
$this->addConfigPath($config, $container); |
98
|
1 |
|
$this->addContainerPath($container); |
99
|
1 |
|
} |
100
|
|
|
|
101
|
1 |
|
private function addContainerPath(ContainerBuilder $container) |
102
|
|
|
{ |
103
|
1 |
|
$handlebarsFilesystemLoaderDefinition = $container->getDefinition('handlebars.loader.filesystem'); |
104
|
|
|
|
105
|
|
|
// register bundles as Handlebars namespaces |
106
|
1 |
|
foreach ($container->getParameter('kernel.bundles') as $bundle => $class) { |
107
|
1 |
|
$dir = $container->getParameter('kernel.root_dir').'/Resources/'.$bundle.'/views'; |
108
|
1 |
|
if (is_dir($dir)) { |
109
|
1 |
|
$handlebarsFilesystemLoaderDefinition->addMethodCall('addPath', array( |
110
|
1 |
|
$dir, |
111
|
1 |
|
$this->normalizeBundleNameForLoaderNamespace($bundle) |
112
|
|
|
)); |
113
|
|
|
} |
114
|
1 |
|
$container->addResource(new FileExistenceResource($dir)); |
115
|
|
|
|
116
|
1 |
|
$reflection = new \ReflectionClass($class); |
117
|
1 |
|
$dir = dirname($reflection->getFileName()).'/Resources/views'; |
118
|
1 |
|
if (is_dir($dir)) { |
119
|
1 |
|
$handlebarsFilesystemLoaderDefinition->addMethodCall('addPath', array( |
120
|
1 |
|
$dir, |
121
|
1 |
|
$this->normalizeBundleNameForLoaderNamespace($bundle) |
122
|
|
|
)); |
123
|
|
|
} |
124
|
1 |
|
$container->addResource(new FileExistenceResource($dir)); |
125
|
|
|
} |
126
|
1 |
|
} |
127
|
|
|
|
128
|
1 |
|
private function normalizeBundleNameForLoaderNamespace($bundle) |
129
|
|
|
{ |
130
|
1 |
|
if ('Bundle' === substr($bundle, -6)) { |
131
|
1 |
|
$bundle = substr($bundle, 0, -6); |
132
|
|
|
} |
133
|
|
|
|
134
|
1 |
|
return $bundle; |
135
|
|
|
} |
136
|
|
|
|
137
|
1 |
|
private function addConfigPath($config, ContainerBuilder $container) |
138
|
|
|
{ |
139
|
1 |
|
$handlebarsFilesystemLoaderDefinition = $container->getDefinition('handlebars.loader.filesystem'); |
140
|
|
|
|
141
|
|
|
// register user-configured paths |
142
|
1 |
|
foreach ($config['paths'] as $path => $namespace) { |
143
|
|
|
if (!$namespace) { |
144
|
|
|
$handlebarsFilesystemLoaderDefinition->addMethodCall('addPath', array($path)); |
145
|
|
|
} else { |
146
|
|
|
$handlebarsFilesystemLoaderDefinition->addMethodCall('addPath', array($path, $namespace)); |
147
|
|
|
} |
148
|
|
|
} |
149
|
1 |
|
$dir = $container->getParameter('kernel.root_dir').'/Resources/views'; |
150
|
1 |
|
if (is_dir($dir)) { |
151
|
1 |
|
$handlebarsFilesystemLoaderDefinition->addMethodCall('addPath', array($dir)); |
152
|
|
|
} |
153
|
1 |
|
$container->addResource(new FileExistenceResource($dir)); |
154
|
1 |
|
} |
155
|
|
|
} |
156
|
|
|
|