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