]> Raphaƫl G. Git Repositories - airbundle/blob - DependencyInjection/RapsysAirExtension.php
Update routes to match rapsys_user one
[airbundle] / DependencyInjection / RapsysAirExtension.php
1 <?php
2
3 namespace Rapsys\AirBundle\DependencyInjection;
4
5 use Symfony\Component\DependencyInjection\ContainerBuilder;
6 use Symfony\Component\DependencyInjection\Extension\Extension;
7 use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
8 use Symfony\Component\Translation\Loader\ArrayLoader;
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 RapsysAirExtension extends Extension implements PrependExtensionInterface {
16 /**
17 * Prepend the configuration
18 *
19 * @desc Preload the configuration to allow sourcing as parameters
20 * {@inheritdoc}
21 */
22 public function prepend(ContainerBuilder $container) {
23 //Load framework configurations
24 //XXX: required to extract default_locale and translation.fallbacks
25 $frameworks = $container->getExtensionConfig('framework');
26
27 //Recursively merge framework configurations
28 $framework = array_reduce(
29 $frameworks,
30 function ($res, $i) {
31 return array_merge_recursive($res, $i);
32 },
33 []
34 );
35
36 //Set translator fallbacks
37 $container->setParameter('kernel.translator.fallbacks', $framework['translator']['fallbacks']);
38
39 //Set default locale
40 $container->setParameter('kernel.default_locale', $framework['default_locale']);
41
42 //Load rapsys_user configurations
43 //XXX: required to extract default_locale and translation.fallbacks
44 $rapsys_users = $container->getExtensionConfig('rapsys_user');
45
46 //Recursively merge rapsys_user configurations
47 $rapsys_user = array_reduce(
48 $rapsys_users,
49 function ($res, $i) {
50 return array_merge_recursive($res, $i);
51 },
52 []
53 );
54
55 //Set rapsys_user.languages key
56 $container->setParameter('rapsys_user.languages', $rapsys_user['languages']);
57
58 //Process the configuration
59 $configs = $container->getExtensionConfig($this->getAlias());
60
61 //Load configuration
62 $configuration = $this->getConfiguration($configs, $container);
63
64 //Process the configuration to get merged config
65 $config = $this->processConfiguration($configuration, $configs);
66
67 //Detect when no user configuration is provided
68 if ($configs === [[]]) {
69 //Prepend default config
70 $container->prependExtensionConfig($this->getAlias(), $config);
71 }
72
73 //Save configuration in parameters
74 $container->setParameter($this->getAlias(), $config);
75
76 //Store flattened array in parameters
77 //XXX: don't flatten rapsys_air.site.png key which is required to be an array
78 foreach($this->flatten($config, $this->getAlias(), 10, '.', ['rapsys_air.site.png', 'rapsys_air.facebook.apps', 'rapsys_air.locales', 'rapsys_air.languages']) as $k => $v) {
79 $container->setParameter($k, $v);
80 }
81 }
82
83 /**
84 * {@inheritdoc}
85 */
86 public function load(array $configs, ContainerBuilder $container) {
87 }
88
89 /**
90 * {@inheritdoc}
91 */
92 public function getAlias() {
93 return 'rapsys_air';
94 }
95
96 /**
97 * The function that parses the array to flatten it into a one level depth array
98 *
99 * @param $array The config values array
100 * @param $path The current key path
101 * @param $depth The maxmium depth
102 * @param $sep The separator string
103 * @param $skip The skipped paths array
104 */
105 protected function flatten($array, $path = '', $depth = 10, $sep = '.', $skip = []) {
106 //Init res
107 $res = array();
108
109 //Detect numerical only array
110 //count(array_filter($array, function($k) { return !is_numeric($k); }, ARRAY_FILTER_USE_KEY)) == 0
111 //array_reduce(array_keys($array), function($c, $k) { return $c += !is_numeric($k); }, 0)
112
113 //Flatten hashed array until depth reach zero
114 if ($depth && is_array($array) && $array !== [] && !in_array($path, $skip)) {
115 foreach($array as $k => $v) {
116 $sub = $path ? $path.$sep.$k:$k;
117 $res += $this->flatten($v, $sub, $depth - 1, $sep, $skip);
118 }
119 //Pass scalar value directly
120 } else {
121 $res[$path] = $array;
122 }
123
124 //Return result
125 return $res;
126 }
127 }