From a7e5f81803c6d36aa9b81b25519b01f93492c3d8 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Mon, 7 Jun 2010 09:08:35 +0200 Subject: [PATCH] changed all framework extensions to take advantage of the new extension configuration inheritance from previous commit --- .../DependencyInjection/DoctrineExtension.php | 105 ++++++++---------- .../DoctrineBundle/Resources/config/dbal.xml | 1 + .../DoctrineBundle/Resources/config/orm.xml | 1 + .../DoctrineExtensionTest.php | 49 ++++++++ .../DependencyInjection/ProfilerExtension.php | 13 ++- .../ProfilerExtensionTest.php | 32 ++++++ .../DependencyInjection/PropelExtension.php | 76 ++++++++----- .../PropelBundle/Resources/config/propel.xml | 1 + .../PropelExtensionTest.php | 67 +++++++++++ .../SwiftmailerExtension.php | 44 ++++---- .../Resources/config/swiftmailer.xml | 1 + .../SwiftmailerExtensionTest.php | 33 ++++++ .../DependencyInjection/TwigExtension.php | 12 +- .../TwigBundle/Resources/config/twig.xml | 14 +-- .../DependencyInjection/TwigExtensionTest.php | 33 ++++++ .../DependencyInjection/WebExtension.php | 69 +++++++----- .../WebBundle/Resources/config/templating.xml | 2 + .../DependencyInjection/WebExtensionTest.php | 46 ++++++++ .../DependencyInjection/ZendExtension.php | 16 +-- .../DependencyInjection/ZendExtensionTest.php | 31 ++++++ 20 files changed, 486 insertions(+), 160 deletions(-) create mode 100644 src/Symfony/Framework/DoctrineBundle/Tests/DependencyInjection/DoctrineExtensionTest.php create mode 100644 src/Symfony/Framework/ProfilerBundle/Tests/DependencyInjection/ProfilerExtensionTest.php create mode 100644 src/Symfony/Framework/PropelBundle/Tests/DependencyInjection/PropelExtensionTest.php create mode 100644 src/Symfony/Framework/SwiftmailerBundle/Tests/DependencyInjection/SwiftmailerExtensionTest.php create mode 100644 src/Symfony/Framework/TwigBundle/Tests/DependencyInjection/TwigExtensionTest.php create mode 100644 src/Symfony/Framework/WebBundle/Tests/DependencyInjection/WebExtensionTest.php create mode 100644 src/Symfony/Framework/ZendBundle/Tests/DependencyInjection/ZendExtensionTest.php diff --git a/src/Symfony/Framework/DoctrineBundle/DependencyInjection/DoctrineExtension.php b/src/Symfony/Framework/DoctrineBundle/DependencyInjection/DoctrineExtension.php index fff947e58e55..705bfbc031d0 100644 --- a/src/Symfony/Framework/DoctrineBundle/DependencyInjection/DoctrineExtension.php +++ b/src/Symfony/Framework/DoctrineBundle/DependencyInjection/DoctrineExtension.php @@ -26,17 +26,17 @@ */ class DoctrineExtension extends LoaderExtension { - protected $resources = array( - 'dbal' => 'dbal.xml', - 'orm' => 'orm.xml', - ); - + protected $resources; protected $alias; protected $bundleDirs; protected $bundles; public function __construct(array $bundleDirs, array $bundles) { + $this->resources = array( + 'dbal' => 'dbal.xml', + 'orm' => 'orm.xml', + ); $this->bundleDirs = $bundleDirs; $this->bundles = $bundles; } @@ -57,12 +57,12 @@ public function setAlias($alias) * * @return BuilderConfiguration A BuilderConfiguration instance */ - public function dbalLoad($config) + public function dbalLoad($config, BuilderConfiguration $configuration) { - $configuration = new BuilderConfiguration(); - - $loader = new XmlFileLoader(__DIR__.'/../Resources/config'); - $configuration->merge($loader->load($this->resources['dbal'])); + if (!$configuration->hasDefinition('doctrine.dbal.logger')) { + $loader = new XmlFileLoader(__DIR__.'/../Resources/config'); + $configuration->merge($loader->load($this->resources['dbal'])); + } $defaultConnection = array( 'driver' => 'PDOMySql', @@ -76,7 +76,9 @@ public function dbalLoad($config) 'options' => array() ); - $config['default_connection'] = isset($config['default_connection']) ? $config['default_connection'] : 'default'; + $defaultConnectionName = isset($config['default_connection']) ? $config['default_connection'] : $configuration->getParameter('doctrine.dbal.default_connection'); + $configuration->setAlias('database_connection', null !== $this->alias ? $this->alias : sprintf('doctrine.dbal.%s_connection', $defaultConnectionName)); + $configuration->setParameter('doctrine.dbal.default_connection', $defaultConnectionName); $connections = array(); if (isset($config['connections'])) { @@ -84,35 +86,34 @@ public function dbalLoad($config) $connections[isset($connection['id']) ? $connection['id'] : $name] = $connection; } } else { - $connections = array($config['default_connection'] => $config); + $connections = array($defaultConnectionName => $config); } foreach ($connections as $name => $connection) { - $connection = array_merge($defaultConnection, $connection); - $configurationClass = isset($connection['configuration_class']) ? - $connection['configuration_class'] : 'Doctrine\DBAL\Configuration'; - - $configurationDef = new Definition($configurationClass); - $configurationDef->addMethodCall('setSqlLogger', array( - new Reference('doctrine.dbal.logger') - )); - $configuration->setDefinition( - sprintf('doctrine.dbal.%s_connection.configuration', $name), - $configurationDef - ); - - $eventManagerDef = new Definition($connection['event_manager_class']); - $configuration->setDefinition( - sprintf('doctrine.dbal.%s_connection.event_manager', $name), - $eventManagerDef - ); + // previously registered? + if ($configuration->hasDefinition(sprintf('doctrine.dbal.%s_connection', $name))) { + $driverDef = $configuration->getDefinition(sprintf('doctrine.dbal.%s_connection', $name)); + $arguments = $driverDef->getArguments(); + $driverOptions = $arguments[0]; + } else { + $connection = array_merge($defaultConnection, $connection); + + $configurationClass = isset($connection['configuration_class']) ? $connection['configuration_class'] : 'Doctrine\DBAL\Configuration'; + $configurationDef = new Definition($configurationClass); + $configurationDef->addMethodCall('setSqlLogger', array(new Reference('doctrine.dbal.logger'))); + $configuration->setDefinition(sprintf('doctrine.dbal.%s_connection.configuration', $name), $configurationDef); + + $eventManagerDef = new Definition($connection['event_manager_class']); + $configuration->setDefinition(sprintf('doctrine.dbal.%s_connection.event_manager', $name), $eventManagerDef); + + $driverOptions = array(); + $driverDef = new Definition('Doctrine\DBAL\DriverManager'); + $driverDef->setConstructor('getConnection'); + $configuration->setDefinition(sprintf('doctrine.dbal.%s_connection', $name), $driverDef); + } - $driverOptions = array(); if (isset($connection['driver'])) { - $driverOptions['driverClass'] = sprintf( - 'Doctrine\\DBAL\\Driver\\%s\\Driver', - $connection['driver'] - ); + $driverOptions['driverClass'] = sprintf('Doctrine\\DBAL\\Driver\\%s\\Driver', $connection['driver']); } if (isset($connection['wrapper_class'])) { $driverOptions['wrapperClass'] = $connection['wrapper_class']; @@ -125,22 +126,14 @@ public function dbalLoad($config) $driverOptions[$key] = $connection[$key]; } } - $driverArgs = array( + + $driverDef->setArguments(array( $driverOptions, new Reference(sprintf('doctrine.dbal.%s_connection.configuration', $name)), new Reference(sprintf('doctrine.dbal.%s_connection.event_manager', $name)) - ); - $driverDef = new Definition('Doctrine\DBAL\DriverManager', $driverArgs); - $driverDef->setConstructor('getConnection'); - $configuration->setDefinition(sprintf('doctrine.dbal.%s_connection', $name), $driverDef); + )); } - $configuration->setAlias('database_connection', - null !== $this->alias ? $this->alias : sprintf( - 'doctrine.dbal.%s_connection', $config['default_connection'] - ) - ); - return $configuration; } @@ -151,28 +144,26 @@ public function dbalLoad($config) * * @return BuilderConfiguration A BuilderConfiguration instance */ - public function ormLoad($config) + public function ormLoad($config, BuilderConfiguration $configuration) { - $configuration = new BuilderConfiguration(); - $loader = new XmlFileLoader(__DIR__.'/../Resources/config'); $configuration->merge($loader->load($this->resources['orm'])); - $config['default_entity_manager'] = isset($config['default_entity_manager']) ? $config['default_entity_manager'] : 'default'; + if (isset($config['default_entity_manager'])) { + $configuration->getParameter('doctrine.orm.default_entity_manager', $config['default_entity_manager']); + } + $defaultEntityManager = $configuration->getParameter('doctrine.orm.default_entity_manager'); + foreach (array('metadata_driver', 'cache_driver') as $key) { if (isset($config[$key])) { $configuration->setParameter('doctrine.orm.'.$key, $config[$key]); } } - $config['entity_managers'] = isset($config['entity_managers']) ? - $config['entity_managers'] : array($config['default_entity_manager'] => array()) - ; + $config['entity_managers'] = isset($config['entity_managers']) ? $config['entity_managers'] : array($defaultEntityManager => array()); foreach ($config['entity_managers'] as $name => $connection) { $ormConfigDef = new Definition('Doctrine\ORM\Configuration'); - $configuration->setDefinition( - sprintf('doctrine.orm.%s_configuration', $name), $ormConfigDef - ); + $configuration->setDefinition(sprintf('doctrine.orm.%s_configuration', $name), $ormConfigDef); $drivers = array('metadata', 'query', 'result'); foreach ($drivers as $driver) { @@ -249,7 +240,7 @@ public function ormLoad($config) $ormEmDef ); - if ($name == $config['default_entity_manager']) { + if ($name == $defaultEntityManager) { $configuration->setAlias( 'doctrine.orm.entity_manager', sprintf('doctrine.orm.%s_entity_manager', $name) diff --git a/src/Symfony/Framework/DoctrineBundle/Resources/config/dbal.xml b/src/Symfony/Framework/DoctrineBundle/Resources/config/dbal.xml index 9bb139ffdc3e..bd6c03e746eb 100644 --- a/src/Symfony/Framework/DoctrineBundle/Resources/config/dbal.xml +++ b/src/Symfony/Framework/DoctrineBundle/Resources/config/dbal.xml @@ -6,6 +6,7 @@ Symfony\Framework\DoctrineBundle\DataCollector\DoctrineDataCollector + default diff --git a/src/Symfony/Framework/DoctrineBundle/Resources/config/orm.xml b/src/Symfony/Framework/DoctrineBundle/Resources/config/orm.xml index 0df8dd63e92d..1ba7b80a69f3 100644 --- a/src/Symfony/Framework/DoctrineBundle/Resources/config/orm.xml +++ b/src/Symfony/Framework/DoctrineBundle/Resources/config/orm.xml @@ -8,6 +8,7 @@ array localhost 11211 + default diff --git a/src/Symfony/Framework/DoctrineBundle/Tests/DependencyInjection/DoctrineExtensionTest.php b/src/Symfony/Framework/DoctrineBundle/Tests/DependencyInjection/DoctrineExtensionTest.php new file mode 100644 index 000000000000..e155b3b9778f --- /dev/null +++ b/src/Symfony/Framework/DoctrineBundle/Tests/DependencyInjection/DoctrineExtensionTest.php @@ -0,0 +1,49 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Framework\DoctrineBundle\Tests\DependencyInjection; + +use Symfony\Framework\DoctrineBundle\Tests\TestCase; +use Symfony\Framework\DoctrineBundle\DependencyInjection\DoctrineExtension; +use Symfony\Components\DependencyInjection\BuilderConfiguration; + +class DoctrineExtensionTest extends TestCase +{ + public function testDbalLoad() + { + $configuration = new BuilderConfiguration(); + $loader = new DoctrineExtension(array(), array()); + + $configuration = $loader->dbalLoad(array(), $configuration); + $this->assertEquals('Symfony\\Framework\\DoctrineBundle\\DataCollector\\DoctrineDataCollector', $configuration->getParameter('doctrine.data_collector.class'), '->dbalLoad() loads the dbal.xml file if not already loaded'); + + // doctrine.dbal.default_connection + $this->assertEquals('default', $configuration->getParameter('doctrine.dbal.default_connection'), '->dbalLoad() overrides existing configuration options'); + $configuration = $loader->dbalLoad(array('default_connection' => 'foo'), $configuration); + $this->assertEquals('foo', $configuration->getParameter('doctrine.dbal.default_connection'), '->dbalLoad() overrides existing configuration options'); + $configuration = $loader->dbalLoad(array(), $configuration); + $this->assertEquals('foo', $configuration->getParameter('doctrine.dbal.default_connection'), '->dbalLoad() overrides existing configuration options'); + + $configuration = new BuilderConfiguration(); + $loader = new DoctrineExtension(array(), array()); + $configuration = $loader->dbalLoad(array('password' => 'foo'), $configuration); + + $arguments = $configuration->getDefinition('doctrine.dbal.default_connection')->getArguments(); + $config = $arguments[0]; + + $this->assertEquals('foo', $config['password']); + $this->assertEquals('root', $config['user']); + + $configuration = $loader->dbalLoad(array('user' => 'foo'), $configuration); + $this->assertEquals('foo', $config['password']); + $this->assertEquals('root', $config['user']); + } +} diff --git a/src/Symfony/Framework/ProfilerBundle/DependencyInjection/ProfilerExtension.php b/src/Symfony/Framework/ProfilerBundle/DependencyInjection/ProfilerExtension.php index 44a861f902b8..890d6a1c0e28 100644 --- a/src/Symfony/Framework/ProfilerBundle/DependencyInjection/ProfilerExtension.php +++ b/src/Symfony/Framework/ProfilerBundle/DependencyInjection/ProfilerExtension.php @@ -24,14 +24,15 @@ */ class ProfilerExtension extends LoaderExtension { - public function configLoad($config) + public function configLoad($config, BuilderConfiguration $configuration) { - $configuration = new BuilderConfiguration(); - - $loader = new XmlFileLoader(__DIR__.'/../Resources/config'); - $configuration->merge($loader->load('collectors.xml')); + if (!$configuration->hasDefinition('data_collector_manager')) { + $loader = new XmlFileLoader(__DIR__.'/../Resources/config'); + $configuration->merge($loader->load('collectors.xml')); + } - if (isset($config['toolbar']) && $config['toolbar']) { + if (isset($config['toolbar']) && $config['toolbar'] && !$configuration->hasDefinition('debug.toolbar')) { + $loader = new XmlFileLoader(__DIR__.'/../Resources/config'); $configuration->merge($loader->load('toolbar.xml')); } diff --git a/src/Symfony/Framework/ProfilerBundle/Tests/DependencyInjection/ProfilerExtensionTest.php b/src/Symfony/Framework/ProfilerBundle/Tests/DependencyInjection/ProfilerExtensionTest.php new file mode 100644 index 000000000000..5fe77d9e1b1f --- /dev/null +++ b/src/Symfony/Framework/ProfilerBundle/Tests/DependencyInjection/ProfilerExtensionTest.php @@ -0,0 +1,32 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Framework\ProfilerBundle\Tests\DependencyInjection; + +use Symfony\Framework\ProfilerBundle\Tests\TestCase; +use Symfony\Framework\ProfilerBundle\DependencyInjection\ProfilerExtension; +use Symfony\Components\DependencyInjection\BuilderConfiguration; + +class ProfilerExtensionTest extends TestCase +{ + public function testLoggerLoad() + { + $configuration = new BuilderConfiguration(); + $loader = new ProfilerExtension(); + + $configuration = $loader->configLoad(array(), $configuration); + $this->assertEquals('Symfony\\Framework\\ProfilerBundle\\DataCollector\\DataCollectorManager', $configuration->getParameter('data_collector_manager.class'), '->configLoad() loads the collectors.xml file if not already loaded'); + $this->assertFalse($configuration->hasParameter('debug.toolbar.class'), '->configLoad() does not load the toolbar.xml file'); + + $configuration = $loader->configLoad(array('toolbar' => true), $configuration); + $this->assertEquals('Symfony\\Framework\\ProfilerBundle\\Listener\\WebDebugToolbar', $configuration->getParameter('debug.toolbar.class'), '->configLoad() loads the collectors.xml file if the toolbar option is given'); + } +} diff --git a/src/Symfony/Framework/PropelBundle/DependencyInjection/PropelExtension.php b/src/Symfony/Framework/PropelBundle/DependencyInjection/PropelExtension.php index 08d181e51eb7..2b6a34d68d03 100644 --- a/src/Symfony/Framework/PropelBundle/DependencyInjection/PropelExtension.php +++ b/src/Symfony/Framework/PropelBundle/DependencyInjection/PropelExtension.php @@ -21,14 +21,20 @@ class PropelExtension extends LoaderExtension * * @return BuilderConfiguration A BuilderConfiguration instance */ - public function configLoad($config) + public function configLoad($config, BuilderConfiguration $configuration) { - if (!isset($config['path'])) { - throw new \InvalidArgumentException('The "path" parameter is mandatory.'); + if (!$configuration->hasParameter('propel.path')) { + if (!isset($config['path'])) { + throw new \InvalidArgumentException('The "path" parameter is mandatory.'); + } + + $configuration->setParameter('propel.path', $config['path']); } - $configuration = new BuilderConfiguration(); - $configuration->setParameter('propel.path', $config['path']); + if (isset($config['path'])) + { + $configuration->setParameter('propel.path', $config['path']); + } if (isset($config['phing_path'])) { @@ -45,12 +51,12 @@ public function configLoad($config) * * @return BuilderConfiguration A BuilderConfiguration instance */ - public function dbalLoad($config) + public function dbalLoad($config, BuilderConfiguration $configuration) { - $configuration = new BuilderConfiguration(); - - $loader = new XmlFileLoader(__DIR__.'/../Resources/config'); - $configuration->merge($loader->load($this->resources['propel'])); + if (!$configuration->hasDefinition('propel')) { + $loader = new XmlFileLoader(__DIR__.'/../Resources/config'); + $configuration->merge($loader->load($this->resources['propel'])); + } $defaultConnection = array( 'driver' => 'mysql', @@ -65,7 +71,8 @@ public function dbalLoad($config) 'settings' => array('charset' => array('value' => 'UTF8')), ); - $config['default_connection'] = isset($config['default_connection']) ? $config['default_connection'] : 'default'; + $defaultConnectionName = isset($config['default_connection']) ? $config['default_connection'] : $configuration->getParameter('propel.dbal.default_connection'); + $configuration->setParameter('propel.dbal.default_connection', $defaultConnectionName); $connections = array(); if (isset($config['connections'])) { @@ -73,31 +80,40 @@ public function dbalLoad($config) $connections[isset($connection['id']) ? $connection['id'] : $name] = $connection; } } else { - $connections = array($config['default_connection'] => $config); + $connections = array($defaultConnectionName => $config); } - $c = array( + $arguments = $configuration->getDefinition('propel.configuration')->getArguments(); + if (count($arguments)) { + $c = $arguments[0]; + } else { + $c = array( // FIXME: should be the same value as %zend.logger.priority% - 'log' => array('level' => 7), - 'datasources' => array(), - ); - foreach ($connections as $name => $connection) { - $connection = array_replace($defaultConnection, $connection); - - $c['datasources'][$name] = array( - 'adapter' => $connection['driver'], - 'connection' => array( - 'dsn' => $connection['dsn'], - 'user' => $connection['user'], - 'password' => $connection['password'], - 'classname' => $connection['classname'], - 'options' => $connection['options'], - 'attributes' => $connection['attributes'], - 'settings' => $connection['settings'], - ), + 'log' => array('level' => 7), + 'datasources' => array(), ); } + foreach ($connections as $name => $connection) { + if (isset($c['datasources'][$name])) { + } else { + $connection = array_replace($defaultConnection, $connection); + + $c['datasources'][$name] = array( + 'connection' => array(), + ); + } + + if (isset($connection['driver'])) { + $c['datasources'][$name]['adapter'] = $connection['driver']; + } + foreach (array('dsn', 'user', 'password', 'classname', 'options', 'attributes', 'settings') as $att) { + if (isset($connection[$att])) { + $c['datasources'][$name]['connection'][$att] = $connection[$att]; + } + } + } + $configuration->getDefinition('propel.configuration')->setArguments(array($c)); return $configuration; diff --git a/src/Symfony/Framework/PropelBundle/Resources/config/propel.xml b/src/Symfony/Framework/PropelBundle/Resources/config/propel.xml index 9d54b9944686..8fd78f6acdf0 100644 --- a/src/Symfony/Framework/PropelBundle/Resources/config/propel.xml +++ b/src/Symfony/Framework/PropelBundle/Resources/config/propel.xml @@ -8,6 +8,7 @@ Propel PropelConfiguration Symfony\Framework\PropelBundle\Logger\PropelLogger + default diff --git a/src/Symfony/Framework/PropelBundle/Tests/DependencyInjection/PropelExtensionTest.php b/src/Symfony/Framework/PropelBundle/Tests/DependencyInjection/PropelExtensionTest.php new file mode 100644 index 000000000000..51b00fe92767 --- /dev/null +++ b/src/Symfony/Framework/PropelBundle/Tests/DependencyInjection/PropelExtensionTest.php @@ -0,0 +1,67 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Framework\PropelBundle\Tests\DependencyInjection; + +use Symfony\Framework\PropelBundle\Tests\TestCase; +use Symfony\Framework\PropelBundle\DependencyInjection\PropelExtension; +use Symfony\Components\DependencyInjection\BuilderConfiguration; + +class PropelExtensionTest extends TestCase +{ + public function testConfigLoad() + { + $configuration = new BuilderConfiguration(); + $loader = new PropelExtension(); + + try { + $configuration = $loader->configLoad(array(), $configuration); + $this->fail(); + } catch (\Exception $e) { + $this->assertInstanceOf('InvalidArgumentException', $e, '->configLoad() throws an \InvalidArgumentException if the Propel path is not set.'); + } + + $configuration = $loader->configLoad(array('path' => '/propel'), $configuration); + $this->assertEquals('/propel', $configuration->getParameter('propel.path'), '->configLoad() sets the Propel path'); + + $configuration = $loader->configLoad(array(), $configuration); + $this->assertEquals('/propel', $configuration->getParameter('propel.path'), '->configLoad() sets the Propel path'); + } + + public function testDbalLoad() + { + $configuration = new BuilderConfiguration(); + $loader = new PropelExtension(); + + $configuration = $loader->dbalLoad(array(), $configuration); + $this->assertEquals('Propel', $configuration->getParameter('propel.class'), '->dbalLoad() loads the propel.xml file if not already loaded'); + + // propel.dbal.default_connection + $this->assertEquals('default', $configuration->getParameter('propel.dbal.default_connection'), '->dbalLoad() overrides existing configuration options'); + $configuration = $loader->dbalLoad(array('default_connection' => 'foo'), $configuration); + $this->assertEquals('foo', $configuration->getParameter('propel.dbal.default_connection'), '->dbalLoad() overrides existing configuration options'); + $configuration = $loader->dbalLoad(array(), $configuration); + $this->assertEquals('foo', $configuration->getParameter('propel.dbal.default_connection'), '->dbalLoad() overrides existing configuration options'); + + $configuration = new BuilderConfiguration(); + $loader = new PropelExtension(); + $configuration = $loader->dbalLoad(array('password' => 'foo'), $configuration); + + $arguments = $configuration->getDefinition('propel.configuration')->getArguments(); + $config = $arguments[0]; + $this->assertEquals('foo', $config['datasources']['default']['connection']['password']); + $this->assertEquals('root', $config['datasources']['default']['connection']['user']); + + $configuration = $loader->dbalLoad(array('user' => 'foo'), $configuration); + $this->assertEquals('foo', $config['datasources']['default']['connection']['password']); + $this->assertEquals('root', $config['datasources']['default']['connection']['user']); + } +} diff --git a/src/Symfony/Framework/SwiftmailerBundle/DependencyInjection/SwiftmailerExtension.php b/src/Symfony/Framework/SwiftmailerBundle/DependencyInjection/SwiftmailerExtension.php index c2f1982749e9..eae01bba5725 100644 --- a/src/Symfony/Framework/SwiftmailerBundle/DependencyInjection/SwiftmailerExtension.php +++ b/src/Symfony/Framework/SwiftmailerBundle/DependencyInjection/SwiftmailerExtension.php @@ -44,25 +44,31 @@ class SwiftMailerExtension extends LoaderExtension * * @return BuilderConfiguration A BuilderConfiguration instance */ - public function mailerLoad($config) + public function mailerLoad($config, BuilderConfiguration $configuration) { - $configuration = new BuilderConfiguration(); - - $loader = new XmlFileLoader(__DIR__.'/../Resources/config'); - $configuration->merge($loader->load($this->resources['mailer'])); - - if (isset($config['transport']) && null === $config['transport']) { - $config['transport'] = 'null'; - } elseif (!isset($config['transport'])) { - $config['transport'] = 'smtp'; - } elseif ('gmail' === $config['transport']) { - $config['encryption'] = 'ssl'; - $config['auth_mode'] = 'login'; - $config['host'] = 'smtp.gmail.com'; - $config['transport'] = 'smtp'; + if (!$configuration->hasDefinition('swiftmailer.mailer')) { + $loader = new XmlFileLoader(__DIR__.'/../Resources/config'); + $configuration->merge($loader->load($this->resources['mailer'])); + $configuration->setAlias('mailer', 'swiftmailer.mailer'); } - $configuration->setAlias('swiftmailer.transport', 'swiftmailer.transport.'.$config['transport']); + $transport = $configuration->getParameter('swiftmailer.transport.name'); + if (array_key_exists('transport', $config)) { + if (null === $config['transport']) { + $transport = 'null'; + } elseif ('gmail' === $config['transport']) { + $config['encryption'] = 'ssl'; + $config['auth_mode'] = 'login'; + $config['host'] = 'smtp.gmail.com'; + $transport = 'smtp'; + } else { + $transport = $config['transport']; + } + + $configuration->setParameter('swiftmailer.transport.name', $transport); + } + + $configuration->setAlias('swiftmailer.transport', 'swiftmailer.transport.'.$transport); if (isset($config['encryption']) && 'ssl' === $config['encryption'] && !isset($config['port'])) { $config['port'] = 465; @@ -70,7 +76,7 @@ public function mailerLoad($config) foreach (array('encryption', 'port', 'host', 'username', 'password', 'auth_mode') as $key) { if (isset($config[$key])) { - $configuration->setParameter('swiftmailer.transport.'.$config['transport'].'.'.$key, $config[$key]); + $configuration->setParameter('swiftmailer.transport.'.$transport.'.'.$key, $config[$key]); } } @@ -78,7 +84,7 @@ public function mailerLoad($config) if (isset($config['spool'])) { $type = isset($config['type']) ? $config['type'] : 'file'; - $configuration->setAlias('swiftmailer.transport.real', 'swiftmailer.transport.'.$config['transport']); + $configuration->setAlias('swiftmailer.transport.real', 'swiftmailer.transport.'.$transport); $configuration->setAlias('swiftmailer.transport', 'swiftmailer.transport.spool'); $configuration->setAlias('swiftmailer.spool', 'swiftmailer.spool.'.$type); @@ -98,8 +104,6 @@ public function mailerLoad($config) $configuration->findDefinition('swiftmailer.transport')->addMethodCall('registerPlugin', array(new Reference('swiftmailer.plugin.blackhole'))); } - $configuration->setAlias('mailer', 'swiftmailer.mailer'); - return $configuration; } diff --git a/src/Symfony/Framework/SwiftmailerBundle/Resources/config/swiftmailer.xml b/src/Symfony/Framework/SwiftmailerBundle/Resources/config/swiftmailer.xml index 4213b177a17a..e1c77e91e5e0 100644 --- a/src/Symfony/Framework/SwiftmailerBundle/Resources/config/swiftmailer.xml +++ b/src/Symfony/Framework/SwiftmailerBundle/Resources/config/swiftmailer.xml @@ -7,6 +7,7 @@ Swift_Mailer + smtp Swift_Transport_EsmtpTransport Swift_Transport_SendmailTransport Swift_Transport_MailTransport diff --git a/src/Symfony/Framework/SwiftmailerBundle/Tests/DependencyInjection/SwiftmailerExtensionTest.php b/src/Symfony/Framework/SwiftmailerBundle/Tests/DependencyInjection/SwiftmailerExtensionTest.php new file mode 100644 index 000000000000..e5d4fbcc5d21 --- /dev/null +++ b/src/Symfony/Framework/SwiftmailerBundle/Tests/DependencyInjection/SwiftmailerExtensionTest.php @@ -0,0 +1,33 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Framework\SwiftmailerBundle\Tests\DependencyInjection; + +use Symfony\Framework\SwiftmailerBundle\Tests\TestCase; +use Symfony\Framework\SwiftmailerBundle\DependencyInjection\SwiftmailerExtension; +use Symfony\Components\DependencyInjection\BuilderConfiguration; + +class SwiftmailerExtensionTest extends TestCase +{ + public function testMailerLoad() + { + $configuration = new BuilderConfiguration(); + $loader = new SwiftmailerExtension(); + + $configuration = $loader->mailerLoad(array(), $configuration); + $this->assertEquals('Swift_Mailer', $configuration->getParameter('swiftmailer.class'), '->mailerLoad() loads the swiftmailer.xml file if not already loaded'); + + $configuration = $loader->mailerLoad(array('transport' => 'sendmail'), $configuration); + $this->assertEquals('sendmail', $configuration->getParameter('swiftmailer.transport.name'), '->mailerLoad() overrides existing configuration options'); + $configuration = $loader->mailerLoad(array(), $configuration); + $this->assertEquals('sendmail', $configuration->getParameter('swiftmailer.transport.name'), '->mailerLoad() overrides existing configuration options'); + } +} diff --git a/src/Symfony/Framework/TwigBundle/DependencyInjection/TwigExtension.php b/src/Symfony/Framework/TwigBundle/DependencyInjection/TwigExtension.php index 19b7b483dc39..301c8feb73dd 100644 --- a/src/Symfony/Framework/TwigBundle/DependencyInjection/TwigExtension.php +++ b/src/Symfony/Framework/TwigBundle/DependencyInjection/TwigExtension.php @@ -24,14 +24,14 @@ */ class TwigExtension extends LoaderExtension { - public function configLoad($config) + public function configLoad($config, BuilderConfiguration $configuration) { - $configuration = new BuilderConfiguration(); + if (!$configuration->hasDefinition('twig')) { + $loader = new XmlFileLoader(__DIR__.'/../Resources/config'); + $configuration->merge($loader->load('twig.xml')); + } - $loader = new XmlFileLoader(__DIR__.'/../Resources/config'); - $configuration->merge($loader->load('twig.xml')); - - $configuration->setParameter('twig_options', array_replace($configuration->getParameter('twig_options'), $config)); + $configuration->setParameter('twig.options', array_replace($configuration->getParameter('twig.options'), $config)); return $configuration; } diff --git a/src/Symfony/Framework/TwigBundle/Resources/config/twig.xml b/src/Symfony/Framework/TwigBundle/Resources/config/twig.xml index 563b7d125982..cc4909602f81 100644 --- a/src/Symfony/Framework/TwigBundle/Resources/config/twig.xml +++ b/src/Symfony/Framework/TwigBundle/Resources/config/twig.xml @@ -6,29 +6,29 @@ Symfony\Framework\TwigBundle\Environment - + %kernel.charset% %kernel.debug% %kernel.cache_dir%/twig - Symfony\Framework\TwigBundle\Loader\Loader - Symfony\Framework\TwigBundle\Renderer\Renderer + Symfony\Framework\TwigBundle\Loader\Loader + Symfony\Framework\TwigBundle\Renderer\Renderer - - %twig_options% + + %twig.options% - + - + diff --git a/src/Symfony/Framework/TwigBundle/Tests/DependencyInjection/TwigExtensionTest.php b/src/Symfony/Framework/TwigBundle/Tests/DependencyInjection/TwigExtensionTest.php new file mode 100644 index 000000000000..2d1a8c25992f --- /dev/null +++ b/src/Symfony/Framework/TwigBundle/Tests/DependencyInjection/TwigExtensionTest.php @@ -0,0 +1,33 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Framework\TwigBundle\Tests\DependencyInjection; + +use Symfony\Framework\TwigBundle\Tests\TestCase; +use Symfony\Framework\TwigBundle\DependencyInjection\TwigExtension; +use Symfony\Components\DependencyInjection\BuilderConfiguration; + +class TwigExtensionTest extends TestCase +{ + public function testConfigLoad() + { + $configuration = new BuilderConfiguration(); + $loader = new TwigExtension(); + + $configuration = $loader->configLoad(array(), $configuration); + $this->assertEquals('Symfony\\Framework\\TwigBundle\\Environment', $configuration->getParameter('twig.class'), '->configLoad() loads the twig.xml file if not already loaded'); + + $configuration = $loader->configLoad(array('charset' => 'ISO-8859-1'), $configuration); + $options = $configuration->getParameter('twig.options'); + $this->assertEquals('ISO-8859-1', $options['charset'], '->configLoad() overrides existing configuration options'); + $this->assertEquals('%kernel.debug%', $options['debug'], '->configLoad() merges the new values with the old ones'); + } +} diff --git a/src/Symfony/Framework/WebBundle/DependencyInjection/WebExtension.php b/src/Symfony/Framework/WebBundle/DependencyInjection/WebExtension.php index e5f8625a555a..862c99f865c5 100644 --- a/src/Symfony/Framework/WebBundle/DependencyInjection/WebExtension.php +++ b/src/Symfony/Framework/WebBundle/DependencyInjection/WebExtension.php @@ -31,12 +31,20 @@ class WebExtension extends LoaderExtension 'user' => 'user.xml', ); - public function webLoad($config) + /** + * Loads the web configuration. + * + * @param array $config A configuration array + * @param BuilderConfiguration $configuration A BuilderConfiguration instance + * + * @return BuilderConfiguration A BuilderConfiguration instance + */ + public function webLoad($config, BuilderConfiguration $configuration) { - $configuration = new BuilderConfiguration(); - - $loader = new XmlFileLoader(__DIR__.'/../Resources/config'); - $configuration->merge($loader->load($this->resources['web'])); + if (!$configuration->hasDefinition('controller_manager')) { + $loader = new XmlFileLoader(__DIR__.'/../Resources/config'); + $configuration->merge($loader->load($this->resources['web'])); + } if (isset($config['ide']) && 'textmate' === $config['ide']) { $configuration->setParameter('debug.file_link_format', 'txmt://open?url=file://%%f&line=%%l'); @@ -45,12 +53,20 @@ public function webLoad($config) return $configuration; } - public function userLoad($config) + /** + * Loads the user configuration. + * + * @param array $config A configuration array + * @param BuilderConfiguration $configuration A BuilderConfiguration instance + * + * @return BuilderConfiguration A BuilderConfiguration instance + */ + public function userLoad($config, BuilderConfiguration $configuration) { - $configuration = new BuilderConfiguration(); - - $loader = new XmlFileLoader(__DIR__.'/../Resources/config'); - $configuration->merge($loader->load($this->resources['user'])); + if (!$configuration->hasDefinition('user')) { + $loader = new XmlFileLoader(__DIR__.'/../Resources/config'); + $configuration->merge($loader->load($this->resources['user'])); + } if (isset($config['default_culture'])) { $configuration->setParameter('user.default_culture', $config['default_culture']); @@ -85,15 +101,20 @@ public function userLoad($config) * * @return BuilderConfiguration A BuilderConfiguration instance */ - public function templatingLoad($config) + public function templatingLoad($config, BuilderConfiguration $configuration) { - $configuration = new BuilderConfiguration(); + if (!$configuration->hasDefinition('templating')) { + $loader = new XmlFileLoader(__DIR__.'/../Resources/config'); + $configuration->merge($loader->load($this->resources['templating'])); + } - $loader = new XmlFileLoader(__DIR__.'/../Resources/config'); - $configuration->merge($loader->load($this->resources['templating'])); + if (array_key_exists('escaping', $config)) { + $configuration->setParameter('templating.output_escaper', $config['escaping']); + } - $configuration->setParameter('templating.output_escaper', array_key_exists('escaping', $config) ? $config['escaping'] : false); - $configuration->setParameter('templating.assets.version', array_key_exists('assets_version', $config) ? $config['assets_version'] : null); + if (array_key_exists('assets_version', $config)) { + $configuration->setParameter('templating.assets.version', $config['assets_version']); + } // path for the filesystem loader if (isset($config['path'])) { @@ -107,17 +128,13 @@ public function templatingLoad($config) foreach ($ids as $id) { $loaders[] = new Reference($id); } - } else { - $loaders = array( - new Reference('templating.loader.filesystem'), - ); - } - if (1 === count($loaders)) { - $configuration->setAlias('templating.loader', (string) $loaders[0]); - } else { - $configuration->getDefinition('templating.loader.chain')->addArgument($loaders); - $configuration->setAlias('templating.loader', 'templating.loader.chain'); + if (1 === count($loaders)) { + $configuration->setAlias('templating.loader', (string) $loaders[0]); + } else { + $configuration->getDefinition('templating.loader.chain')->addArgument($loaders); + $configuration->setAlias('templating.loader', 'templating.loader.chain'); + } } // cache? diff --git a/src/Symfony/Framework/WebBundle/Resources/config/templating.xml b/src/Symfony/Framework/WebBundle/Resources/config/templating.xml index eca249841f8a..cffcbe64e42b 100644 --- a/src/Symfony/Framework/WebBundle/Resources/config/templating.xml +++ b/src/Symfony/Framework/WebBundle/Resources/config/templating.xml @@ -17,6 +17,8 @@ Symfony\Framework\WebBundle\Helper\RouterHelper Symfony\Framework\WebBundle\Helper\RequestHelper Symfony\Framework\WebBundle\Helper\UserHelper + false + null diff --git a/src/Symfony/Framework/WebBundle/Tests/DependencyInjection/WebExtensionTest.php b/src/Symfony/Framework/WebBundle/Tests/DependencyInjection/WebExtensionTest.php new file mode 100644 index 000000000000..42a5e4965151 --- /dev/null +++ b/src/Symfony/Framework/WebBundle/Tests/DependencyInjection/WebExtensionTest.php @@ -0,0 +1,46 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Framework\WebBundle\Tests\DependencyInjection; + +use Symfony\Framework\WebBundle\Tests\TestCase; +use Symfony\Framework\WebBundle\DependencyInjection\WebExtension; +use Symfony\Components\DependencyInjection\BuilderConfiguration; + +class WebExtensionTest extends TestCase +{ + public function testWebLoad() + { + $configuration = new BuilderConfiguration(); + $loader = new WebExtension(); + + $configuration = $loader->webLoad(array(), $configuration); + $this->assertEquals('Symfony\\Framework\\WebBundle\\Listener\\RequestParser', $configuration->getParameter('request_parser.class'), '->webLoad() loads the web.xml file if not already loaded'); + } + + public function testUserLoad() + { + $configuration = new BuilderConfiguration(); + $loader = new WebExtension(); + + $configuration = $loader->userLoad(array(), $configuration); + $this->assertEquals('Symfony\\Framework\\WebBundle\\User', $configuration->getParameter('user.class'), '->userLoad() loads the user.xml file if not already loaded'); + } + + public function testTemplatingLoad() + { + $configuration = new BuilderConfiguration(); + $loader = new WebExtension(); + + $configuration = $loader->templatingLoad(array(), $configuration); + $this->assertEquals('Symfony\\Framework\\WebBundle\\Templating\\Engine', $configuration->getParameter('templating.engine.class'), '->templatingLoad() loads the templating.xml file if not already loaded'); + } +} diff --git a/src/Symfony/Framework/ZendBundle/DependencyInjection/ZendExtension.php b/src/Symfony/Framework/ZendBundle/DependencyInjection/ZendExtension.php index d7ae1e5584b3..ad8d37a13c08 100644 --- a/src/Symfony/Framework/ZendBundle/DependencyInjection/ZendExtension.php +++ b/src/Symfony/Framework/ZendBundle/DependencyInjection/ZendExtension.php @@ -35,16 +35,18 @@ class ZendExtension extends LoaderExtension * * * - * @param array $config A configuration array + * @param array $config A configuration array + * @param BuilderConfiguration $configuration A BuilderConfiguration instance * * @return BuilderConfiguration A BuilderConfiguration instance */ - public function loggerLoad($config) + public function loggerLoad($config, BuilderConfiguration $configuration) { - $configuration = new BuilderConfiguration(); - - $loader = new XmlFileLoader(__DIR__.'/../Resources/config'); - $configuration->merge($loader->load($this->resources['logger'])); + if (!$configuration->hasDefinition('zend.logger')) { + $loader = new XmlFileLoader(__DIR__.'/../Resources/config'); + $configuration->merge($loader->load($this->resources['logger'])); + $configuration->setAlias('logger', 'zend.logger'); + } if (isset($config['priority'])) { $configuration->setParameter('zend.logger.priority', is_int($config['priority']) ? $config['priority'] : constant('\Zend_Log::'.strtoupper($config['priority']))); @@ -54,8 +56,6 @@ public function loggerLoad($config) $configuration->setParameter('zend.logger.path', $config['path']); } - $configuration->setAlias('logger', 'zend.logger'); - return $configuration; } diff --git a/src/Symfony/Framework/ZendBundle/Tests/DependencyInjection/ZendExtensionTest.php b/src/Symfony/Framework/ZendBundle/Tests/DependencyInjection/ZendExtensionTest.php new file mode 100644 index 000000000000..eec042716268 --- /dev/null +++ b/src/Symfony/Framework/ZendBundle/Tests/DependencyInjection/ZendExtensionTest.php @@ -0,0 +1,31 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Framework\ZendBundle\Tests\DependencyInjection; + +use Symfony\Framework\ZendBundle\Tests\TestCase; +use Symfony\Framework\ZendBundle\DependencyInjection\ZendExtension; +use Symfony\Components\DependencyInjection\BuilderConfiguration; + +class ZendExtensionTest extends TestCase +{ + public function testLoggerLoad() + { + $configuration = new BuilderConfiguration(); + $loader = new ZendExtension(); + + $configuration = $loader->loggerLoad(array(), $configuration); + $this->assertEquals('Symfony\\Framework\\ZendBundle\\Logger\\Logger', $configuration->getParameter('zend.logger.class'), '->loggerLoad() loads the logger.xml file if not already loaded'); + + $configuration = $loader->loggerLoad(array('priority' => 3), $configuration); + $this->assertEquals(3, $configuration->getParameter('zend.logger.priority'), '->loggerLoad() overrides existing configuration options'); + } +}