]> Raphaël G. Git Repositories - airbundle/blob - DependencyInjection/RapsysAirExtension.php
Add libre air logo translation
[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 /* XXX: All that shit is not used anymore in theory
35 * TODO: drop it ???
36 * XXX: problem was with ignoreExtraKeys($remove = true) missing false argument
37 //Load framework configurations
38 //XXX: required to extract default_locale and translation.fallbacks
39 $frameworks = $container->getExtensionConfig('framework');
40
41 //Recursively merge framework configurations
42 $framework = array_reduce(
43 $frameworks,
44 function ($res, $i) {
45 return array_merge_recursive($res, $i);
46 },
47 []
48 );
49
50 //Set translator fallbacks
51 $container->setParameter('kernel.translator.fallbacks', $framework['translator']['fallbacks']);
52
53 //Set default locale
54 $container->setParameter('kernel.default_locale', $framework['default_locale']);
55
56 //Load rapsys_user configurations
57 //XXX: required to extract class ?
58 $rapsys_users = $container->getExtensionConfig('rapsys_user');
59
60 //Recursively merge rapsys_user configurations
61 $rapsys_user = array_reduce(
62 $rapsys_users,
63 function ($res, $i) {
64 return array_merge_recursive($res, $i);
65 },
66 []
67 );
68
69 //Set rapsys_user.languages key
70 $container->setParameter('rapsys_user', $rapsys_user);
71
72 //Set rapsys_user.languages key
73 $container->setParameter('rapsys_user.languages', $rapsys_user['languages']);*/
74
75 //Process the configuration
76 $configs = $container->getExtensionConfig($this->getAlias());
77
78 //Load configuration
79 $configuration = $this->getConfiguration($configs, $container);
80
81 //Process the configuration to get merged config
82 $config = $this->processConfiguration($configuration, $configs);
83
84 //Detect when no user configuration is provided
85 if ($configs === [[]]) {
86 //Prepend default config
87 $container->prependExtensionConfig($this->getAlias(), $config);
88 }
89
90 //Save configuration in parameters
91 $container->setParameter($this->getAlias(), $config);
92
93 //Store flattened array in parameters
94 //XXX: don't flatten rapsys_air.site.png key which is required to be an array
95 foreach($this->flatten($config, $this->getAlias(), 10, '.', ['rapsys_air.copy', 'rapsys_air.icon', 'rapsys_air.icon.png', 'rapsys_air.logo', 'rapsys_air.facebook.apps', 'rapsys_air.locales', 'rapsys_air.languages']) as $k => $v) {
96 $container->setParameter($k, $v);
97 }
98 }
99
100 /**
101 * {@inheritdoc}
102 */
103 public function load(array $configs, ContainerBuilder $container) {
104 }
105
106 /**
107 * {@inheritdoc}
108 */
109 public function getAlias(): string {
110 return RapsysAirBundle::getAlias();
111 }
112
113 /**
114 * The function that parses the array to flatten it into a one level depth array
115 *
116 * @param $array The config values array
117 * @param $path The current key path
118 * @param $depth The maxmium depth
119 * @param $sep The separator string
120 * @param $skip The skipped paths array
121 */
122 protected function flatten($array, $path = '', $depth = 10, $sep = '.', $skip = []) {
123 //Init res
124 $res = array();
125
126 //Detect numerical only array
127 //count(array_filter($array, function($k) { return !is_numeric($k); }, ARRAY_FILTER_USE_KEY)) == 0
128 //array_reduce(array_keys($array), function($c, $k) { return $c += !is_numeric($k); }, 0)
129
130 //Flatten hashed array until depth reach zero
131 if ($depth && is_array($array) && $array !== [] && !in_array($path, $skip)) {
132 foreach($array as $k => $v) {
133 $sub = $path ? $path.$sep.$k:$k;
134 $res += $this->flatten($v, $sub, $depth - 1, $sep, $skip);
135 }
136 //Pass scalar value directly
137 } else {
138 $res[$path] = $array;
139 }
140
141 //Return result
142 return $res;
143 }
144 }