]> Raphaël G. Git Repositories - airbundle/blob - DependencyInjection/RapsysAirExtension.php
Shorten air bundle alias
[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 rapsys_air.site.png key which is required to be an array
72 foreach($this->flatten($config, $alias, 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) {
73 $container->setParameter($k, $v);
74 }
75 }
76
77 /**
78 * {@inheritdoc}
79 */
80 public function load(array $configs, ContainerBuilder $container): void {
81 }
82
83 /**
84 * The function that parses the array to flatten it into a one level depth array
85 *
86 * @param $array The config values array
87 * @param $path The current key path
88 * @param $depth The maxmium depth
89 * @param $sep The separator string
90 * @param $skip The skipped paths array
91 */
92 protected function flatten($array, $path = '', $depth = 10, $sep = '.', $skip = []) {
93 //Init res
94 $res = array();
95
96 //Detect numerical only array
97 //count(array_filter($array, function($k) { return !is_numeric($k); }, ARRAY_FILTER_USE_KEY)) == 0
98 //array_reduce(array_keys($array), function($c, $k) { return $c += !is_numeric($k); }, 0)
99
100 //Flatten hashed array until depth reach zero
101 if ($depth && is_array($array) && $array !== [] && !in_array($path, $skip)) {
102 foreach($array as $k => $v) {
103 $sub = $path ? $path.$sep.$k:$k;
104 $res += $this->flatten($v, $sub, $depth - 1, $sep, $skip);
105 }
106 //Pass scalar value directly
107 } else {
108 $res[$path] = $array;
109 }
110
111 //Return result
112 return $res;
113 }
114 }