|
3 | 3 | * Copyright © Magento, Inc. All rights reserved.
|
4 | 4 | * See COPYING.txt for license details.
|
5 | 5 | */
|
| 6 | + |
6 | 7 | namespace Magento\Framework\Code\Generator;
|
7 | 8 |
|
| 9 | +use Magento\Framework\App\ObjectManager; |
8 | 10 | use Magento\Framework\Code\Generator;
|
| 11 | +use Psr\Log\LoggerInterface; |
9 | 12 |
|
10 | 13 | class Autoloader
|
11 | 14 | {
|
12 | 15 | /**
|
13 |
| - * @var \Magento\Framework\Code\Generator |
| 16 | + * @var Generator |
14 | 17 | */
|
15 | 18 | protected $_generator;
|
16 | 19 |
|
17 | 20 | /**
|
18 |
| - * @param \Magento\Framework\Code\Generator $generator |
| 21 | + * Enables guarding against spamming the debug log with duplicate messages, as |
| 22 | + * the generation exception will be thrown multiple times within a single request. |
| 23 | + * |
| 24 | + * @var string |
| 25 | + */ |
| 26 | + private $lastGenerationErrorMessage; |
| 27 | + |
| 28 | + /** |
| 29 | + * @param Generator $generator |
19 | 30 | */
|
20 |
| - public function __construct( |
21 |
| - \Magento\Framework\Code\Generator $generator |
22 |
| - ) { |
| 31 | + public function __construct(Generator $generator) |
| 32 | + { |
23 | 33 | $this->_generator = $generator;
|
24 | 34 | }
|
25 | 35 |
|
26 | 36 | /**
|
27 | 37 | * Load specified class name and generate it if necessary
|
28 | 38 | *
|
| 39 | + * According to PSR-4 section 2.4 an autoloader MUST NOT throw an exception and SHOULD NOT return a value. |
| 40 | + * |
| 41 | + * @see https://www.php-fig.org/psr/psr-4/ |
| 42 | + * |
29 | 43 | * @param string $className
|
30 |
| - * @return bool True if class was loaded |
| 44 | + * @return void |
31 | 45 | */
|
32 | 46 | public function load($className)
|
33 | 47 | {
|
34 |
| - if (!class_exists($className)) { |
35 |
| - return Generator::GENERATION_ERROR != $this->_generator->generateClass($className); |
| 48 | + if (! class_exists($className)) { |
| 49 | + try { |
| 50 | + $this->_generator->generateClass($className); |
| 51 | + } catch (\Exception $exception) { |
| 52 | + $this->tryToLogExceptionMessageIfNotDuplicate($exception); |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * @param \Exception $exception |
| 59 | + */ |
| 60 | + private function tryToLogExceptionMessageIfNotDuplicate(\Exception $exception): void |
| 61 | + { |
| 62 | + if ($this->lastGenerationErrorMessage !== $exception->getMessage()) { |
| 63 | + $this->lastGenerationErrorMessage = $exception->getMessage(); |
| 64 | + $this->tryToLogException($exception); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Try to capture the exception message. |
| 70 | + * |
| 71 | + * The Autoloader is instantiated before the ObjectManager, so the LoggerInterface can not be injected. |
| 72 | + * The Logger is instantiated in the try/catch block because ObjectManager might still not be initialized. |
| 73 | + * In that case the exception message can not be captured. |
| 74 | + * |
| 75 | + * The debug level is used for logging in case class generation fails for a common class, but a custom |
| 76 | + * autoloader is used later in the stack. A more severe log level would fill the logs with messages on production. |
| 77 | + * The exception message now can be accessed in developer mode if debug logging is enabled. |
| 78 | + * |
| 79 | + * @param \Exception $exception |
| 80 | + * @return void |
| 81 | + */ |
| 82 | + private function tryToLogException(\Exception $exception): void |
| 83 | + { |
| 84 | + try { |
| 85 | + $logger = ObjectManager::getInstance()->get(LoggerInterface::class); |
| 86 | + $logger->debug($exception->getMessage(), ['exception' => $exception]); |
| 87 | + } catch (\Exception $ignoreThisException) { |
| 88 | + // Do not take an action here, since the original exception might have been caused by logger |
36 | 89 | }
|
37 |
| - return true; |
38 | 90 | }
|
39 | 91 | }
|
0 commit comments