|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the QueryResourcesLoader Bundle, an RunOpenCode project. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) 2016 RunOpenCode |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
namespace RunOpenCode\Bundle\QueryResourcesLoader\DependencyInjection; |
|
11
|
|
|
|
|
12
|
|
|
use Doctrine\Common\Cache\FilesystemCache; |
|
13
|
|
|
use Symfony\Component\Config\FileLocator; |
|
14
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
15
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
|
16
|
|
|
use Symfony\Component\DependencyInjection\Extension\Extension as BaseExtension; |
|
17
|
|
|
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; |
|
18
|
|
|
use Symfony\Component\Config\Resource\FileExistenceResource; |
|
19
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
|
20
|
|
|
|
|
21
|
|
|
class Extension extends BaseExtension |
|
22
|
|
|
{ |
|
23
|
|
|
public function getAlias() |
|
24
|
|
|
{ |
|
25
|
|
|
return 'run_open_code_query_resources_loader'; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* {@inheritdoc} |
|
30
|
|
|
*/ |
|
31
|
|
|
public function load(array $configs, ContainerBuilder $container) |
|
32
|
|
|
{ |
|
33
|
|
|
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
|
34
|
|
|
$loader->load('services.xml'); |
|
35
|
|
|
|
|
36
|
|
|
$configuration = new Configuration(); |
|
37
|
|
|
$config = $this->processConfiguration($configuration, $configs); |
|
38
|
|
|
|
|
39
|
|
|
$this |
|
40
|
|
|
->configureTwigGlobals($config, $container) |
|
41
|
|
|
->configureTwigEnvironment($config, $container) |
|
42
|
|
|
->configureTwigWarmUpCommands($config, $container) |
|
43
|
|
|
->configureTwigResourcePaths($config, $container) |
|
44
|
|
|
->configureTwigBundlePaths($config, $container) |
|
45
|
|
|
; |
|
46
|
|
|
|
|
47
|
|
|
if (null !== $config['default_executor']) { |
|
48
|
|
|
$container->setParameter('run_open_code.query_resources_loader.default_executor', $config['default_executor']); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
if (isset($config['twig']['autoescape_service'], $config['twig']['autoescape_service_method'])) { |
|
52
|
|
|
$config['twig']['autoescape'] = array(new Reference($config['twig']['autoescape_service']), $config['twig']['autoescape_service_method']); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
unset($config['twig']['autoescape_service'], $config['twig']['autoescape_service_method'], $config['twig']['globals']); |
|
56
|
|
|
|
|
57
|
|
|
$container->getDefinition('run_open_code.query_resources_loader.twig')->replaceArgument(1, $config['twig']); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param array $config |
|
62
|
|
|
* @param ContainerBuilder $container |
|
63
|
|
|
* @return Extension $this |
|
64
|
|
|
*/ |
|
65
|
|
|
protected function configureTwigGlobals(array $config, ContainerBuilder $container) |
|
66
|
|
|
{ |
|
67
|
|
|
if (false !== $container->hasDefinition('run_open_code.query_resources_loader.twig') && !empty($config['twig']['globals'])) { |
|
68
|
|
|
|
|
69
|
|
|
$definition = $container->getDefinition('run_open_code.query_resources_loader.twig'); |
|
70
|
|
|
|
|
71
|
|
|
foreach ($config['globals'] as $key => $global) { |
|
72
|
|
|
|
|
73
|
|
|
if (isset($global['type']) && 'service' === $global['type']) { |
|
74
|
|
|
$definition->addMethodCall('addGlobal', array($key, new Reference($global['id']))); |
|
75
|
|
|
} else { |
|
76
|
|
|
$definition->addMethodCall('addGlobal', array($key, $global['value'])); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
return $this; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param array $config |
|
86
|
|
|
* @param ContainerBuilder $container |
|
87
|
|
|
* @return Extension $this |
|
88
|
|
|
*/ |
|
89
|
|
|
protected function configureTwigEnvironment(array $config, ContainerBuilder $container) |
|
90
|
|
|
{ |
|
91
|
|
|
$envConfiguratorDefinition = $container->getDefinition('run_open_code.query_resources_loader.twig.configurator.environment'); |
|
92
|
|
|
$envConfiguratorDefinition->replaceArgument(0, $config['twig']['date']['format']); |
|
93
|
|
|
$envConfiguratorDefinition->replaceArgument(1, $config['twig']['date']['interval_format']); |
|
94
|
|
|
$envConfiguratorDefinition->replaceArgument(2, $config['twig']['date']['timezone']); |
|
95
|
|
|
$envConfiguratorDefinition->replaceArgument(3, $config['twig']['number_format']['decimals']); |
|
96
|
|
|
$envConfiguratorDefinition->replaceArgument(4, $config['twig']['number_format']['decimal_point']); |
|
97
|
|
|
$envConfiguratorDefinition->replaceArgument(5, $config['twig']['number_format']['thousands_separator']); |
|
98
|
|
|
|
|
99
|
|
|
return $this; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @param array $config |
|
104
|
|
|
* @param ContainerBuilder $container |
|
105
|
|
|
* @return Extension $this |
|
106
|
|
|
*/ |
|
107
|
|
|
protected function configureTwigWarmUpCommands(array $config, ContainerBuilder $container) |
|
|
|
|
|
|
108
|
|
|
{ |
|
109
|
|
|
// @TODO Implement this functionality |
|
110
|
|
|
|
|
111
|
|
|
return $this; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @param array $config |
|
116
|
|
|
* @param ContainerBuilder $container |
|
117
|
|
|
* @return Extension $this |
|
118
|
|
|
*/ |
|
119
|
|
|
protected function configureTwigResourcePaths(array $config, ContainerBuilder $container) |
|
120
|
|
|
{ |
|
121
|
|
|
$twigFilesystemLoaderDefinition = $container->getDefinition('run_open_code.query_resources_loader.twig.loader.filesystem'); |
|
122
|
|
|
|
|
123
|
|
|
// register user-configured paths |
|
124
|
|
|
foreach ($config['twig']['paths'] as $path => $namespace) { |
|
125
|
|
|
if (!$namespace) { |
|
126
|
|
|
$twigFilesystemLoaderDefinition->addMethodCall('addPath', array($path)); |
|
127
|
|
|
} else { |
|
128
|
|
|
$twigFilesystemLoaderDefinition->addMethodCall('addPath', array($path, $namespace)); |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
return $this; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @param array $config |
|
137
|
|
|
* @param ContainerBuilder $container |
|
138
|
|
|
* @return Extension $this |
|
139
|
|
|
*/ |
|
140
|
|
|
protected function configureTwigBundlePaths(array $config, ContainerBuilder $container) |
|
|
|
|
|
|
141
|
|
|
{ |
|
142
|
|
|
$twigFilesystemLoaderDefinition = $container->getDefinition('run_open_code.query_resources_loader.twig.loader.filesystem'); |
|
143
|
|
|
|
|
144
|
|
|
$addTwigPath = function($dir, $bundle) use ($twigFilesystemLoaderDefinition) { |
|
145
|
|
|
|
|
146
|
|
|
$name = $bundle; |
|
147
|
|
|
|
|
148
|
|
|
if ('Bundle' === substr($name, -6)) { |
|
149
|
|
|
$name = substr($name, 0, -6); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
$twigFilesystemLoaderDefinition->addMethodCall('addPath', array($dir, $name)); |
|
153
|
|
|
}; |
|
154
|
|
|
|
|
155
|
|
|
// register bundles as Twig namespaces |
|
156
|
|
|
foreach ($container->getParameter('kernel.bundles') as $bundle => $class) { |
|
157
|
|
|
|
|
158
|
|
|
$dir = $container->getParameter('kernel.root_dir').'/Resources/'.$bundle.'/query'; |
|
159
|
|
|
|
|
160
|
|
|
if (is_dir($dir)) { |
|
161
|
|
|
$addTwigPath($dir, $bundle); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
$container->addResource(new FileExistenceResource($dir)); |
|
165
|
|
|
|
|
166
|
|
|
$reflection = new \ReflectionClass($class); |
|
167
|
|
|
$dir = dirname($reflection->getFileName()).'/Resources/query'; |
|
168
|
|
|
|
|
169
|
|
|
if (is_dir($dir)) { |
|
170
|
|
|
$addTwigPath($dir, $bundle); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
$container->addResource(new FileExistenceResource($dir)); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
return $this; |
|
177
|
|
|
} |
|
178
|
|
|
} |
|
179
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.