Added a new way to parse configuration
This commit is contained in:
parent
7f4a758659
commit
5cd55f05c2
103
DependencyInjection/Configuration.php
Normal file
103
DependencyInjection/Configuration.php
Normal file
|
@ -0,0 +1,103 @@
|
|||
<?php
|
||||
|
||||
namespace Propel\PropelBundle\DependencyInjection;
|
||||
|
||||
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
|
||||
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
|
||||
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 William DURAND <william.durand1@gmail.com>
|
||||
*/
|
||||
class Configuration implements ConfigurationInterface
|
||||
{
|
||||
private $debug;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param Boolean $debug Wether to use the debug mode
|
||||
*/
|
||||
public function __construct($debug)
|
||||
{
|
||||
$this->debug = (Boolean) $debug;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates the configuration tree builder.
|
||||
*
|
||||
* @return \Symfony\Component\Config\Definition\Builder\TreeBuilder The tree builder
|
||||
*/
|
||||
public function getConfigTreeBuilder()
|
||||
{
|
||||
$treeBuilder = new TreeBuilder();
|
||||
$rootNode = $treeBuilder->root('propel');
|
||||
|
||||
$this->addGeneralSection($rootNode);
|
||||
$this->addDbalSection($rootNode);
|
||||
|
||||
return $treeBuilder;
|
||||
}
|
||||
|
||||
private function addGeneralSection(ArrayNodeDefinition $node)
|
||||
{
|
||||
$node
|
||||
->children()
|
||||
->scalarNode('path')->end()
|
||||
->scalarNode('phing_path')->end()
|
||||
->scalarNode('charset')->defaultValue('UTF8')->end()
|
||||
->scalarNode('logging')->defaultValue($this->debug)->end()
|
||||
;
|
||||
}
|
||||
|
||||
private function addDbalSection(ArrayNodeDefinition $node)
|
||||
{
|
||||
$node
|
||||
->children()
|
||||
->arrayNode('dbal')
|
||||
->beforeNormalization()
|
||||
->ifNull()
|
||||
->then(function($v) { return array ('connections' => array('default' => array())); })
|
||||
->end()
|
||||
->children()
|
||||
->scalarNode('default_connection')->end()
|
||||
->end()
|
||||
->fixXmlConfig('connection')
|
||||
->append($this->getDbalConnectionsNode())
|
||||
->end()
|
||||
;
|
||||
}
|
||||
|
||||
private function getDbalConnectionsNode()
|
||||
{
|
||||
$treeBuilder = new TreeBuilder();
|
||||
$node = $treeBuilder->root('connections');
|
||||
|
||||
$node
|
||||
->requiresAtLeastOneElement()
|
||||
->useAttributeAsKey('name')
|
||||
->prototype('array')
|
||||
->children()
|
||||
->scalarNode('driver')->defaultValue('mysql')->end()
|
||||
->scalarNode('user')->defaultValue('root')->end()
|
||||
->scalarNode('password')->defaultNull()->end()
|
||||
->scalarNode('dsn')->defaultValue('')->end()
|
||||
->scalarNode('classname')->defaultValue($this->debug ? 'DebugPDO' : 'PropelPDO')->end()
|
||||
->end()
|
||||
->fixXmlConfig('options')
|
||||
->children()
|
||||
->arrayNode('options')
|
||||
->useAttributeAsKey('key')
|
||||
->prototype('scalar')->end()
|
||||
->end()
|
||||
->end()
|
||||
;
|
||||
|
||||
return $node;
|
||||
}
|
||||
}
|
|
@ -8,6 +8,7 @@ use Symfony\Component\DependencyInjection\ContainerBuilder;
|
|||
use Symfony\Component\DependencyInjection\Definition;
|
||||
use Symfony\Component\DependencyInjection\Reference;
|
||||
use Symfony\Component\Config\FileLocator;
|
||||
use Symfony\Component\Config\Definition\Processor;
|
||||
|
||||
class PropelExtension extends Extension
|
||||
{
|
||||
|
@ -19,6 +20,11 @@ class PropelExtension extends Extension
|
|||
*/
|
||||
public function load(array $configs, ContainerBuilder $container)
|
||||
{
|
||||
$processor = new Processor();
|
||||
$configuration = new Configuration($container->getParameter('kernel.debug'));
|
||||
$config = $processor->processConfiguration($configuration, $configs);
|
||||
|
||||
/*
|
||||
$dbal = array();
|
||||
foreach ($configs as $config) {
|
||||
if (isset($config['dbal'])) {
|
||||
|
@ -27,18 +33,11 @@ class PropelExtension extends Extension
|
|||
}
|
||||
|
||||
$config = $configs[0];
|
||||
|
||||
if (!$container->hasDefinition('propel')) {
|
||||
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
|
||||
$loader->load('propel.xml');
|
||||
}
|
||||
|
||||
*/
|
||||
if (!$container->hasParameter('propel.path')) {
|
||||
if (!isset($config['path'])) {
|
||||
throw new \InvalidArgumentException('The "path" parameter is mandatory.');
|
||||
}
|
||||
|
||||
$container->setParameter('propel.path', $config['path']);
|
||||
}
|
||||
|
||||
if (isset($config['path'])) {
|
||||
|
@ -54,6 +53,7 @@ class PropelExtension extends Extension
|
|||
} else {
|
||||
$charset = 'UTF8';
|
||||
}
|
||||
|
||||
$container->setParameter('propel.charset', $charset);
|
||||
|
||||
if (isset($config['logging']) && $config['logging']) {
|
||||
|
@ -61,35 +61,33 @@ class PropelExtension extends Extension
|
|||
} else {
|
||||
$logging = false;
|
||||
}
|
||||
|
||||
$container->setParameter('propel.logging', $logging);
|
||||
|
||||
|
||||
if (!empty($dbal)) {
|
||||
$this->dbalLoad($dbal, $container);
|
||||
if (!empty($config['dbal'])) {
|
||||
$this->dbalLoad($config['dbal'], $container);
|
||||
} else {
|
||||
throw new \RuntimeException('No "dbal" configuration found.');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads the DBAL configuration.
|
||||
*
|
||||
* @param array $configs An array of configuration settings
|
||||
* @param array $config An array of configuration settings
|
||||
* @param ContainerBuilder $container A ContainerBuilder instance
|
||||
*/
|
||||
protected function dbalLoad(array $configs, ContainerBuilder $container)
|
||||
protected function dbalLoad(array $config, ContainerBuilder $container)
|
||||
{
|
||||
if (!$container->hasDefinition('propel')) {
|
||||
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
|
||||
$loader->load('propel.xml');
|
||||
}
|
||||
|
||||
/*
|
||||
$mergedConfig = array(
|
||||
'default_connection' => 'default',
|
||||
);
|
||||
|
||||
if (!$container->hasParameter('propel.charset')) {
|
||||
$container->setParameter('propel.charset', 'utf8');
|
||||
}
|
||||
|
||||
if ($container->hasParameter('kernel.debug')) {
|
||||
$className = $container->getParameter('kernel.debug') ? 'DebugPDO' : 'PropelPDO';
|
||||
} else {
|
||||
|
@ -142,8 +140,8 @@ class PropelExtension extends Extension
|
|||
}
|
||||
|
||||
$config = $mergedConfig;
|
||||
*/
|
||||
$connectionName = $config['default_connection'];
|
||||
|
||||
$container->setParameter('propel.dbal.default_connection', $connectionName);
|
||||
|
||||
$c = array();
|
||||
|
|
Loading…
Reference in a new issue