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 //Load framework configurations
35 //XXX: required to extract default_locale and translation.fallbacks
36 $frameworks = $container->getExtensionConfig('framework');
38 //Recursively merge framework configurations
39 $framework = array_reduce(
42 return array_merge_recursive($res, $i);
47 //Set translator fallbacks
48 $container->setParameter('kernel.translator.fallbacks', $framework['translator']['fallbacks']);
51 $container->setParameter('kernel.default_locale', $framework['default_locale']);
53 //Load rapsys_user configurations
54 //XXX: required to extract default_locale and translation.fallbacks
55 $rapsys_users = $container->getExtensionConfig('rapsys_user');
57 //Recursively merge rapsys_user configurations
58 $rapsys_user = array_reduce(
61 return array_merge_recursive($res, $i);
66 //Set rapsys_user.languages key
67 $container->setParameter('rapsys_user.languages', $rapsys_user['languages']);
69 //Process the configuration
70 $configs = $container->getExtensionConfig($this->getAlias());
73 $configuration = $this->getConfiguration($configs, $container);
75 //Process the configuration to get merged config
76 $config = $this->processConfiguration($configuration, $configs);
78 //Detect when no user configuration is provided
79 if ($configs === [[]]) {
80 //Prepend default config
81 $container->prependExtensionConfig($this->getAlias(), $config);
84 //Save configuration in parameters
85 $container->setParameter($this->getAlias(), $config);
87 //Store flattened array in parameters
88 //XXX: don't flatten rapsys_air.site.png key which is required to be an array
89 foreach($this->flatten($config, $this->getAlias(), 10, '.', ['rapsys_air.site.png', 'rapsys_air.site.icon', 'rapsys_air.site.logo', 'rapsys_air.facebook.apps', 'rapsys_air.locales', 'rapsys_air.languages']) as $k => $v) {
90 $container->setParameter($k, $v);
97 public function load(array $configs, ContainerBuilder
$container) {
103 public function getAlias(): string {
104 return RapsysAirBundle
::getAlias();
108 * The function that parses the array to flatten it into a one level depth array
110 * @param $array The config values array
111 * @param $path The current key path
112 * @param $depth The maxmium depth
113 * @param $sep The separator string
114 * @param $skip The skipped paths array
116 protected function flatten($array, $path = '', $depth = 10, $sep = '.', $skip = []) {
120 //Detect numerical only array
121 //count(array_filter($array, function($k) { return !is_numeric($k); }, ARRAY_FILTER_USE_KEY)) == 0
122 //array_reduce(array_keys($array), function($c, $k) { return $c += !is_numeric($k); }, 0)
124 //Flatten hashed array until depth reach zero
125 if ($depth && is_array($array) && $array !== [] && !in_array($path, $skip)) {
126 foreach($array as $k => $v) {
127 $sub = $path ? $path.$sep.$k:$k;
128 $res +
= $this->flatten($v, $sub, $depth - 1, $sep, $skip);
130 //Pass scalar value directly
132 $res[$path] = $array;