Permalink
Browse files

added a way to configure converters via app config

  • Loading branch information...
1 parent cfb1b28 commit 1b3bbfe0b1b2f3b42c6b542008c4210f5a933418 @docteurklein docteurklein committed Dec 3, 2011
View
59 DependencyInjection/Configuration.php
@@ -0,0 +1,59 @@
+<?php
+
+namespace GHub\PommBundle\DependencyInjection;
+
+use Symfony\Component\Config\Definition\Builder\TreeBuilder,
+ Symfony\Component\Config\Definition\ConfigurationInterface;
+
+/**
+ * Configuration for the bundle
+ */
+class Configuration implements ConfigurationInterface
+{
+ /**
+ * Generates the configuration tree.
+ *
+ * @return TreeBuilder
+ */
+ public function getConfigTreeBuilder()
+ {
+ $treeBuilder = new TreeBuilder();
+ $rootNode = $treeBuilder->root('g_hub_pomm');
+
+ $rootNode
+ ->beforeNormalization()
+ ->ifTrue(function($v) {
+ return isset($v['converters']) && is_array($v['converters']);
+ })
+ ->then(function($v) {
+ foreach ($v['databases'] as &$database) {
+ $database['converters'] = array_merge($v['converters'], @(array)$database['converters']);
+ }
+ unset($v['converters']);
+
+ return $v;
+ })
+ ->end()
+ ->children()
+ ->arrayNode('databases')
+ ->useAttributeAsKey('key')
+ ->requiresAtLeastOneElement()
+ ->prototype('array')
+ ->children()
+ ->scalarNode('dsn')->isRequired()->end()
+ ->scalarNode('class')->defaultValue('Pomm\Connection\Database')->end()
+ ->scalarNode('isolation')->defaultNull()->end()
+ ->arrayNode('converters')
+ ->useAttributeAsKey('class')
+ ->prototype('scalar')->end()
+ ->end()
+ ->end()
+ ->end()
+ ->end()
+ ->end()
+ ;
+
+ return $treeBuilder;
+ }
+}
+
View
27 DependencyInjection/GHubPommExtension.php
@@ -7,6 +7,9 @@
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
+use Symfony\Component\Config\Definition\Processor;
+use Symfony\Component\DependencyInjection\Definition;
+use Symfony\Component\DependencyInjection\Reference;
class GHubPommExtension implements ExtensionInterface
{
@@ -16,19 +19,25 @@ class GHubPommExtension implements ExtensionInterface
* @param array $config An array of configuration settings
* @param ContainerBuilder $container A ContainerBuilder instance
*/
- public function load(array $config, ContainerBuilder $container)
+ public function load(array $configs, ContainerBuilder $container)
{
- if (!$container->hasDefinition('pomm')) {
- $this->loadDefaults($container);
- }
+ $processor = new Processor();
+ $configuration = new Configuration();
+ $config = $processor->processConfiguration($configuration, $configs);
- $configuration = $config[0];
+ $this->loadDefaults($container);
- if (!array_key_exists('connections', $configuration) or !is_array($configuration['connections'])) {
- throw new InvalidArgumentException("Inexistant or invalid connections definition");
+ foreach ($config['databases'] as $name => $parameters) {
+ $id = sprintf('pomm.%s_database', $name);
+ $databaseDefinition = $container->setDefinition(
+ $id,
+ new Definition($parameters['class'], array($parameters))
+ );
+ foreach ($parameters['converters'] as $type => $converterClass) {
+ $databaseDefinition->addMethodCall('registerConverter', array($type, new Definition($converterClass)));
+ }
+ $container->getDefinition('pomm')->addMethodCall('setDatabase', array($name, new Reference($id)));
}
-
- $container->setParameter('pomm.connections', $configuration['connections']);
}
/**
View
4 Resources/config/pomm.yml
@@ -1,7 +1,3 @@
-parameters:
- connections: %pomm.connections%
-
services:
pomm:
class: Pomm\Service
- arguments: [%connections%]
View
37 Service/Service.php
@@ -1,37 +0,0 @@
-<?php
-
-namespace GHub\Bundle\PommBundle\Service;
-
-use Pomm\Pomm;
-
-class Service
-{
- protected $pomm_class;
-
- public function __construct($pomm_class, array $connections)
- {
- $this->pomm_class = $pomm_class;
-
- foreach ($connections as $name => $params)
- {
- $pomm_class::setDatabase($name, $params);
- }
- }
-
- public function getDatabase($name = null)
- {
- $pomm_class = $this->pomm_class;
-
- return $pomm_class::getDatabase($name);
- }
-
- public function getConnection($name = null)
- {
- return $this->getDatabase($name)->createConnection();
- }
-
- public function getTransaction($name = null)
- {
- return $this->getDatabase($name)->createTransaction();
- }
-}

0 comments on commit 1b3bbfe

Please sign in to comment.