LoggerPass.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. public function process(ContainerBuilder $container): void
  25. {
  26. $container->setAlias(LoggerInterface::class, 'logger');
  27. if ($container->has('logger')) {
  28. return;
  29. }
  30. if ($debug = $container->getParameter('kernel.debug')) {
  31. $debug = $container->hasParameter('kernel.runtime_mode.web')
  32. ? $container->getParameter('kernel.runtime_mode.web')
  33. : !\in_array(\PHP_SAPI, ['cli', 'phpdbg', 'embed'], true);
  34. }
  35. $container->register('logger', Logger::class)
  36. ->setArguments([null, null, null, new Reference(RequestStack::class), $debug]);
  37. }
  38. }