1 <?php
declare(strict_types
=1);
4 * This file is part of the Rapsys BlogBundle 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\BlogBundle\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\BlogBundle\RapsysBlogBundle
;
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 RapsysBlogExtension
extends Extension
implements PrependExtensionInterface
{
28 * Prepend the configuration
30 * @desc Preload the configuration to allow sourcing as parameters
33 public function prepend(ContainerBuilder
$container): void {
34 //Process the configuration
35 $configs = $container->getExtensionConfig($this->getAlias());
38 $configuration = $this->getConfiguration($configs, $container);
40 //Process the configuration to get merged config
41 $config = $this->processConfiguration($configuration, $configs);
43 //Detect when no user configuration is provided
44 if ($configs === [[]]) {
45 //Prepend default config
46 $container->prependExtensionConfig($this->getAlias(), $config);
49 //Save configuration in parameters
50 $container->setParameter($this->getAlias(), $config);
52 //Store flattened array in parameters
53 //XXX: don't flatten rapsys_blog.icon.png key which is required to be an array
54 foreach($this->flatten($config, $this->getAlias(), 10, '.', ['rapsys_blog.icon.png', 'rapsys_blog.facebook.apps', 'rapsys_blog.locales', 'rapsys_blog.languages']) as $k => $v) {
55 $container->setParameter($k, $v);
62 public function load(array $configs, ContainerBuilder
$container): void {
68 public function getAlias(): string {
69 return RapsysBlogBundle
::getAlias();
73 * The function that parses the array to flatten it into a one level depth array
75 * @param $array The config values array
76 * @param $path The current key path
77 * @param $depth The maxmium depth
78 * @param $sep The separator string
79 * @param $skip The skipped paths array
80 * @return array The result array
82 protected function flatten($array, $path = '', $depth = 10, $sep = '.', $skip = []): array {
86 //Flatten hashed array until depth reach zero
87 if ($depth && is_array($array) && $array !== [] && !in_array($path, $skip)) {
88 foreach($array as $k => $v) {
89 $sub = $path ? $path.$sep.$k:$k;
90 $res +
= $this->flatten($v, $sub, $depth - 1, $sep, $skip);
92 //Pass scalar value directly