![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/old/vendor/magento/framework/Code/Generator/ |
<?php /** * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ namespace Magento\Framework\Code\Generator; use Magento\Framework\App\ObjectManager; use Magento\Framework\Code\Generator; use Psr\Log\LoggerInterface; /** * Class loader and generator. */ class Autoloader { /** * @var Generator */ protected $_generator; /** * Enables guarding against spamming the debug log with duplicate messages, as * the generation exception will be thrown multiple times within a single request. * * @var string */ private $lastGenerationErrorMessage; /** * @param Generator $generator */ public function __construct(Generator $generator) { $this->_generator = $generator; } /** * Load specified class name and generate it if necessary * * According to PSR-4 section 2.4 an autoloader MUST NOT throw an exception and SHOULD NOT return a value. * * @see https://www.php-fig.org/psr/psr-4/ * * @param string $className * @return void */ public function load($className) { if (! class_exists($className)) { try { $this->_generator->generateClass($className); } catch (\Exception $exception) { $this->tryToLogExceptionMessageIfNotDuplicate($exception); } } } /** * Log exception. * * @param \Exception $exception */ private function tryToLogExceptionMessageIfNotDuplicate(\Exception $exception): void { if ($this->lastGenerationErrorMessage !== $exception->getMessage()) { $this->lastGenerationErrorMessage = $exception->getMessage(); $this->tryToLogException($exception); } } /** * Try to capture the exception message. * * The Autoloader is instantiated before the ObjectManager, so the LoggerInterface can not be injected. * The Logger is instantiated in the try/catch block because ObjectManager might still not be initialized. * In that case the exception message can not be captured. * * The debug level is used for logging in case class generation fails for a common class, but a custom * autoloader is used later in the stack. A more severe log level would fill the logs with messages on production. * The exception message now can be accessed in developer mode if debug logging is enabled. * * @param \Exception $exception * @return void */ private function tryToLogException(\Exception $exception): void { try { $logger = ObjectManager::getInstance()->get(LoggerInterface::class); $logger->debug($exception->getMessage(), ['exception' => $exception]); } catch (\Exception $ignoreThisException) { // Do not take an action here, since the original exception might have been caused by logger } } }