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 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);
80 public function load(array $configs, ContainerBuilder
$container): void {
84 * The function that parses the array to flatten it into a one level depth array
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
92 protected function flatten($array, $path = '', $depth = 10, $sep = '.', $skip = []) {
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)
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);
106 //Pass scalar value directly
108 $res[$path] = $array;