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                  * XXX: problem was with ignoreExtraKeys($remove = true) missing false argument 
  37                 //Load framework configurations 
  38                 //XXX: required to extract default_locale and translation.fallbacks 
  39                 $frameworks = $container->getExtensionConfig('framework'); 
  41                 //Recursively merge framework configurations 
  42                 $framework = array_reduce( 
  45                                 return array_merge_recursive($res, $i); 
  50                 //Set translator fallbacks 
  51                 $container->setParameter('kernel.translator.fallbacks', $framework['translator']['fallbacks']); 
  54                 $container->setParameter('kernel.default_locale', $framework['default_locale']); 
  56                 //Load rapsys_user configurations 
  57                 //XXX: required to extract class ? 
  58                 $rapsys_users = $container->getExtensionConfig('rapsys_user'); 
  60                 //Recursively merge rapsys_user configurations 
  61                 $rapsys_user = array_reduce( 
  64                                 return array_merge_recursive($res, $i); 
  69                 //Set rapsys_user.languages key 
  70                 $container->setParameter('rapsys_user', $rapsys_user); 
  72                 //Set rapsys_user.languages key 
  73                 $container->setParameter('rapsys_user.languages', $rapsys_user['languages']);*/ 
  75                 //Process the configuration 
  76                 $configs = $container->getExtensionConfig($this->getAlias()); 
  79                 $configuration = $this->getConfiguration($configs, $container); 
  81                 //Process the configuration to get merged config 
  82                 $config = $this->processConfiguration($configuration, $configs); 
  84                 //Detect when no user configuration is provided 
  85                 if ($configs === [[]]) { 
  86                         //Prepend default config 
  87                         $container->prependExtensionConfig($this->getAlias(), $config); 
  90                 //Save configuration in parameters 
  91                 $container->setParameter($this->getAlias(), $config); 
  93                 //Store flattened array in parameters 
  94                 //XXX: don't flatten rapsys_air.site.png key which is required to be an array 
  95                 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) { 
  96                         $container->setParameter($k, $v); 
 103         public function load(array $configs, ContainerBuilder 
$container) { 
 109         public function getAlias(): string { 
 110                 return RapsysAirBundle
::getAlias(); 
 114          * The function that parses the array to flatten it into a one level depth array 
 116          * @param $array        The config values array 
 117          * @param $path         The current key path 
 118          * @param $depth        The maxmium depth 
 119          * @param $sep          The separator string 
 120          * @param $skip         The skipped paths array 
 122         protected function flatten($array, $path = '', $depth = 10, $sep = '.', $skip = []) { 
 126                 //Detect numerical only array 
 127                 //count(array_filter($array, function($k) { return !is_numeric($k); }, ARRAY_FILTER_USE_KEY)) == 0 
 128                 //array_reduce(array_keys($array), function($c, $k) { return $c += !is_numeric($k); }, 0) 
 130                 //Flatten hashed array until depth reach zero 
 131                 if ($depth && is_array($array) && $array !== [] && !in_array($path, $skip)) { 
 132                         foreach($array as $k => $v) { 
 133                                 $sub = $path ? $path.$sep.$k:$k; 
 134                                 $res +
= $this->flatten($v, $sub, $depth - 1, $sep, $skip); 
 136                 //Pass scalar value directly 
 138                         $res[$path] = $array;