1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Gerard van Helden <[email protected]> |
4
|
|
|
* @copyright Zicht Online <http://zicht.nl> |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace Zicht\Bundle\FrameworkExtraBundle\DependencyInjection; |
8
|
|
|
|
9
|
|
|
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; |
10
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
11
|
|
|
use Symfony\Component\Yaml\Yaml; |
12
|
|
|
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException; |
13
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension as DIExtension; |
14
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
15
|
|
|
use Symfony\Component\Config\FileLocator; |
16
|
|
|
use Symfony\Component\Config\Resource\FileResource; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* DI extension for the bundle |
20
|
|
|
*/ |
21
|
|
|
class ZichtFrameworkExtraExtension extends DIExtension |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Adds the uglify configuration |
25
|
|
|
* |
26
|
|
|
* @param string $uglifyConfigFile |
27
|
|
|
* @param boolean $isDebug |
28
|
|
|
* @param \Symfony\Component\DependencyInjection\ContainerBuilder $container |
29
|
|
|
* @return void |
30
|
|
|
* |
31
|
|
|
* @throws \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException |
32
|
|
|
*/ |
33
|
|
|
public function addUglifyConfiguration($uglifyConfigFile, $isDebug, ContainerBuilder $container) |
34
|
|
|
{ |
35
|
|
|
if (!is_file($uglifyConfigFile)) { |
36
|
|
|
throw new InvalidConfigurationException( |
37
|
|
|
"zicht_framework_extra.uglify setting '$uglifyConfigFile' is not a file" |
38
|
|
|
); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
$container->addResource(new FileResource($uglifyConfigFile)); |
42
|
|
|
|
43
|
|
|
try { |
44
|
|
|
$uglifyConfig = Yaml::parse($uglifyConfigFile); |
45
|
|
|
} catch (\Exception $e) { |
46
|
|
|
throw new InvalidConfigurationException( |
47
|
|
|
"zicht_framework_extra.uglify setting '$uglifyConfigFile' could not be read", |
48
|
|
|
0, |
49
|
|
|
$e |
50
|
|
|
); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$global = new Definition( |
54
|
|
|
'Zicht\Bundle\FrameworkExtraBundle\Twig\UglifyGlobal', |
55
|
|
|
array( |
56
|
|
|
$uglifyConfig, |
57
|
|
|
$isDebug |
58
|
|
|
) |
59
|
|
|
); |
60
|
|
|
|
61
|
|
|
$global->addTag('twig.global'); |
62
|
|
|
$global->addMethodCall('setDebug', array($isDebug)); |
63
|
|
|
$container->getDefinition('zicht_twig_extension')->addMethodCall('setGlobal', array('zicht_uglify', $global)); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Adds the requirejs configuration |
68
|
|
|
* |
69
|
|
|
* @param string $requirejsConfigFile |
70
|
|
|
* @param boolean $isDebug |
71
|
|
|
* @param \Symfony\Component\DependencyInjection\ContainerBuilder $container |
72
|
|
|
* @return void |
73
|
|
|
* |
74
|
|
|
* @throws \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException |
75
|
|
|
*/ |
76
|
|
|
public function addRequirejsConfiguration($requirejsConfigFile, $isDebug, ContainerBuilder $container) |
77
|
|
|
{ |
78
|
|
|
if (!is_file($requirejsConfigFile)) { |
79
|
|
|
throw new InvalidConfigurationException( |
80
|
|
|
"zicht_framework_extra.requirejs setting '$requirejsConfigFile' is not a file" |
81
|
|
|
); |
82
|
|
|
} |
83
|
|
|
$container->addResource(new FileResource($requirejsConfigFile)); |
84
|
|
|
try { |
85
|
|
|
$requirejsConfig = Yaml::parse($requirejsConfigFile); |
86
|
|
|
} catch (\Exception $e) { |
87
|
|
|
throw new InvalidConfigurationException( |
88
|
|
|
"zicht_framework_extra.requirejs setting '$requirejsConfigFile' could not be read", |
89
|
|
|
0, |
90
|
|
|
$e |
91
|
|
|
); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$global = new Definition( |
95
|
|
|
'Zicht\Bundle\FrameworkExtraBundle\Twig\RequirejsGlobal', |
96
|
|
|
array( |
97
|
|
|
$requirejsConfig, |
98
|
|
|
$isDebug |
99
|
|
|
) |
100
|
|
|
); |
101
|
|
|
|
102
|
|
|
$global->addTag('twig.global'); |
103
|
|
|
$global->addMethodCall('setDebug', array($isDebug)); |
104
|
|
|
$container->getDefinition('zicht_twig_extension')->addMethodCall('setGlobal', array('zicht_requirejs', $global)); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @{inheritDoc} |
109
|
|
|
*/ |
110
|
|
|
public function load(array $configs, ContainerBuilder $container) |
111
|
|
|
{ |
112
|
|
|
$loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config')); |
113
|
|
|
$loader->load('services.xml'); |
114
|
|
|
|
115
|
|
|
$loader->load('doctrine.xml'); |
116
|
|
|
$loader->load('imagine.xml'); |
117
|
|
|
|
118
|
|
|
$configuration = new Configuration(); |
119
|
|
|
$config = $this->processConfiguration($configuration, $configs); |
120
|
|
|
|
121
|
|
|
if (!empty($config['uglify'])) { |
122
|
|
|
if (!isset($config['uglify_debug'])) { |
123
|
|
|
$config['uglify_debug']= $container->getParameter('kernel.debug'); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
$this->addUglifyConfiguration($config['uglify'], $config['uglify_debug'], $container); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
if (!empty($config['requirejs'])) { |
130
|
|
|
if (!isset($config['requirejs_debug'])) { |
131
|
|
|
$config['requirejs_debug']= $container->getParameter('kernel.debug'); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
$this->addRequirejsConfiguration($config['requirejs'], $config['requirejs_debug'], $container); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
if (!empty($config['embed_helper'])) { |
138
|
|
|
$container->getDefinition('zicht_embed_helper') |
139
|
|
|
->addMethodCall( |
140
|
|
|
'setMarkExceptionsAsFormErrors', |
141
|
|
|
[$config['embed_helper']['mark_exceptions_as_errors']] |
142
|
|
|
); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
if (false === $config['disable_schema-update']) { |
146
|
|
|
$container->removeDefinition('zicht_framework_extra.event_listener.update_schema_doctrine_command_listener'); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|