|
0
|
1 |
<?php |
|
|
2 |
|
|
|
3 |
/* |
|
|
4 |
* This file is part of the Symfony package. |
|
|
5 |
* |
|
|
6 |
* (c) Fabien Potencier <fabien@symfony.com> |
|
|
7 |
* |
|
|
8 |
* For the full copyright and license information, please view the LICENSE |
|
|
9 |
* file that was distributed with this source code. |
|
|
10 |
*/ |
|
|
11 |
|
|
|
12 |
namespace Symfony\Bundle\MonologBundle\DependencyInjection\Compiler; |
|
|
13 |
|
|
|
14 |
use Symfony\Component\DependencyInjection\Reference; |
|
|
15 |
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
|
16 |
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
|
|
17 |
use Symfony\Component\DependencyInjection\DefinitionDecorator; |
|
|
18 |
|
|
|
19 |
/** |
|
|
20 |
* Replaces the default logger by another one with its own channel for tagged services. |
|
|
21 |
* |
|
|
22 |
* @author Christophe Coevoet <stof@notk.org> |
|
|
23 |
*/ |
|
|
24 |
class LoggerChannelPass implements CompilerPassInterface |
|
|
25 |
{ |
|
|
26 |
protected $channels = array(); |
|
|
27 |
|
|
|
28 |
public function process(ContainerBuilder $container) |
|
|
29 |
{ |
|
|
30 |
if (!$container->hasDefinition('monolog.logger')) { |
|
|
31 |
return; |
|
|
32 |
} |
|
|
33 |
|
|
|
34 |
foreach ($container->findTaggedServiceIds('monolog.logger') as $id => $tags) { |
|
|
35 |
foreach ($tags as $tag) { |
|
|
36 |
if (!empty($tag['channel']) && 'app' !== $tag['channel']) { |
|
|
37 |
$definition = $container->getDefinition($id); |
|
|
38 |
$loggerId = sprintf('monolog.logger.%s', $tag['channel']); |
|
|
39 |
$this->createLogger($tag['channel'], $loggerId, $container); |
|
|
40 |
foreach ($definition->getArguments() as $index => $argument) { |
|
|
41 |
if ($argument instanceof Reference && 'logger' === (string) $argument) { |
|
|
42 |
$definition->replaceArgument($index, new Reference($loggerId, $argument->getInvalidBehavior(), $argument->isStrict())); |
|
|
43 |
} |
|
|
44 |
} |
|
|
45 |
} |
|
|
46 |
} |
|
|
47 |
} |
|
|
48 |
} |
|
|
49 |
|
|
|
50 |
protected function createLogger($channel, $loggerId, ContainerBuilder $container) |
|
|
51 |
{ |
|
|
52 |
if (!in_array($channel, $this->channels)) { |
|
|
53 |
$logger = new DefinitionDecorator('monolog.logger_prototype'); |
|
|
54 |
$logger->replaceArgument(0, $channel); |
|
|
55 |
$container->setDefinition($loggerId, $logger); |
|
|
56 |
$this->channels[] = $channel; |
|
|
57 |
} |
|
|
58 |
} |
|
|
59 |
} |