Skip to content

Instantly share code, notes, and snippets.

@garex
Created June 10, 2016 12:24
Show Gist options
  • Save garex/bb991f14412dca823f74086684b4070a to your computer and use it in GitHub Desktop.
Save garex/bb991f14412dca823f74086684b4070a to your computer and use it in GitHub Desktop.
Tree-like parameters from services into flat
<?php
namespace Vendor\AppBundle\DependencyInjection;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Loader;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
class VendorAppExtension extends Extension
{
final public function load(array $configs, ContainerBuilder $container)
{
$this->loadInternal($this->processConfiguration($this->getConfiguration($configs, $container), $configs), $container);
}
protected function loadInternal(array $mergedConfig, ContainerBuilder $container)
{
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
$loader->load('services.yml');
$this->flatifyParameter($container, $this->getAlias());
}
private function flatifyParameter(ContainerBuilder $container, $globalKey, $value = null)
{
if (is_null($value)) {
$value = $container->getParameter($globalKey);
}
if (is_array($value)) {
foreach ($value as $key => $value) {
$this->flatifyParameter($container, $globalKey . '.' . $key, $value);
}
} else {
$container->setParameter($globalKey, $value);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment