[ZendBundle] Added a Configuration class

This commit is contained in:
Christophe Coevoet 2011-02-24 17:38:40 +01:00
parent 788f63d460
commit 192583a225
2 changed files with 62 additions and 38 deletions

View File

@ -0,0 +1,48 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\ZendBundle\DependencyInjection;
use Symfony\Component\Config\Definition\Builder\NodeBuilder;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
/**
* 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
{
/**
* Generates the configuration tree.
*
* @return \Symfony\Component\Config\Definition\NodeInterface
*/
public function getConfigTree()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('zend', 'array');
$rootNode
->arrayNode('logger')
->canBeUnset()
->scalarNode('priority')->defaultValue('CRIT')->end()
->scalarNode('path')->defaultValue('%kernel.logs_dir%/%kernel.environment%.log')->end()
->booleanNode('log_errors')->defaultFalse()->end()
->end()
;
return $treeBuilder->buildTree();
}
}

View File

@ -15,6 +15,7 @@ use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator; use Symfony\Component\Config\FileLocator;
use Symfony\Component\Config\Definition\Processor;
/** /**
* ZendExtension is an extension for the Zend Framework libraries. * ZendExtension is an extension for the Zend Framework libraries.
@ -37,52 +38,27 @@ class ZendExtension extends Extension
*/ */
public function load(array $configs, ContainerBuilder $container) public function load(array $configs, ContainerBuilder $container)
{ {
$first = true; $configuration = new Configuration();
foreach ($configs as $config) { $processor = new Processor();
if (!isset($config['logger'])) { $config = $processor->process($configuration->getConfigTree(), $configs);
continue;
}
if ($first) { if (isset($config['logger'])) {
$first = false; $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('logger.xml');
$container->setAlias('logger', 'zend.logger');
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); $config = $config['logger'];
$loader->load('logger.xml');
$container->setAlias('logger', 'zend.logger');
}
$this->registerLoggerConfiguration($config, $container);
}
}
/**
* Loads the logger configuration.
*
* Usage example:
*
* <zend:logger priority="info" path="/path/to/some.log" />
*
* @param array $config An array of configuration settings
* @param ContainerBuilder $container A ContainerBuilder instance
*/
protected function registerLoggerConfiguration($config, ContainerBuilder $container)
{
$config = $config['logger'];
if (isset($config['priority'])) {
$container->setParameter('zend.logger.priority', is_int($config['priority']) ? $config['priority'] : constant('\\Zend\\Log\\Logger::'.strtoupper($config['priority']))); $container->setParameter('zend.logger.priority', is_int($config['priority']) ? $config['priority'] : constant('\\Zend\\Log\\Logger::'.strtoupper($config['priority'])));
}
if (isset($config['path'])) {
$container->setParameter('zend.logger.path', $config['path']); $container->setParameter('zend.logger.path', $config['path']);
}
if (isset($config['log_errors'])) {
$definition = $container->findDefinition('zend.logger'); $definition = $container->findDefinition('zend.logger');
if (false === $config['log_errors'] && $definition->hasMethodCall('registerErrorHandler')) { if ($config['log_errors']) {
$container->findDefinition('zend.logger')->removeMethodCall('registerErrorHandler');
} else {
$container->findDefinition('zend.logger')->addMethodCall('registerErrorHandler'); $container->findDefinition('zend.logger')->addMethodCall('registerErrorHandler');
} else {
if ($definition->hasMethodCall('registerErrorHandler')) {
$container->findDefinition('zend.logger')->removeMethodCall('registerErrorHandler');
}
} }
} }
} }