]> Raphaƫl G. Git Repositories - airbundle/blob - DependencyInjection/RapsysAirExtension.php
Add a skip feature in flatten member function
[airbundle] / DependencyInjection / RapsysAirExtension.php
1 <?php
2
3 namespace Rapsys\AirBundle\DependencyInjection;
4
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;
9
10 /**
11 * This is the class that loads and manages your bundle configuration.
12 *
13 * @link http://symfony.com/doc/current/cookbook/bundles/extension.html
14 */
15 class RapsysAirExtension extends Extension implements PrependExtensionInterface {
16 /**
17 * Prepend the configuration
18 *
19 * @desc Preload the configuration to allow sourcing as parameters
20 * {@inheritdoc}
21 */
22 public function prepend(ContainerBuilder $container) {
23 //Process the configuration
24 $configs = $container->getExtensionConfig($this->getAlias());
25
26 //Load configuration
27 $configuration = $this->getConfiguration($configs, $container);
28
29 //Process the configuration to get merged config
30 $config = $this->processConfiguration($configuration, $configs);
31
32 //Detect when no user configuration is provided
33 if ($configs === [[]]) {
34 //Prepend default config
35 $container->prependExtensionConfig($this->getAlias(), $config);
36 }
37
38 //Save configuration in parameters
39 $container->setParameter($this->getAlias(), $config);
40
41 //Store flattened array in parameters
42 //XXX: don't flatten rapsys_air.site.png key which is required to be an array
43 foreach($this->flatten($config, $this->getAlias(), 10, '.', ['rapsys_air.site.png']) as $k => $v) {
44 $container->setParameter($k, $v);
45 }
46 }
47
48 /**
49 * {@inheritdoc}
50 */
51 public function load(array $configs, ContainerBuilder $container) {
52 }
53
54 /**
55 * {@inheritdoc}
56 */
57 public function getAlias() {
58 return 'rapsys_air';
59 }
60
61 /**
62 * The function that parses the array to flatten it into a one level depth array
63 *
64 * @param $array The config values array
65 * @param $path The current key path
66 * @param $depth The maxmium depth
67 * @param $sep The separator string
68 * @param $skip The skipped paths array
69 */
70 protected function flatten($array, $path = '', $depth = 10, $sep = '.', $skip = []) {
71 //Init res
72 $res = array();
73
74 //Detect numerical only array
75 //count(array_filter($array, function($k) { return !is_numeric($k); }, ARRAY_FILTER_USE_KEY)) == 0
76 //array_reduce(array_keys($array), function($c, $k) { return $c += !is_numeric($k); }, 0)
77
78 //Flatten hashed array until depth reach zero
79 if ($depth && is_array($array) && $array !== [] && !in_array($path, $skip)) {
80 foreach($array as $k => $v) {
81 $sub = $path ? $path.$sep.$k:$k;
82 $res += $this->flatten($v, $sub, $depth - 1, $sep, $skip);
83 }
84 //Pass scalar value directly
85 } else {
86 $res[$path] = $array;
87 }
88
89 //Return result
90 return $res;
91 }
92 }