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