]> Raphaël G. Git Repositories - airbundle/blob - DependencyInjection/RapsysAirExtension.php
Fix zipcode default value to match form input type
[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 use Rapsys\UserBundle\RapsysUserBundle;
22
23 /**
24 * This is the class that loads and manages your bundle configuration.
25 *
26 * @link http://symfony.com/doc/current/cookbook/bundles/extension.html
27 */
28 class RapsysAirExtension extends Extension implements PrependExtensionInterface {
29 /**
30 * {@inheritdoc}
31 *
32 * Prepend the configuration
33 *
34 * Preload the configuration to allow sourcing as parameters
35 */
36 public function prepend(ContainerBuilder $container): void {
37 /*Load rapsysuser configurations
38 $rapsysusers = $container->getExtensionConfig($alias = RapsysUserBundle::getAlias());
39
40 //Recursively merge rapsysuser configurations
41 $rapsysuser = array_reduce(
42 $rapsysusers,
43 function ($res, $i) {
44 return array_merge_recursive($res, $i);
45 },
46 []
47 );
48
49 //Set rapsysuser.languages key
50 $container->setParameter($alias, $rapsysuser);*/
51
52 //Process the configuration
53 $configs = $container->getExtensionConfig($alias = RapsysAirBundle::getAlias());
54
55 //Load configuration
56 $configuration = $this->getConfiguration($configs, $container);
57
58 //Process the configuration to get merged config
59 $config = $this->processConfiguration($configuration, $configs);
60
61 //Detect when no user configuration is provided
62 if ($configs === [[]]) {
63 //Prepend default config
64 $container->prependExtensionConfig($alias, $config);
65 }
66
67 //Save configuration in parameters
68 $container->setParameter($alias, $config);
69
70 //Store flattened array in parameters
71 //XXX: don't flatten rapsysair.site.png key which is required to be an array
72 foreach($this->flatten($config, $alias, 10, '.', ['rapsysair.copy', 'rapsysair.icon', 'rapsysair.icon.png', 'rapsysair.logo', 'rapsysair.facebook.apps', 'rapsysair.locales', 'rapsysair.languages']) as $k => $v) {
73 $container->setParameter($k, $v);
74 }
75
76 //Set rapsysair.alias key
77 $container->setParameter($alias.'.alias', $alias);
78
79 //Set rapsysair.version key
80 $container->setParameter($alias.'.version', RapsysAirBundle::getVersion());
81 }
82
83 /**
84 * {@inheritdoc}
85 */
86 public function load(array $configs, ContainerBuilder $container): void {
87 }
88
89 /**
90 * The function that parses the array to flatten it into a one level depth array
91 *
92 * @param $array The config values array
93 * @param $path The current key path
94 * @param $depth The maxmium depth
95 * @param $sep The separator string
96 * @param $skip The skipped paths array
97 */
98 protected function flatten($array, $path = '', $depth = 10, $sep = '.', $skip = []) {
99 //Init res
100 $res = array();
101
102 //Detect numerical only array
103 //count(array_filter($array, function($k) { return !is_numeric($k); }, ARRAY_FILTER_USE_KEY)) == 0
104 //array_reduce(array_keys($array), function($c, $k) { return $c += !is_numeric($k); }, 0)
105
106 //Flatten hashed array until depth reach zero
107 if ($depth && is_array($array) && $array !== [] && !in_array($path, $skip)) {
108 foreach($array as $k => $v) {
109 $sub = $path ? $path.$sep.$k:$k;
110 $res += $this->flatten($v, $sub, $depth - 1, $sep, $skip);
111 }
112 //Pass scalar value directly
113 } else {
114 $res[$path] = $array;
115 }
116
117 //Return result
118 return $res;
119 }
120
121 /**
122 * {@inheritdoc}
123 *
124 * @xxx Required by kernel to load renamed alias configuration
125 */
126 public function getAlias(): string {
127 return RapsysAirBundle::getAlias();
128 }
129 }