diff --git a/src/M6Web/Bundle/LogBridgeBundle/DependencyInjection/CompilerPass/LoggerValidationPass.php b/src/M6Web/Bundle/LogBridgeBundle/DependencyInjection/CompilerPass/LoggerValidationPass.php new file mode 100644 index 0000000..26b4718 --- /dev/null +++ b/src/M6Web/Bundle/LogBridgeBundle/DependencyInjection/CompilerPass/LoggerValidationPass.php @@ -0,0 +1,35 @@ +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)); + } + } +} diff --git a/src/M6Web/Bundle/LogBridgeBundle/DependencyInjection/M6WebLogBridgeExtension.php b/src/M6Web/Bundle/LogBridgeBundle/DependencyInjection/M6WebLogBridgeExtension.php index c80bc1e..6777759 100644 --- a/src/M6Web/Bundle/LogBridgeBundle/DependencyInjection/M6WebLogBridgeExtension.php +++ b/src/M6Web/Bundle/LogBridgeBundle/DependencyInjection/M6WebLogBridgeExtension.php @@ -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 * diff --git a/src/M6Web/Bundle/LogBridgeBundle/M6WebLogBridgeBundle.php b/src/M6Web/Bundle/LogBridgeBundle/M6WebLogBridgeBundle.php index fe288f7..a463194 100644 --- a/src/M6Web/Bundle/LogBridgeBundle/M6WebLogBridgeBundle.php +++ b/src/M6Web/Bundle/LogBridgeBundle/M6WebLogBridgeBundle.php @@ -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; @@ -23,5 +25,6 @@ public function build(ContainerBuilder $container) parent::build($container); $container->addCompilerPass(new MatcherStatusTypeCompilerPass()); + $container->addCompilerPass(new LoggerValidationPass(), PassConfig::TYPE_OPTIMIZE); } }