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