Skip to content

Commit

Permalink
Merge pull request #45 from M6Web/fix/validateLoggerService
Browse files Browse the repository at this point in the history
Fix logger service validation
  • Loading branch information
lolautruche authored Nov 15, 2016
2 parents 17846d4 + 6b86ef9 commit 025e4fc
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace M6Web\Bundle\LogBridgeBundle\DependencyInjection\CompilerPass;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

/**
* Validates logger service used by the log request listener.
*/
class LoggerValidationPass implements CompilerPassInterface
{
/**
* {@inheritdoc}
*/
public function process(ContainerBuilder $container)
{
if (!$container->hasParameter('m6web_log_bridge.logger.service')) {
return;
}

$loggerServiceId = $container->getParameter('m6web_log_bridge.logger.service');
$loggerClass = $container->findDefinition($loggerServiceId)->getClass();

// If $loggerClass is not a valid namespace but a container parameter
if (preg_match("/^%[\w\.]+%$/", $loggerClass)) {
$loggerClassParameter = substr($loggerClass, 1, -1);
$loggerClass = $container->getParameter($loggerClassParameter);
}

if (!in_array('Psr\Log\LoggerInterface', class_implements($loggerClass))) {
throw new \InvalidArgumentException(sprintf('Class "%s" must be implement "Psr\Log\LoggerInterface"', $loggerClass));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,37 +50,9 @@ public function load(array $configs, ContainerBuilder $container)
);
}

$this->validateLogger($container, $config['logger']);
$this->loadRequestListener($container);
}

/**
* validateLogger
*
* @param ContainerBuilder $container Container
* @param array $loggerConfig Logger configuration
*
* @throws \Exception Class logger must be implement "Psr\Log\LoggerInterface"
*
* @return bool
*/
protected function validateLogger(ContainerBuilder $container, $loggerConfig)
{
$loggerClass = $container->getDefinition($loggerConfig['service'])->getClass();

// If $loggerClass is not a valid namespace but a container parameter
if (preg_match("/^%[\w\.]+%$/", $loggerClass)) {
$loggerClassParameter = substr($loggerClass, 1, -1);
$loggerClass = $container->getParameter($loggerClassParameter);
}

if (!in_array('Psr\Log\LoggerInterface', class_implements($loggerClass))) {
throw new \Exception(sprintf('Class "%s" must be implement "Psr\Log\LoggerInterface"', $loggerClass));
}

return true;
}

/**
* loadRequestListener
*
Expand Down
3 changes: 3 additions & 0 deletions src/M6Web/Bundle/LogBridgeBundle/M6WebLogBridgeBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace M6Web\Bundle\LogBridgeBundle;

use M6Web\Bundle\LogBridgeBundle\DependencyInjection\CompilerPass\LoggerValidationPass;
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use M6Web\Bundle\LogBridgeBundle\DependencyInjection\CompilerPass\MatcherStatusTypeCompilerPass;
Expand All @@ -23,5 +25,6 @@ public function build(ContainerBuilder $container)
parent::build($container);

$container->addCompilerPass(new MatcherStatusTypeCompilerPass());
$container->addCompilerPass(new LoggerValidationPass(), PassConfig::TYPE_OPTIMIZE);
}
}

0 comments on commit 025e4fc

Please sign in to comment.