]> Raphaƫl G. Git Repositories - packbundle/blob - DependencyInjection/RapsysPackExtension.php
Cleanup
[packbundle] / DependencyInjection / RapsysPackExtension.php
1 <?php
2
3 namespace Rapsys\PackBundle\DependencyInjection;
4
5 use Symfony\Component\DependencyInjection\ContainerBuilder;
6 use Symfony\Component\Config\FileLocator;
7 use Symfony\Component\HttpKernel\DependencyInjection\Extension;
8 use Symfony\Component\DependencyInjection\Loader;
9
10 /**
11 * This is the class that loads and manages your bundle configuration.
12 *
13 * @link http://symfony.com/doc/current/cookbook/bundles/extension.html
14 */
15 class RapsysPackExtension extends Extension {
16 /**
17 * {@inheritdoc}
18 */
19 public function load(array $configs, ContainerBuilder $container) {
20 //Load configuration
21 $loader = new Loader\YamlFileLoader($container, new FileLocator('config/packages'));
22 $loader->load($this->getAlias().'.yaml');
23
24 //Load configuration
25 $configuration = $this->getConfiguration($configs, $container);
26 $config = $this->processConfiguration($configuration, $configs);
27
28 //Set default config in parameter
29 if (!$container->hasParameter($alias = $this->getAlias())) {
30 $container->setParameter($alias, $config[$alias]);
31 //Fill missing entries
32 } else {
33 //Change in config flag
34 $change = false;
35
36 //Iterate on each user configuration keys
37 foreach($container->getParameter($alias) as $k => $v) {
38 //Check if value is an array
39 if (is_array($v)) {
40 //Iterate on each array keys
41 foreach($v as $sk => $sv) {
42 //Check if sub value is an array
43 if (is_array($sv)) {
44 //TODO: implement sub sub key merging ? (or recursive ?)
45 @trigger_error('Nested level > 2 not yet implemented here', E_USER_ERROR);
46 //Save sub value
47 } else {
48 //Trigger changed flag
49 $change = true;
50 //Replace default value with user provided one
51 $config[$alias][$k][$sk] = $sv;
52 }
53 }
54 //Save value
55 } else {
56 //Trigger changed flag
57 $change = true;
58 //Replace default value with user provided one
59 $config[$alias][$k] = $v;
60 }
61 }
62
63 //Check if change occured
64 if ($change) {
65 //Save parameters
66 $container->setParameter($alias, $config[$alias]);
67 }
68 }
69 }
70
71 /**
72 * {@inheritdoc}
73 */
74 public function getConfiguration(array $configs, ContainerBuilder $container) {
75 //Get configuration instance with resolved public path
76 return new Configuration($container->getParameter('kernel.project_dir').'/public/');
77 }
78
79 /**
80 * {@inheritdoc}
81 */
82 public function getAlias() {
83 return 'rapsys_pack';
84 }
85 }