3 namespace Rapsys\AirBundle\DependencyInjection
;
5 use Symfony\Component\DependencyInjection\ContainerBuilder
;
6 use Symfony\Component\DependencyInjection\Extension\Extension
;
7 use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface
;
8 use Symfony\Component\Translation\Loader\ArrayLoader
;
11 * This is the class that loads and manages your bundle configuration.
13 * @link http://symfony.com/doc/current/cookbook/bundles/extension.html
15 class RapsysAirExtension
extends Extension
implements PrependExtensionInterface
{
17 * Prepend the configuration
19 * @desc Preload the configuration to allow sourcing as parameters
22 public function prepend(ContainerBuilder
$container) {
23 //Load framework configurations
24 //XXX: required to extract default_locale and translation.fallbacks
25 $frameworks = $container->getExtensionConfig('framework');
27 //Recursively merge framework configurations
28 $framework = array_reduce(
31 return array_merge_recursive($res, $i);
36 //Set translator fallbacks
37 $container->setParameter('kernel.translator.fallbacks', $framework['translator']['fallbacks']);
39 //Process the configuration
40 $configs = $container->getExtensionConfig($this->getAlias());
43 $configuration = $this->getConfiguration($configs, $container);
45 //Process the configuration to get merged config
46 $config = $this->processConfiguration($configuration, $configs);
48 //Detect when no user configuration is provided
49 if ($configs === [[]]) {
50 //Prepend default config
51 $container->prependExtensionConfig($this->getAlias(), $config);
54 //Save configuration in parameters
55 $container->setParameter($this->getAlias(), $config);
57 //Store flattened array in parameters
58 //XXX: don't flatten rapsys_air.site.png key which is required to be an array
59 foreach($this->flatten($config, $this->getAlias(), 10, '.', ['rapsys_air.site.png', 'rapsys_air.locales']) as $k => $v) {
60 $container->setParameter($k, $v);
67 public function load(array $configs, ContainerBuilder
$container) {
73 public function getAlias() {
78 * The function that parses the array to flatten it into a one level depth array
80 * @param $array The config values array
81 * @param $path The current key path
82 * @param $depth The maxmium depth
83 * @param $sep The separator string
84 * @param $skip The skipped paths array
86 protected function flatten($array, $path = '', $depth = 10, $sep = '.', $skip = []) {
90 //Detect numerical only array
91 //count(array_filter($array, function($k) { return !is_numeric($k); }, ARRAY_FILTER_USE_KEY)) == 0
92 //array_reduce(array_keys($array), function($c, $k) { return $c += !is_numeric($k); }, 0)
94 //Flatten hashed array until depth reach zero
95 if ($depth && is_array($array) && $array !== [] && !in_array($path, $skip)) {
96 foreach($array as $k => $v) {
97 $sub = $path ? $path.$sep.$k:$k;
98 $res +
= $this->flatten($v, $sub, $depth - 1, $sep, $skip);
100 //Pass scalar value directly
102 $res[$path] = $array;