LoggerPass.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\HttpKernel\DependencyInjection;
  11. use Psr\Log\LoggerInterface;
  12. use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
  13. use Symfony\Component\DependencyInjection\ContainerBuilder;
  14. use Symfony\Component\DependencyInjection\Reference;
  15. use Symfony\Component\HttpFoundation\RequestStack;
  16. use Symfony\Component\HttpKernel\Log\Logger;
  17. /**
  18. * Registers the default logger if necessary.
  19. *
  20. * @author Kévin Dunglas <dunglas@gmail.com>
  21. */
  22. class LoggerPass implements CompilerPassInterface
  23. {
  24. /**
  25. * @return void
  26. */
  27. public function process(ContainerBuilder $container)
  28. {
  29. if (!$container->has(LoggerInterface::class)) {
  30. $container->setAlias(LoggerInterface::class, 'logger');
  31. }
  32. if ($container->has('logger')) {
  33. return;
  34. }
  35. if ($debug = $container->getParameter('kernel.debug')) {
  36. $debug = $container->hasParameter('kernel.runtime_mode.web')
  37. ? $container->getParameter('kernel.runtime_mode.web')
  38. : !\in_array(\PHP_SAPI, ['cli', 'phpdbg', 'embed'], true);
  39. }
  40. $container->register('logger', Logger::class)
  41. ->setArguments([null, null, null, new Reference(RequestStack::class), $debug]);
  42. }
  43. }