Started to integrate the new configuration system (still WIP/dirty)
This commit is contained in:
parent
48b29243f3
commit
09e4da0c19
13 changed files with 53 additions and 413 deletions
|
|
@ -10,237 +10,11 @@
|
|||
|
||||
namespace Propel\PropelBundle\DependencyInjection;
|
||||
|
||||
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
|
||||
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
|
||||
use Symfony\Component\Config\Definition\ConfigurationInterface;
|
||||
use Propel\Common\Config\PropelConfiguration;
|
||||
|
||||
/**
|
||||
* 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
|
||||
class Configuration extends PropelConfiguration
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds 'general' configuration.
|
||||
*
|
||||
* propel:
|
||||
* logging: %kernel.debug%
|
||||
* build_properties:
|
||||
* xxxx.xxxx: xxxxxx
|
||||
* ...
|
||||
* behaviors:
|
||||
* fooable: My\FooableBehavior
|
||||
* barable: src.barable.BarableBehavior
|
||||
*/
|
||||
private function addGeneralSection(ArrayNodeDefinition $node)
|
||||
{
|
||||
$node
|
||||
->children()
|
||||
->scalarNode('logging')->defaultValue($this->debug)->end()
|
||||
->arrayNode('build_properties')
|
||||
->useAttributeAsKey('key')
|
||||
->prototype('scalar')->end()
|
||||
->end()
|
||||
->arrayNode('behaviors')
|
||||
->useAttributeAsKey('key')
|
||||
->prototype('scalar')->end()
|
||||
->end()
|
||||
;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds 'dbal' configuration.
|
||||
*
|
||||
* propel:
|
||||
* dbal:
|
||||
* driver: mysql
|
||||
* user: root
|
||||
* password: null
|
||||
* dsn: xxxxxxxx
|
||||
* options: {}
|
||||
* attributes: {}
|
||||
* settings: {}
|
||||
* default_connection: xxxxxx
|
||||
*/
|
||||
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')->defaultValue('default')->end()
|
||||
->scalarNode('driver')
|
||||
->beforeNormalization()
|
||||
->always()
|
||||
->then(function ($v) { return str_replace('pdo_', '', $v); })
|
||||
->end()
|
||||
->defaultValue('mysql')
|
||||
->end()
|
||||
->scalarNode('user')->defaultValue('root')->end()
|
||||
->scalarNode('password')->defaultValue('')->end()
|
||||
->scalarNode('dsn')
|
||||
->beforeNormalization()
|
||||
->always()
|
||||
->then(function ($v) { return str_replace('pdo_', '', $v); })
|
||||
->end()
|
||||
->defaultValue('')
|
||||
->end()
|
||||
->scalarNode('classname')->defaultValue($this->debug ? '\Propel\Runtime\Connection\DebugPDO' : '\Propel\Runtime\Connection\ConnectionWrapper')->end()
|
||||
->end()
|
||||
->fixXmlConfig('option')
|
||||
->children()
|
||||
->arrayNode('options')
|
||||
->useAttributeAsKey('key')
|
||||
->prototype('scalar')->end()
|
||||
->end()
|
||||
->end()
|
||||
->fixXmlConfig('attribute')
|
||||
->children()
|
||||
->arrayNode('attributes')
|
||||
->useAttributeAsKey('key')
|
||||
->prototype('scalar')->end()
|
||||
->end()
|
||||
->end()
|
||||
->fixXmlConfig('setting')
|
||||
->children()
|
||||
->arrayNode('settings')
|
||||
->useAttributeAsKey('key')
|
||||
->prototype('scalar')->end()
|
||||
->end()
|
||||
->end()
|
||||
->fixXmlConfig('connection')
|
||||
->append($this->getDbalConnectionsNode())
|
||||
->end()
|
||||
;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a tree configuration for this part of configuration:
|
||||
*
|
||||
* connections:
|
||||
* default:
|
||||
* driver: mysql
|
||||
* user: root
|
||||
* password: null
|
||||
* dsn: xxxxxxxx
|
||||
* classname: PropelPDO
|
||||
* options: {}
|
||||
* attributes: {}
|
||||
* settings: {}
|
||||
*
|
||||
* @return \Symfony\Component\Config\Definition\Builder\TreeBuilder The tree builder
|
||||
*/
|
||||
private function getDbalConnectionsNode()
|
||||
{
|
||||
$treeBuilder = new TreeBuilder();
|
||||
$node = $treeBuilder->root('connections');
|
||||
|
||||
$node
|
||||
->requiresAtLeastOneElement()
|
||||
->useAttributeAsKey('name')
|
||||
->prototype('array')
|
||||
->children()
|
||||
->scalarNode('driver')
|
||||
->beforeNormalization()
|
||||
->always()
|
||||
->then(function ($v) { return str_replace('pdo_', '', $v); })
|
||||
->end()
|
||||
->defaultValue('mysql')
|
||||
->end()
|
||||
->scalarNode('user')->defaultValue('root')->end()
|
||||
->scalarNode('password')->defaultValue('')->end()
|
||||
->scalarNode('dsn')
|
||||
->beforeNormalization()
|
||||
->always()
|
||||
->then(function ($v) { return str_replace('pdo_', '', $v); })
|
||||
->end()
|
||||
->defaultValue('')
|
||||
->end()
|
||||
->scalarNode('classname')->defaultValue($this->debug ? '\Propel\Runtime\Connection\DebugPDO' : '\Propel\Runtime\Connection\ConnectionWrapper')->end()
|
||||
->arrayNode('slaves')
|
||||
->useAttributeAsKey('name')
|
||||
->prototype('array')
|
||||
->children()
|
||||
->scalarNode('driver')
|
||||
->beforeNormalization()
|
||||
->always()
|
||||
->then(function ($v) { return str_replace('pdo_', '', $v); })
|
||||
->end()
|
||||
->defaultValue('mysql')
|
||||
->end()
|
||||
->scalarNode('user')->defaultValue('root')->end()
|
||||
->scalarNode('password')->defaultValue('')->end()
|
||||
->scalarNode('dsn')
|
||||
->beforeNormalization()
|
||||
->always()
|
||||
->then(function ($v) { return str_replace('pdo_', '', $v); })
|
||||
->end()
|
||||
->defaultValue('')
|
||||
->end()
|
||||
->scalarNode('classname')->defaultValue($this->debug ? '\Propel\Runtime\Connection\DebugPDO' : '\Propel\Runtime\Connection\ConnectionWrapper')->end()
|
||||
->end()
|
||||
->end()
|
||||
->end()
|
||||
->end()
|
||||
->fixXmlConfig('option')
|
||||
->children()
|
||||
->arrayNode('options')
|
||||
->useAttributeAsKey('key')
|
||||
->prototype('scalar')->end()
|
||||
->end()
|
||||
->end()
|
||||
->fixXmlConfig('attribute')
|
||||
->children()
|
||||
->arrayNode('attributes')
|
||||
->useAttributeAsKey('key')
|
||||
->prototype('scalar')->end()
|
||||
->end()
|
||||
->end()
|
||||
->fixXmlConfig('setting')
|
||||
->children()
|
||||
->arrayNode('settings')
|
||||
->useAttributeAsKey('key')
|
||||
->prototype('scalar')->end()
|
||||
->end()
|
||||
->end()
|
||||
;
|
||||
|
||||
return $node;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -36,8 +36,9 @@ class PropelExtension extends Extension
|
|||
$configuration = $this->getConfiguration($configs, $container);
|
||||
$config = $processor->processConfiguration($configuration, $configs);
|
||||
|
||||
$container->setParameter('propel.logging', $config['logging']);
|
||||
$container->setParameter('propel.configuration', array());
|
||||
// @todo: restore
|
||||
$container->setParameter('propel.logging', true); //$config['logging']);
|
||||
$container->setParameter('propel.configuration', $config);
|
||||
|
||||
// Load services
|
||||
if (!$container->hasDefinition('propel')) {
|
||||
|
|
@ -46,69 +47,6 @@ class PropelExtension extends Extension
|
|||
$loader->load('converters.xml');
|
||||
$loader->load('security.xml');
|
||||
}
|
||||
|
||||
// build properties
|
||||
$buildProperties = $config['build_properties'];
|
||||
|
||||
// behaviors
|
||||
if (isset($config['behaviors']) && is_array($config['behaviors'])) {
|
||||
foreach ($config['behaviors'] as $name => $class) {
|
||||
$buildProperties[sprintf('propel.behavior.%s.class', $name)] = $class;
|
||||
}
|
||||
}
|
||||
|
||||
$container->setParameter('propel.build_properties', $buildProperties);
|
||||
|
||||
if (!empty($config['dbal'])) {
|
||||
$this->dbalLoad($config['dbal'], $container);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads the DBAL configuration.
|
||||
*
|
||||
* @param array $config An array of configuration settings
|
||||
* @param ContainerBuilder $container A ContainerBuilder instance
|
||||
*/
|
||||
protected function dbalLoad(array $config, ContainerBuilder $container)
|
||||
{
|
||||
if (empty($config['default_connection'])) {
|
||||
$keys = array_keys($config['connections']);
|
||||
$config['default_connection'] = reset($keys);
|
||||
}
|
||||
|
||||
$connectionName = $config['default_connection'];
|
||||
$container->setParameter('propel.dbal.default_connection', $connectionName);
|
||||
|
||||
if (0 === count($config['connections'])) {
|
||||
$config['connections'] = array($connectionName => $config);
|
||||
}
|
||||
|
||||
$c = array();
|
||||
foreach ($config['connections'] as $name => $conf) {
|
||||
$c[$name]['adapter'] = $conf['driver'];
|
||||
if (!empty($conf['slaves'])) {
|
||||
$c[$name]['slaves']['connection'] = $conf['slaves'];
|
||||
}
|
||||
|
||||
foreach (array('dsn', 'user', 'password', 'classname', 'options', 'attributes', 'settings', 'model_paths') as $att) {
|
||||
if (array_key_exists($att, $conf)) {
|
||||
$c[$name]['connection'][$att] = $conf[$att];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Alias the default connection if not defined
|
||||
if (!isset($c['default'])) {
|
||||
$c['default'] = $c[$connectionName];
|
||||
}
|
||||
|
||||
$container->setParameter('propel.configuration', $c);
|
||||
}
|
||||
|
||||
public function getConfiguration(array $config, ContainerBuilder $container)
|
||||
{
|
||||
return new Configuration($container->getParameter('kernel.debug'));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue