From 874fb9540a89f5cbae3b503fc1aad540ffbcc8b0 Mon Sep 17 00:00:00 2001 From: Christophe Coevoet Date: Wed, 6 Jul 2011 15:50:39 +0200 Subject: [PATCH 1/2] [MonologBundle] Refactored the configuration of processors --- UPDATE.md | 11 +++- .../Compiler/AddProcessorsPass.php | 59 +++++++++++++++++++ .../DependencyInjection/Configuration.php | 17 ------ .../DependencyInjection/MonologExtension.php | 14 ----- .../Bundle/MonologBundle/MonologBundle.php | 2 + .../Resources/config/monolog.xml | 8 --- .../MonologExtensionTest.php | 15 ----- 7 files changed, 71 insertions(+), 55 deletions(-) create mode 100644 src/Symfony/Bundle/MonologBundle/DependencyInjection/Compiler/AddProcessorsPass.php diff --git a/UPDATE.md b/UPDATE.md index e9054c67d4..19e963f3c4 100644 --- a/UPDATE.md +++ b/UPDATE.md @@ -9,6 +9,15 @@ timeline closely anyway. RC4 to RC5 ---------- +* The configuration of MonologBundle has been refactored. + + * Only services are supported for the processors. They are now registered + using the `monolog.processor` tag which accept three optionnal attributes: + + * `handler`: the name of an handler to register it only for a specific handler + * `channel`: to register it only for one logging channel (exclusive with `handler`) + * `method`: The method used to process the record (`__invoke` is used if not set) + * To avoid security issues, HTTP headers coming from proxies are not trusted anymore by default (like `HTTP_X_FORWARDED_FOR`, `X_FORWARDED_PROTO`, and `X_FORWARDED_HOST`). If your application is behind a reverse proxy, add the @@ -20,7 +29,7 @@ RC4 to RC5 RC3 to RC4 ---------- -* Annotation classes must be annotated with @Annotation +* Annotation classes must be annotated with @Annotation (see the validator constraints for examples) * Annotations are not using the PHP autoloading but their own mechanism. This diff --git a/src/Symfony/Bundle/MonologBundle/DependencyInjection/Compiler/AddProcessorsPass.php b/src/Symfony/Bundle/MonologBundle/DependencyInjection/Compiler/AddProcessorsPass.php new file mode 100644 index 0000000000..83b2b13fec --- /dev/null +++ b/src/Symfony/Bundle/MonologBundle/DependencyInjection/Compiler/AddProcessorsPass.php @@ -0,0 +1,59 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bundle\MonologBundle\DependencyInjection\Compiler; + +use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; +use Symfony\Component\DependencyInjection\Reference; + +/** + * Registers processors in Monolg loggers or handlers. + * + * @author Christophe Coevoet + */ +class AddProcessorsPass implements CompilerPassInterface +{ + public function process(ContainerBuilder $container) + { + if (!$container->hasDefinition('monolog.logger')) { + return; + } + + foreach ($container->findTaggedServiceIds('monolog.processor') as $id => $tags) { + foreach ($tags as $tag) { + if (!empty($tag['channel']) && !empty($tag['handler'])) { + throw new \InvalidArgumentException(sprintf('you cannot specify both the "handler" and "channel" attributes for the "monolog.processor" tag on service "%s"', $id)); + } + + if (!empty($tag['handler'])) { + $definition = $container->getDefinition(sprintf('monolog.handler.%s', $tag['handler'])); + } elseif (!empty($tag['channel'])) { + if ('app' === $tag['channel']) { + $definition = $container->getDefinition('monolog.logger'); + } else { + $definition = $container->getDefinition(sprintf('monolog.logger.%s', $tag['channel'])); + } + } else { + $definition = $container->getDefinition('monolog.logger_prototype'); + } + + if (!empty($tag['method'])) { + $processor = array(new Reference($id), $tag['method']); + } else { + // If no method is defined, fallback to use __invoke + $processor = new Reference($id); + } + $definition->addMethodCall('pushProcessor', array($processor)); + } + } + } +} diff --git a/src/Symfony/Bundle/MonologBundle/DependencyInjection/Configuration.php b/src/Symfony/Bundle/MonologBundle/DependencyInjection/Configuration.php index d5357e98b3..619824c8ee 100644 --- a/src/Symfony/Bundle/MonologBundle/DependencyInjection/Configuration.php +++ b/src/Symfony/Bundle/MonologBundle/DependencyInjection/Configuration.php @@ -37,7 +37,6 @@ class Configuration implements ConfigurationInterface $rootNode ->fixXmlConfig('handler') - ->fixXmlConfig('processor') ->children() ->arrayNode('handlers') ->canBeUnset() @@ -77,7 +76,6 @@ class Configuration implements ConfigurationInterface ->scalarNode('email_prototype')->end() // swift_mailer ->scalarNode('formatter')->end() ->end() - ->append($this->getProcessorsNode()) ->validate() ->ifTrue(function($v) { return ('fingers_crossed' === $v['type'] || 'buffer' === $v['type']) && 1 !== count($v['handler']); }) ->thenInvalid('The handler has to be specified to use a FingersCrossedHandler or BufferHandler') @@ -101,23 +99,8 @@ class Configuration implements ConfigurationInterface ->end() ->end() ->end() - ->append($this->getProcessorsNode()) ; return $treeBuilder; } - - private function getProcessorsNode() - { - $treeBuilder = new TreeBuilder(); - $node = $treeBuilder->root('processors'); - - $node - ->canBeUnset() - ->performNoDeepMerging() - ->prototype('scalar')->end() - ; - - return $node; - } } diff --git a/src/Symfony/Bundle/MonologBundle/DependencyInjection/MonologExtension.php b/src/Symfony/Bundle/MonologBundle/DependencyInjection/MonologExtension.php index c53f217282..b4c042eb5b 100644 --- a/src/Symfony/Bundle/MonologBundle/DependencyInjection/MonologExtension.php +++ b/src/Symfony/Bundle/MonologBundle/DependencyInjection/MonologExtension.php @@ -49,10 +49,6 @@ class MonologExtension extends Extension $logger = $container->getDefinition('monolog.logger_prototype'); - if (!empty($config['processors'])) { - $this->addProcessors($container, $logger, $config['processors']); - } - $handlers = array(); foreach ($config['handlers'] as $name => $handler) { $handlers[] = array('id' => $this->buildHandler($container, $name, $handler), 'priority' => $handler['priority'] ); @@ -238,9 +234,6 @@ class MonologExtension extends Extension if (!empty($handler['formatter'])) { $definition->addMethodCall('setFormatter', array(new Reference($handler['formatter']))); } - if (!empty($handler['processors'])) { - $this->addProcessors($container, $definition, $handler['processors']); - } $container->setDefinition($handlerId, $definition); return $handlerId; @@ -251,13 +244,6 @@ class MonologExtension extends Extension return sprintf('monolog.handler.%s', $name); } - private function addProcessors(ContainerBuilder $container, Definition $definition, array $processors) - { - foreach (array_reverse($processors) as $processor) { - $definition->addMethodCall('pushProcessor', array($this->parseDefinition($processor, $container))); - } - } - private function parseDefinition($definition, ContainerBuilder $container = null) { if (0 === strpos($definition, '@')) { diff --git a/src/Symfony/Bundle/MonologBundle/MonologBundle.php b/src/Symfony/Bundle/MonologBundle/MonologBundle.php index e51ddf1e32..013f3cfa44 100644 --- a/src/Symfony/Bundle/MonologBundle/MonologBundle.php +++ b/src/Symfony/Bundle/MonologBundle/MonologBundle.php @@ -15,6 +15,7 @@ use Symfony\Component\HttpKernel\Bundle\Bundle; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Bundle\MonologBundle\DependencyInjection\Compiler\LoggerChannelPass; use Symfony\Bundle\MonologBundle\DependencyInjection\Compiler\DebugHandlerPass; +use Symfony\Bundle\MonologBundle\DependencyInjection\Compiler\AddProcessorsPass; /** * Bundle. @@ -29,5 +30,6 @@ class MonologBundle extends Bundle $container->addCompilerPass(new LoggerChannelPass()); $container->addCompilerPass(new DebugHandlerPass()); + $container->addCompilerPass(new AddProcessorsPass()); } } diff --git a/src/Symfony/Bundle/MonologBundle/Resources/config/monolog.xml b/src/Symfony/Bundle/MonologBundle/Resources/config/monolog.xml index 569db8f2e0..8b42f4d965 100644 --- a/src/Symfony/Bundle/MonologBundle/Resources/config/monolog.xml +++ b/src/Symfony/Bundle/MonologBundle/Resources/config/monolog.xml @@ -18,8 +18,6 @@ Symfony\Bridge\Monolog\Handler\DebugHandler Monolog\Handler\SwiftMailerHandler Monolog\Handler\NativeMailerHandler - Symfony\Bridge\Monolog\Processor\WebProcessor - Monolog\Processor\IntrospectionProcessor @@ -32,11 +30,5 @@ - - - - - - diff --git a/src/Symfony/Bundle/MonologBundle/Tests/DependencyInjection/MonologExtensionTest.php b/src/Symfony/Bundle/MonologBundle/Tests/DependencyInjection/MonologExtensionTest.php index 06effcd499..903217d708 100644 --- a/src/Symfony/Bundle/MonologBundle/Tests/DependencyInjection/MonologExtensionTest.php +++ b/src/Symfony/Bundle/MonologBundle/Tests/DependencyInjection/MonologExtensionTest.php @@ -35,21 +35,6 @@ class MonologExtensionTest extends TestCase $this->assertDICConstructorArguments($handler, array('%kernel.logs_dir%/%kernel.environment%.log', \Monolog\Logger::DEBUG, true)); } - public function testLoadWithProcessor() - { - $container = new ContainerBuilder(); - $loader = new MonologExtension(); - - $loader->load(array(array('handlers' => array('main' => array('type' => 'stream', 'processors' => array('@monolog.processor.web'))))), $container); - $this->assertTrue($container->hasDefinition('monolog.handler.main')); - - $handler = $container->getDefinition('monolog.handler.main'); - $this->assertDICDefinitionMethodCallAt(0, $handler, 'pushProcessor', array(new Reference('monolog.processor.web'))); - - $this->assertTrue($container->getDefinition('monolog.processor.web')->isPublic()); - $this->assertFalse($container->getDefinition('monolog.processor.introspection')->isPublic()); - } - public function testLoadWithCustomValues() { $container = new ContainerBuilder(); From f8b5f350dc4a502aa4b51497d0a8c889103387f4 Mon Sep 17 00:00:00 2001 From: Christophe Coevoet Date: Wed, 6 Jul 2011 16:25:32 +0200 Subject: [PATCH 2/2] [MonologBundle] Refactored the way to configure the email prototype for swiftmailer --- UPDATE.md | 16 +++++++++++++++ .../DependencyInjection/Configuration.php | 12 ++++++++++- .../DependencyInjection/MonologExtension.php | 20 +++++-------------- .../Resources/config/schema/monolog-1.0.xsd | 11 ++++++---- 4 files changed, 39 insertions(+), 20 deletions(-) diff --git a/UPDATE.md b/UPDATE.md index 19e963f3c4..95d976d82e 100644 --- a/UPDATE.md +++ b/UPDATE.md @@ -18,6 +18,22 @@ RC4 to RC5 * `channel`: to register it only for one logging channel (exclusive with `handler`) * `method`: The method used to process the record (`__invoke` is used if not set) + * The email_prototype for the `SwiftMailerHandler` only accept a service id now. + + * Before: + + email_prototype: @acme_demo.monolog.email_prototype + + * After: + + email_prototype: acme_demo.monolog.email_prototype + + or if you want to use a factory for the prototype: + + email_prototype: + id: acme_demo.monolog.email_prototype + method: getPrototype + * To avoid security issues, HTTP headers coming from proxies are not trusted anymore by default (like `HTTP_X_FORWARDED_FOR`, `X_FORWARDED_PROTO`, and `X_FORWARDED_HOST`). If your application is behind a reverse proxy, add the diff --git a/src/Symfony/Bundle/MonologBundle/DependencyInjection/Configuration.php b/src/Symfony/Bundle/MonologBundle/DependencyInjection/Configuration.php index 619824c8ee..633fac9203 100644 --- a/src/Symfony/Bundle/MonologBundle/DependencyInjection/Configuration.php +++ b/src/Symfony/Bundle/MonologBundle/DependencyInjection/Configuration.php @@ -73,7 +73,17 @@ class Configuration implements ConfigurationInterface ->scalarNode('from_email')->end() // swift_mailer and native_mailer ->scalarNode('to_email')->end() // swift_mailer and native_mailer ->scalarNode('subject')->end() // swift_mailer and native_mailer - ->scalarNode('email_prototype')->end() // swift_mailer + ->arrayNode('email_prototype') // swift_mailer + ->canBeUnset() + ->beforeNormalization() + ->ifString() + ->then(function($v) { return array('id' => $v); }) + ->end() + ->children() + ->scalarNode('id')->isRequired()->end() + ->scalarNode('factory-method')->defaultNull()->end() + ->end() + ->end() ->scalarNode('formatter')->end() ->end() ->validate() diff --git a/src/Symfony/Bundle/MonologBundle/DependencyInjection/MonologExtension.php b/src/Symfony/Bundle/MonologBundle/DependencyInjection/MonologExtension.php index b4c042eb5b..d38e18f134 100644 --- a/src/Symfony/Bundle/MonologBundle/DependencyInjection/MonologExtension.php +++ b/src/Symfony/Bundle/MonologBundle/DependencyInjection/MonologExtension.php @@ -186,7 +186,11 @@ class MonologExtension extends Extension case 'swift_mailer': if (isset($handler['email_prototype'])) { - $prototype = $this->parseDefinition($handler['email_prototype']); + if (!empty($handler['email_prototype']['method'])) { + $prototype = array(new Reference($handler['email_prototype']['id']), $handler['email_prototype']['method']); + } else { + $prototype = new Reference($handler['email_prototype']['id']); + } } else { $message = new Definition('Swift_Message'); $message->setFactoryService('mailer'); @@ -243,18 +247,4 @@ class MonologExtension extends Extension { return sprintf('monolog.handler.%s', $name); } - - private function parseDefinition($definition, ContainerBuilder $container = null) - { - if (0 === strpos($definition, '@')) { - $definition = substr($definition, 1); - if ($container && $container->hasDefinition($definition)) { - $container->getDefinition($definition)->setPublic(true); - } - - return new Reference($definition); - } - - return $definition; - } } diff --git a/src/Symfony/Bundle/MonologBundle/Resources/config/schema/monolog-1.0.xsd b/src/Symfony/Bundle/MonologBundle/Resources/config/schema/monolog-1.0.xsd index 21705a0539..2cb695d707 100644 --- a/src/Symfony/Bundle/MonologBundle/Resources/config/schema/monolog-1.0.xsd +++ b/src/Symfony/Bundle/MonologBundle/Resources/config/schema/monolog-1.0.xsd @@ -10,16 +10,15 @@ - - + - + @@ -35,7 +34,6 @@ - @@ -56,4 +54,9 @@ + + + + +