1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace steevanb\DevBundle\DependencyInjection; |
6
|
|
|
|
7
|
|
|
use Symfony\Component\Config\FileLocator; |
8
|
|
|
use Symfony\Component\DependencyInjection\{ |
9
|
|
|
ContainerBuilder, |
10
|
|
|
Definition, |
11
|
|
|
Reference, |
12
|
|
|
Loader |
13
|
|
|
}; |
14
|
|
|
use Symfony\Component\HttpKernel\{ |
15
|
|
|
DependencyInjection\Extension, |
16
|
|
|
KernelEvents |
17
|
|
|
}; |
18
|
|
|
|
19
|
|
|
class DevExtension extends Extension |
20
|
|
|
{ |
21
|
|
|
public function load(array $configs, ContainerBuilder $container): void |
22
|
|
|
{ |
23
|
|
|
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config')); |
24
|
|
|
$loader->load('services.yml'); |
25
|
|
|
|
26
|
|
|
$configuration = new Configuration(); |
27
|
|
|
$config = $this->processConfiguration($configuration, $configs); |
28
|
|
|
|
29
|
|
|
$this |
30
|
|
|
->parseTranslationConfig($config['translation_not_found'], $container) |
31
|
|
|
->parseValidateSchemaConfig($config['validate_schema'], $container); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
protected function parseTranslationConfig(array $config, ContainerBuilder $container): self |
35
|
|
|
{ |
36
|
|
|
if ($config['enabled']) { |
37
|
|
|
$definition = new Definition(); |
38
|
|
|
$definition->setClass('steevanb\\DevBundle\\EventListener\\TranslationsNotFoundListener'); |
39
|
|
|
$definition->addArgument(new Reference('translator')); |
40
|
|
|
$definition->addMethodCall('setAllowFallbacks', array($config['allow_fallbacks'])); |
41
|
|
|
$definition->addTag('kernel.event_listener', array( |
42
|
|
|
'event' => KernelEvents::RESPONSE, |
43
|
|
|
'method' => 'assertAllTranslationsFound' |
44
|
|
|
)); |
45
|
|
|
|
46
|
|
|
$container->setDefinition('dev.translations_not_found', $definition); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return $this; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
protected function parseValidateSchemaConfig(array $config, ContainerBuilder $container): self |
53
|
|
|
{ |
54
|
|
|
if ($config['enabled']) { |
55
|
|
|
$validateSchemaDefinition = $container->getDefinition('dev.validate_schema'); |
56
|
|
|
|
57
|
|
|
foreach ($config['paths'] as $path) { |
58
|
|
|
$validateSchemaDefinition->addMethodCall('addMappingPath', array($path)); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if ($config['bundles']['enabled']) { |
62
|
|
|
if (count($config['bundles']['bundles'])) { |
63
|
|
|
$bundles = $config['bundles']['bundles']; |
64
|
|
|
} else { |
65
|
|
|
$bundles = array_keys($container->getParameter('kernel.bundles')); |
66
|
|
|
} |
67
|
|
|
foreach ($bundles as $bundleName) { |
68
|
|
|
$validateSchemaDefinition->addMethodCall('addMappingBundle', array($bundleName)); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$validateSchemaDefinition->addMethodCall('setExcludes', array($config['excludes'])); |
73
|
|
|
|
74
|
|
|
$listener = new Definition('steevanb\\DevBundle\\EventListener\\ValidateSchemaListener'); |
75
|
|
|
$listener->addArgument(new Reference('dev.validate_schema')); |
76
|
|
|
$listener->addArgument($config['disabled_urls']); |
77
|
|
|
|
78
|
|
|
$listener->addTag('kernel.event_listener', array( |
79
|
|
|
'event' => ($config['event'] == 'kernel.request') ? 'kernel.request' : 'kernel.response', |
80
|
|
|
'method' => 'validateSchema' |
81
|
|
|
)); |
82
|
|
|
|
83
|
|
|
$container->setDefinition('dev.validate_schema.listener', $listener); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $this; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|