Skip to content

Commit

Permalink
[MonologBundle] Added services for core processors
Browse files Browse the repository at this point in the history
  • Loading branch information
Seldaek committed Jul 4, 2011
1 parent 5445b0d commit e6da824
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 5 deletions.
28 changes: 28 additions & 0 deletions src/Symfony/Bridge/Monolog/Processor/WebProcessor.php
@@ -0,0 +1,28 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bridge\Monolog\Processor;

use Monolog\Processor\WebProcessor as BaseWebProcessor;
use Symfony\Component\HttpFoundation\Request;

/**
* WebProcessor override to read from the HttpFoundation's Request
*
* @author Jordi Boggiano <j.boggiano@seld.be>
*/
class WebProcessor extends BaseWebProcessor
{
public function __construct(RequestInterface $request)
{
parent::__construct($request->server->all());
}
}
Expand Up @@ -239,7 +239,7 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler
$definition->addMethodCall('setFormatter', array(new Reference($handler['formatter'])));
}
if (!empty($handler['processors'])) {
$this->addProcessors($definition, $handler['processors']);
$this->addProcessors($container, $definition, $handler['processors']);
}
$container->setDefinition($handlerId, $definition);

Expand All @@ -251,17 +251,21 @@ private function getHandlerId($name)
return sprintf('monolog.handler.%s', $name);
}

private function addProcessors(Definition $definition, array $processors)
private function addProcessors(ContainerBuilder $container, Definition $definition, array $processors)
{
foreach (array_reverse($processors) as $processor) {
$definition->addMethodCall('pushProcessor', array($this->parseDefinition($processor)));
$definition->addMethodCall('pushProcessor', array($this->parseDefinition($processor, $container)));
}
}

private function parseDefinition($definition)
private function parseDefinition($definition, ContainerBuilder $container = null)
{
if (0 === strpos($definition, '@')) {
return new Reference(substr($definition, 1));
$definition = substr($definition, 1);
if ($container && $service = $container->getDefinition($definition)) {
$service->setPublic(true);
}
return new Reference($definition);
}

return $definition;
Expand Down
10 changes: 10 additions & 0 deletions src/Symfony/Bundle/MonologBundle/Resources/config/monolog.xml
Expand Up @@ -18,15 +18,25 @@
<parameter key="monolog.handler.debug.class">Symfony\Bridge\Monolog\Handler\DebugHandler</parameter>
<parameter key="monolog.handler.swift_mailer.class">Monolog\Handler\SwiftMailerHandler</parameter>
<parameter key="monolog.handler.native_mailer.class">Monolog\Handler\NativeMailerHandler</parameter>
<parameter key="monolog.processor.web.class">Symfony\Bridge\Monolog\Processor\WebProcessor</parameter>
<parameter key="monolog.processor.introspection.class">Monolog\Processor\IntrospectionProcessor</parameter>
</parameters>

<services>
<service id="monolog.logger" parent="monolog.logger_prototype" public="false">
<argument index="0">app</argument>
</service>

<service id="logger" alias="monolog.logger" />

<service id="monolog.logger_prototype" class="%monolog.logger.class%" abstract="true">
<argument /><!-- Channel -->
</service>

<service id="monolog.processor.web" class="%monolog.processor.web.class%" scope="request" public="false">
<argument type="service" id="request" />
</service>

<service id="monolog.processor.introspection" class="%monolog.processor.introspection.class%" public="false" />
</services>
</container>
Expand Up @@ -35,6 +35,21 @@ public function testLoadWithDefault()
$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();
Expand Down

0 comments on commit e6da824

Please sign in to comment.