1 <?php
declare(strict_types
=1);
4 * This file is part of the Rapsys PackBundle 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\AirBundle\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\AirBundle\RapsysAirBundle
;
21 use Rapsys\UserBundle\RapsysUserBundle
;
24 * This is the class that loads and manages your bundle configuration.
26 * @link http://symfony.com/doc/current/cookbook/bundles/extension.html
28 class RapsysAirExtension
extends Extension
implements PrependExtensionInterface
{
32 * Prepend the configuration
34 * Preload the configuration to allow sourcing as parameters
36 public function prepend(ContainerBuilder
$container): void {
37 /*Load rapsysuser configurations
38 $rapsysusers = $container->getExtensionConfig($alias = RapsysUserBundle::getAlias());
40 //Recursively merge rapsysuser configurations
41 $rapsysuser = array_reduce(
44 return array_merge_recursive($res, $i);
49 //Set rapsysuser.languages key
50 $container->setParameter($alias, $rapsysuser);*/
52 //Process the configuration
53 $configs = $container->getExtensionConfig($alias = RapsysAirBundle
::getAlias());
56 $configuration = $this->getConfiguration($configs, $container);
58 //Process the configuration to get merged config
59 $config = $this->processConfiguration($configuration, $configs);
61 //Detect when no user configuration is provided
62 if ($configs === [[]]) {
63 //Prepend default config
64 $container->prependExtensionConfig($alias, $config);
67 //Save configuration in parameters
68 $container->setParameter($alias, $config);
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);
76 //Set rapsysair.alias key
77 $container->setParameter($alias.'.alias', $alias);
79 //Set rapsysair.version key
80 $container->setParameter($alias.'.version', RapsysAirBundle
::getVersion());
86 public function load(array $configs, ContainerBuilder
$container): void {
90 * The function that parses the array to flatten it into a one level depth array
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
98 protected function flatten($array, $path = '', $depth = 10, $sep = '.', $skip = []) {
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)
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);
112 //Pass scalar value directly
114 $res[$path] = $array;
124 * @xxx Required by kernel to load renamed alias configuration
126 public function getAlias(): string {
127 return RapsysAirBundle
::getAlias();