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