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
;
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 RapsysAirExtension
extends Extension
implements PrependExtensionInterface
{
28 * Prepend the configuration
30 * @desc Preload the configuration to allow sourcing as parameters
33 public function prepend(ContainerBuilder
$container) {
34 /* XXX: All that shit is not used anymore in theory
36 //Load framework configurations
37 //XXX: required to extract default_locale and translation.fallbacks
38 $frameworks = $container->getExtensionConfig('framework');
40 //Recursively merge framework configurations
41 $framework = array_reduce(
44 return array_merge_recursive($res, $i);
49 //Set translator fallbacks
50 $container->setParameter('kernel.translator.fallbacks', $framework['translator']['fallbacks']);
53 $container->setParameter('kernel.default_locale', $framework['default_locale']);
55 //Load rapsys_user configurations
56 //XXX: required to extract default_locale and translation.fallbacks
57 $rapsys_users = $container->getExtensionConfig('rapsys_user');
59 //Recursively merge rapsys_user configurations
60 $rapsys_user = array_reduce(
63 return array_merge_recursive($res, $i);
68 //Set rapsys_user.languages key
69 $container->setParameter('rapsys_user.languages', $rapsys_user['languages']);*/
71 //Process the configuration
72 $configs = $container->getExtensionConfig($this->getAlias());
75 $configuration = $this->getConfiguration($configs, $container);
77 //Process the configuration to get merged config
78 $config = $this->processConfiguration($configuration, $configs);
80 //Detect when no user configuration is provided
81 if ($configs === [[]]) {
82 //Prepend default config
83 $container->prependExtensionConfig($this->getAlias(), $config);
86 //Save configuration in parameters
87 $container->setParameter($this->getAlias(), $config);
89 //Store flattened array in parameters
90 //XXX: don't flatten rapsys_air.site.png key which is required to be an array
91 foreach($this->flatten($config, $this->getAlias(), 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) {
92 $container->setParameter($k, $v);
99 public function load(array $configs, ContainerBuilder
$container) {
105 public function getAlias(): string {
106 return RapsysAirBundle
::getAlias();
110 * The function that parses the array to flatten it into a one level depth array
112 * @param $array The config values array
113 * @param $path The current key path
114 * @param $depth The maxmium depth
115 * @param $sep The separator string
116 * @param $skip The skipped paths array
118 protected function flatten($array, $path = '', $depth = 10, $sep = '.', $skip = []) {
122 //Detect numerical only array
123 //count(array_filter($array, function($k) { return !is_numeric($k); }, ARRAY_FILTER_USE_KEY)) == 0
124 //array_reduce(array_keys($array), function($c, $k) { return $c += !is_numeric($k); }, 0)
126 //Flatten hashed array until depth reach zero
127 if ($depth && is_array($array) && $array !== [] && !in_array($path, $skip)) {
128 foreach($array as $k => $v) {
129 $sub = $path ? $path.$sep.$k:$k;
130 $res +
= $this->flatten($v, $sub, $depth - 1, $sep, $skip);
132 //Pass scalar value directly
134 $res[$path] = $array;