]> Raphaël G. Git Repositories - airbundle/blob - DependencyInjection/RapsysAirExtension.php
Use checker member for isGranted call
[airbundle] / DependencyInjection / RapsysAirExtension.php
1 <?php declare(strict_types=1);
2
3 /*
4 * This file is part of the Rapsys PackBundle package.
5 *
6 * (c) Raphaël Gertz <symfony@rapsys.eu>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Rapsys\AirBundle\DependencyInjection;
13
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;
18
19 use Rapsys\AirBundle\RapsysAirBundle;
20
21 /**
22 * This is the class that loads and manages your bundle configuration.
23 *
24 * @link http://symfony.com/doc/current/cookbook/bundles/extension.html
25 */
26 class RapsysAirExtension extends Extension implements PrependExtensionInterface {
27 /**
28 * Prepend the configuration
29 *
30 * @desc Preload the configuration to allow sourcing as parameters
31 * {@inheritdoc}
32 */
33 public function prepend(ContainerBuilder $container) {
34 /* XXX: All that shit is not used anymore in theory
35 * TODO: drop it ???
36 //Load framework configurations
37 //XXX: required to extract default_locale and translation.fallbacks
38 $frameworks = $container->getExtensionConfig('framework');
39
40 //Recursively merge framework configurations
41 $framework = array_reduce(
42 $frameworks,
43 function ($res, $i) {
44 return array_merge_recursive($res, $i);
45 },
46 []
47 );
48
49 //Set translator fallbacks
50 $container->setParameter('kernel.translator.fallbacks', $framework['translator']['fallbacks']);
51
52 //Set default locale
53 $container->setParameter('kernel.default_locale', $framework['default_locale']);
54
55 //Load rapsys_user configurations
56 //XXX: required to extract default_locale and translation.fallbacks
57 $rapsys_users = $container->getExtensionConfig('rapsys_user');
58
59 //Recursively merge rapsys_user configurations
60 $rapsys_user = array_reduce(
61 $rapsys_users,
62 function ($res, $i) {
63 return array_merge_recursive($res, $i);
64 },
65 []
66 );
67
68 //Set rapsys_user.languages key
69 $container->setParameter('rapsys_user.languages', $rapsys_user['languages']);*/
70
71 //Process the configuration
72 $configs = $container->getExtensionConfig($this->getAlias());
73
74 //Load configuration
75 $configuration = $this->getConfiguration($configs, $container);
76
77 //Process the configuration to get merged config
78 $config = $this->processConfiguration($configuration, $configs);
79
80 //Detect when no user configuration is provided
81 if ($configs === [[]]) {
82 //Prepend default config
83 $container->prependExtensionConfig($this->getAlias(), $config);
84 }
85
86 //Save configuration in parameters
87 $container->setParameter($this->getAlias(), $config);
88
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);
93 }
94 }
95
96 /**
97 * {@inheritdoc}
98 */
99 public function load(array $configs, ContainerBuilder $container) {
100 }
101
102 /**
103 * {@inheritdoc}
104 */
105 public function getAlias(): string {
106 return RapsysAirBundle::getAlias();
107 }
108
109 /**
110 * The function that parses the array to flatten it into a one level depth array
111 *
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
117 */
118 protected function flatten($array, $path = '', $depth = 10, $sep = '.', $skip = []) {
119 //Init res
120 $res = array();
121
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)
125
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);
131 }
132 //Pass scalar value directly
133 } else {
134 $res[$path] = $array;
135 }
136
137 //Return result
138 return $res;
139 }
140 }