‪TYPO3CMS  ‪main
LoggerAwarePass.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
21 use Symfony\Component\DependencyInjection\ContainerBuilder;
22 use Symfony\Component\DependencyInjection\Definition;
23 use Symfony\Component\DependencyInjection\Reference;
27 
31 final class ‪LoggerAwarePass implements CompilerPassInterface
32 {
36  private ‪$tagName;
37 
38  public function ‪__construct(string ‪$tagName)
39  {
40  $this->tagName = ‪$tagName;
41  }
42 
46  public function ‪process(ContainerBuilder $container)
47  {
48  foreach ($container->findTaggedServiceIds($this->tagName) as $id => $tags) {
49  $definition = $container->findDefinition($id);
50  if (!$definition->isAutowired() || $definition->isAbstract()) {
51  continue;
52  }
53 
54  $channel = $id;
55  if ($definition->getClass()) {
56  $reflectionClass = $container->getReflectionClass($definition->getClass(), false);
57  if ($reflectionClass) {
58  $channel = $this->‪getClassChannelName($reflectionClass) ?? $channel;
59  }
60  }
61 
62  $logger = new Definition(Logger::class);
63  $logger->setFactory([new Reference(LogManager::class), 'getLogger']);
64  $logger->setArguments([$channel]);
65  $logger->setShared(false);
66 
67  $definition->addMethodCall('setLogger', [$logger]);
68  }
69  }
70 
71  protected function ‪getClassChannelName(\ReflectionClass $class): ?string
72  {
73  // Attribute channel definition is only supported on PHP 8 and later.
74  if (class_exists('\ReflectionAttribute', false)) {
75  $attributes = $class->getAttributes(Channel::class, \ReflectionAttribute::IS_INSTANCEOF);
76  foreach ($attributes as $channel) {
77  return $channel->newInstance()->name;
78  }
79  }
80 
81  if ($class->getParentClass() !== false) {
82  return $this->‪getClassChannelName($class->getParentClass());
83  }
84 
85  return null;
86  }
87 }
‪TYPO3\CMS\Core\DependencyInjection\LoggerAwarePass\process
‪process(ContainerBuilder $container)
Definition: LoggerAwarePass.php:45
‪TYPO3\CMS\Core\DependencyInjection
Definition: AutowireInjectMethodsPass.php:18
‪TYPO3\CMS\Core\DependencyInjection\LoggerAwarePass\$tagName
‪string $tagName
Definition: LoggerAwarePass.php:35
‪TYPO3\CMS\Core\DependencyInjection\LoggerAwarePass\__construct
‪__construct(string $tagName)
Definition: LoggerAwarePass.php:37
‪TYPO3\CMS\Core\DependencyInjection\LoggerAwarePass
Definition: LoggerAwarePass.php:32
‪TYPO3\CMS\Core\DependencyInjection\LoggerAwarePass\getClassChannelName
‪getClassChannelName(\ReflectionClass $class)
Definition: LoggerAwarePass.php:70
‪TYPO3\CMS\Core\Log\LogManager
Definition: LogManager.php:33
‪TYPO3\CMS\Core\Log\Channel
Definition: Channel.php:25
‪TYPO3\CMS\Core\Log\Logger
Definition: Logger.php:28