1 <?php
declare(strict_types
=1);
4 * This file is part of the Rapsys TreeBundle package.
6 * (c) Raphaël Gertz <symfony@rapsys.eu>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Rapsys\TreeBundle\DependencyInjection
;
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
;
19 use Rapsys\TreeBundle\RapsysTreeBundle
;
22 * This is the class that loads and manages your bundle configuration.
24 * @link http://symfony.com/doc/current/cookbook/bundles/extension.html
26 class RapsysTreeExtension
extends Extension
implements PrependExtensionInterface
{
30 * Prepend the configuration
32 * Preload the configuration to allow sourcing as parameters
34 public function prepend(ContainerBuilder
$container): void {
35 //Process the configuration
36 $configs = $container->getExtensionConfig($alias = RapsysTreeBundle
::getAlias());
39 $configuration = $this->getConfiguration($configs, $container);
41 //Process the configuration to get merged config
42 $config = $this->processConfiguration($configuration, $configs);
44 //Detect when no user configuration is provided
45 if ($configs === [[]]) {
46 //Prepend default config
47 $container->prependExtensionConfig($alias, $config);
50 //Save configuration in parameters
51 $container->setParameter($alias, $config);
53 //Store flattened array in parameters
54 //XXX: don't flatten rapsystree.site.png key which is required to be an array
55 foreach($this->flatten($config, $alias, 10, '.', ['rapsystree.copy', 'rapsystree.icon', 'rapsystree.icon.png', 'rapsystree.logo', 'rapsystree.facebook.apps', 'rapsystree.locales', 'rapsystree.languages']) as $k => $v) {
56 $container->setParameter($k, $v);
59 //Set rapsystree.alias key
60 $container->setParameter($alias.'.alias', $alias);
62 //Set rapsystree.version key
63 $container->setParameter($alias.'.version', RapsysTreeBundle
::getVersion());
69 public function load(array $configs, ContainerBuilder
$container): void {
73 * The function that parses the array to flatten it into a one level depth array
75 * @param $array The config values array
76 * @param $path The current key path
77 * @param $depth The maxmium depth
78 * @param $sep The separator string
79 * @param $skip The skipped paths array
81 protected function flatten($array, $path = '', $depth = 10, $sep = '.', $skip = []) {
85 //Detect numerical only array
86 //count(array_filter($array, function($k) { return !is_numeric($k); }, ARRAY_FILTER_USE_KEY)) == 0
87 //array_reduce(array_keys($array), function($c, $k) { return $c += !is_numeric($k); }, 0)
89 //Flatten hashed array until depth reach zero
90 if ($depth && is_array($array) && $array !== [] && !in_array($path, $skip)) {
91 foreach($array as $k => $v) {
92 $sub = $path ? $path.$sep.$k:$k;
93 $res +
= $this->flatten($v, $sub, $depth - 1, $sep, $skip);
95 //Pass scalar value directly
107 * @xxx Required by kernel to load renamed alias configuration
109 public function getAlias(): string {
110 return RapsysTreeBundle
::getAlias();