‪TYPO3CMS  ‪main
LoggerInterfacePass.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 Psr\Log\LoggerInterface;
21 use Symfony\Component\DependencyInjection\Compiler\AbstractRecursivePass;
22 use Symfony\Component\DependencyInjection\Definition;
23 use Symfony\Component\DependencyInjection\Reference;
27 
31 class ‪LoggerInterfacePass extends AbstractRecursivePass
32 {
38  protected function ‪processValue($value, $isRoot = false)
39  {
40  $value = parent::processValue($value, $isRoot);
41 
42  if (!$value instanceof Definition || !$value->isAutowired() || $value->isAbstract() || !$value->getClass()) {
43  return $value;
44  }
45  if (!$reflectionClass = $this->container->getReflectionClass($value->getClass(), false)) {
46  return $value;
47  }
48 
49  $constructor = $reflectionClass->getConstructor();
50  if ($constructor === null) {
51  return $value;
52  }
53 
54  $arguments = $value->getArguments();
55  foreach ($reflectionClass->getConstructor()->getParameters() as $index => $parameter) {
56  $name = '$' . $parameter->getName();
57 
58  if (isset($arguments[$name]) || isset($arguments[$index])) {
59  continue;
60  }
61 
62  if (!$parameter->hasType()) {
63  continue;
64  }
65 
66  $type = $parameter->getType();
67  if (!($type instanceof \ReflectionNamedType && $type->getName() === LoggerInterface::class)) {
68  continue;
69  }
70 
71  $channel = $this->‪getParameterChannelName($parameter) ?? $this->‪getClassChannelName($reflectionClass) ?? $value->getClass();
72 
73  $logger = new Definition(Logger::class);
74  $logger->setFactory([new Reference(LogManager::class), 'getLogger']);
75  $logger->setArguments([$channel]);
76  $logger->setShared(false);
77 
78  $value->setArgument($name, $logger);
79  }
80 
81  return $value;
82  }
83 
84  protected function ‪getParameterChannelName(\ReflectionParameter $parameter): ?string
85  {
86  // Attribute channel definition is only supported on PHP 8 and later.
87  if (class_exists('\ReflectionAttribute', false)) {
88  $attributes = $parameter->getAttributes(Channel::class, \ReflectionAttribute::IS_INSTANCEOF);
89  foreach ($attributes as $channel) {
90  return $channel->newInstance()->name;
91  }
92  }
93 
94  return null;
95  }
96 
97  protected function ‪getClassChannelName(\ReflectionClass $class): ?string
98  {
99  // Attribute channel definition is only supported on PHP 8 and later.
100  if (class_exists('\ReflectionAttribute', false)) {
101  $attributes = $class->getAttributes(Channel::class, \ReflectionAttribute::IS_INSTANCEOF);
102  foreach ($attributes as $channel) {
103  return $channel->newInstance()->name;
104  }
105  }
106 
107  if ($class->getParentClass() !== false) {
108  return $this->‪getClassChannelName($class->getParentClass());
109  }
110 
111  return null;
112  }
113 }
‪TYPO3\CMS\Core\DependencyInjection\LoggerInterfacePass\getParameterChannelName
‪getParameterChannelName(\ReflectionParameter $parameter)
Definition: LoggerInterfacePass.php:84
‪TYPO3\CMS\Core\DependencyInjection\LoggerInterfacePass
Definition: LoggerInterfacePass.php:32
‪TYPO3\CMS\Core\DependencyInjection
Definition: AutowireInjectMethodsPass.php:18
‪TYPO3\CMS\Core\DependencyInjection\LoggerInterfacePass\getClassChannelName
‪getClassChannelName(\ReflectionClass $class)
Definition: LoggerInterfacePass.php:97
‪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
‪TYPO3\CMS\Core\DependencyInjection\LoggerInterfacePass\processValue
‪mixed processValue($value, $isRoot=false)
Definition: LoggerInterfacePass.php:38