diff options
author | Christophe Coevoet <stof@notk.org> | 2012-09-26 00:51:23 +0200 |
---|---|---|
committer | Christophe Coevoet <stof@notk.org> | 2012-09-26 00:51:23 +0200 |
commit | a1263fe9da9b38358a1248d3883502f30df56b91 (patch) | |
tree | 6eb27b151bb1dec13b013373d21b67a6fce81d78 | |
parent | 4833acf3c970602bb794af10c277569699fe7d88 (diff) |
Refactored the config to use the Config component
-rw-r--r-- | DependencyInjection/Configuration.php | 86 | ||||
-rw-r--r-- | DependencyInjection/SdeBduExtension.php | 31 |
2 files changed, 96 insertions, 21 deletions
diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php new file mode 100644 index 0000000..6a05c43 --- /dev/null +++ b/DependencyInjection/Configuration.php @@ -0,0 +1,86 @@ +<?php + +/*********************************************************************** + * Copyright (c) 2011 Serveur des Elèves de l'ECP * + * This program is free software; you can redistribute and/or modify * + * it under the terms of the GNU General Public License as published * + * by the Free Software Foundation; version 2 of the license. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * + * See the GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, you can get it from: * + * http://www.gnu.org/copyleft/gpl.html * + ***********************************************************************/ + +namespace Sde\BduBundle\DependencyInjection; + +use Symfony\Component\Config\Definition\Builder\TreeBuilder; +use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; +use Symfony\Component\Config\Definition\ConfigurationInterface; + +/** + * This class contains the configuration information for the bundle + * + * This information is solely responsible for how the different configuration + * sections are normalized, and merged. + * + * @author Christophe Coevoet <stof@notk.org> + */ +class Configuration implements ConfigurationInterface +{ + private $debug; + + /** + * Constructor + * + * @param Boolean $debug Whether to use the debug mode + */ + public function __construct($debug) + { + $this->debug = (Boolean) $debug; + } + + /** + * Generates the configuration tree. + * + * @return TreeBuilder + */ + public function getConfigTreeBuilder() + { + $treeBuilder = new TreeBuilder(); + $rootNode = $treeBuilder->root('sde_bdu'); + + $supportedTypes = array(\Bdu_Configuration::TYPE_PEOPLE, \Bdu_Configuration::TYPE_GROUP, \Bdu_Configuration::TYPE_APPLI); + + $rootNode + ->children() + ->arrayNode('credentials') + ->addDefaultsIfNotSet() + ->children() + ->scalarNode('type') + ->isRequired() + ->beforeNormalization() + ->ifTrue(function ($v) { return defined('Bdu_Configuration::TYPE_'.strtoupper($v)); }) + ->then(function ($v) { return constant('Bdu_Configuration::TYPE_'.strtoupper($v));}) + ->end() + ->validate() + ->ifNotInArray($supportedTypes) + ->thenInvalid('The type %s is not supported. Please choose one of '.json_encode($supportedTypes)) + ->end() + ->end() + ->scalarNode('login')->isRequired()->cannotBeEmpty()->end() + ->scalarNode('password')->isRequired()->cannotBeEmpty()->end() + ->end() + ->isRequired() + ->end() + ->booleanNode('logging')->defaultValue($this->debug)->end() + ->booleanNode('use_username_form_type')->defaultTrue()->end() + ->end(); + + return $treeBuilder; + } +} diff --git a/DependencyInjection/SdeBduExtension.php b/DependencyInjection/SdeBduExtension.php index f958e0c..598f5d9 100644 --- a/DependencyInjection/SdeBduExtension.php +++ b/DependencyInjection/SdeBduExtension.php @@ -35,29 +35,13 @@ class SdeBduExtension extends Extension $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); $loader->load('api.xml'); - $normalizedConfig = array ("logging" => $container->getParameter('kernel.debug')); - foreach ($configs as $config) { - if (isset($config['credentials'])) { - $normalizedConfig['credentials'] = $config['credentials']; - } - if (isset ($config['logging'])) { - $normalizedConfig['logging'] = $config['logging']; - } - } - - if (!array_key_exists('credentials', $normalizedConfig) || array_diff(array ('login', 'type', 'password'), array_keys($normalizedConfig['credentials']))) { - throw new \InvalidArgumentException('You must provide the sde_bdu.credentials configuration'); - } + $config = $this->processConfiguration($this->getConfiguration($configs, $container), $configs); - $type = strtoupper($normalizedConfig['credentials']['type']); - if (!defined(constant('Bdu_Configuration::TYPE_'.$type))) { - throw new \InvalidArgumentException(sprintf('The type "%s" is not supported. It should be one of "people", "group", "appli"', $type)); - } - $container->setParameter('sde_bdu.credentials.login', $normalizedConfig['credentials']['login']); - $container->setParameter('sde_bdu.credentials.type', constant('Bdu_Configuration::TYPE_'.$type)); - $container->setParameter('sde_bdu.credentials.password', $normalizedConfig['credentials']['password']); + $container->setParameter('sde_bdu.credentials.login', $config['credentials']['login']); + $container->setParameter('sde_bdu.credentials.type', $config['credentials']['type']); + $container->setParameter('sde_bdu.credentials.password', $config['credentials']['password']); - if (false === $normalizedConfig['logging']) { + if (!$config['logging']) { $container->getDefinition('sde_bdu.configuration')->removeMethodCall('setLogger'); } } @@ -76,4 +60,9 @@ class SdeBduExtension extends Extension { return 'http://www.campus.ecp.fr/schema/dic/sde_bdu'; } + + public function getConfiguration(array $config, ContainerBuilder $container) + { + return new Configuration($container->getParameter('kernel.debug')); + } } |