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