]> Raphaël G. Git Repositories - blogbundle/blob - DependencyInjection/RapsysBlogExtension.php
Rename rapsys_blog route to rapsysblog
[blogbundle] / DependencyInjection / RapsysBlogExtension.php
1 <?php declare(strict_types=1);
2
3 /*
4 * This file is part of the Rapsys BlogBundle 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\BlogBundle\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\BlogBundle\RapsysBlogBundle;
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 RapsysBlogExtension extends Extension implements PrependExtensionInterface {
27 /**
28 * Prepend the configuration
29 *
30 * Preload the configuration to allow sourcing as parameters
31 *
32 * {@inheritdoc}
33 */
34 public function prepend(ContainerBuilder $container): void {
35 //Process the configuration
36 $configs = $container->getExtensionConfig($alias = $this->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 rapsysblog.icon.png key which is required to be an array
55 foreach($this->flatten($config, $alias, 10, '.', ['rapsysblog.contact', 'rapsysblog.copy', 'rapsysblog.icon', 'rapsysblog.icon.png', 'rapsysblog.logo', 'rapsysblog.facebook.apps', 'rapsysblog.locales', 'rapsysblog.languages']) as $k => $v) {
56 $container->setParameter($k, $v);
57 }
58
59 //Set rapsysair.alias key
60 $container->setParameter($alias.'.alias', $alias);
61
62 //Set rapsysair.version key
63 $container->setParameter($alias.'.version', RapsysBlogBundle::getVersion());
64 }
65
66 /**
67 * {@inheritdoc}
68 */
69 public function load(array $configs, ContainerBuilder $container): void {
70 }
71
72 /**
73 * {@inheritdoc}
74 */
75 public function getAlias(): string {
76 return RapsysBlogBundle::getAlias();
77 }
78
79 /**
80 * The function that parses the array to flatten it into a one level depth array
81 *
82 * @param $array The config values array
83 * @param $path The current key path
84 * @param $depth The maxmium depth
85 * @param $sep The separator string
86 * @param $skip The skipped paths array
87 * @return array The result array
88 */
89 protected function flatten($array, $path = '', $depth = 10, $sep = '.', $skip = []): array {
90 //Init res
91 $res = [];
92
93 //Flatten hashed array until depth reach zero
94 if ($depth && is_array($array) && $array !== [] && !in_array($path, $skip)) {
95 foreach($array as $k => $v) {
96 $sub = $path ? $path.$sep.$k:$k;
97 $res += $this->flatten($v, $sub, $depth - 1, $sep, $skip);
98 }
99 //Pass scalar value directly
100 } else {
101 $res[$path] = $array;
102 }
103
104 //Return result
105 return $res;
106 }
107 }