[BUGFIX] Migrate cObject to LoggerAwareTrait 20/56920/2
authorBenni Mack <benni@typo3.org>
Fri, 11 May 2018 07:03:27 +0000 (09:03 +0200)
committerFrank Naegler <frank.naegler@typo3.org>
Fri, 11 May 2018 07:49:14 +0000 (09:49 +0200)
TYPO3 Core supports PSR-3 through the LoggerAwareTrait and LoggerAwareInterface
since TYPO3 v9 in all available PHP classes via makeInstance() automatically.

However, during the migration, ContentObjectRenderer was not replaced.

Resolves: #84967
Releases: master
Change-Id: I5e3481273f60858fe9bf1d8fed30d27ad1cd5707
Reviewed-on: https://review.typo3.org/56920
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Mathias Schreiber <mathias.schreiber@typo3.com>
Tested-by: Mathias Schreiber <mathias.schreiber@typo3.com>
Reviewed-by: Frank Naegler <frank.naegler@typo3.org>
Tested-by: Frank Naegler <frank.naegler@typo3.org>
typo3/sysext/frontend/Classes/ContentObject/ContentObjectRenderer.php
typo3/sysext/frontend/Tests/Unit/ContentObject/ContentObjectRendererTest.php

index 3253296..766ffdf 100644 (file)
@@ -16,6 +16,8 @@ namespace TYPO3\CMS\Frontend\ContentObject;
 
 use Doctrine\DBAL\DBALException;
 use Doctrine\DBAL\Driver\Statement;
+use Psr\Log\LoggerAwareInterface;
+use Psr\Log\LoggerAwareTrait;
 use TYPO3\CMS\Core\Cache\CacheManager;
 use TYPO3\CMS\Core\Database\Connection;
 use TYPO3\CMS\Core\Database\ConnectionPool;
@@ -28,7 +30,6 @@ use TYPO3\CMS\Core\Html\HtmlParser;
 use TYPO3\CMS\Core\Imaging\ImageManipulation\Area;
 use TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariantCollection;
 use TYPO3\CMS\Core\LinkHandling\LinkService;
-use TYPO3\CMS\Core\Log\LogManager;
 use TYPO3\CMS\Core\Mail\MailMessage;
 use TYPO3\CMS\Core\Resource\Exception;
 use TYPO3\CMS\Core\Resource\Exception\ResourceDoesNotExistException;
@@ -73,8 +74,10 @@ use TYPO3\CMS\Frontend\Typolink\UnableToLinkException;
  * The class is normally instantiated and referred to as "cObj".
  * When you call your own PHP-code typically through a USER or USER_INT cObject then it is this class that instantiates the object and calls the main method. Before it does so it will set (if you are using classes) a reference to itself in the internal variable "cObj" of the object. Thus you can access all functions and data from this class by $this->cObj->... from within you classes written to be USER or USER_INT content objects.
  */
-class ContentObjectRenderer
+class ContentObjectRenderer implements LoggerAwareInterface
 {
+    use LoggerAwareTrait;
+
     /**
      * @var array
      */
@@ -4575,9 +4578,7 @@ class ContentObjectRenderer
                         $fileObject = $this->getResourceFactory()->retrieveFileOrFolderObject($file);
                     }
                 } catch (Exception $exception) {
-                    /** @var \TYPO3\CMS\Core\Log\Logger $logger */
-                    $logger = GeneralUtility::makeInstance(LogManager::class)->getLogger(__CLASS__);
-                    $logger->warning('The image "' . $file . '" could not be found and won\'t be included in frontend output', ['exception' => $exception]);
+                    $this->logger->warning('The image "' . $file . '" could not be found and won\'t be included in frontend output', ['exception' => $exception]);
                     return null;
                 }
             }
@@ -5017,9 +5018,7 @@ class ContentObjectRenderer
                 $fileObject = null;
             }
         } catch (Exception $exception) {
-            /** @var \TYPO3\CMS\Core\Log\Logger $logger */
-            $logger = GeneralUtility::makeInstance(LogManager::class)->getLogger(__CLASS__);
-            $logger->warning('The file "' . $fileUidOrCurrentKeyword . '" could not be found and won\'t be included in frontend output', ['exception' => $exception]);
+            $this->logger->warning('The file "' . $fileUidOrCurrentKeyword . '" could not be found and won\'t be included in frontend output', ['exception' => $exception]);
             $fileObject = null;
         }
 
@@ -5259,9 +5258,7 @@ class ContentObjectRenderer
         try {
             $linkDetails = $linkService->resolve($linkParameter);
         } catch (Exception\InvalidPathException $exception) {
-            $logger = GeneralUtility::makeInstance(LogManager::class)->getLogger(__CLASS__);
-            $logger->warning('The link could not be generated', ['exception' => $exception]);
-
+            $this->logger->warning('The link could not be generated', ['exception' => $exception]);
             return $linkText;
         }
 
index f5e5385..0528609 100644 (file)
@@ -15,13 +15,11 @@ namespace TYPO3\CMS\Frontend\Tests\Unit\ContentObject;
  */
 
 use Prophecy\Argument;
-use Psr\Log\LoggerInterface;
 use TYPO3\CMS\Core\Cache\CacheManager;
 use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface as CacheFrontendInterface;
 use TYPO3\CMS\Core\Core\ApplicationContext;
 use TYPO3\CMS\Core\LinkHandling\LinkService;
 use TYPO3\CMS\Core\Log\Logger;
-use TYPO3\CMS\Core\Log\LogManager;
 use TYPO3\CMS\Core\Resource\Exception\InvalidPathException;
 use TYPO3\CMS\Core\Resource\File;
 use TYPO3\CMS\Core\Resource\ResourceFactory;
@@ -128,7 +126,6 @@ class ContentObjectRendererTest extends \TYPO3\TestingFramework\Core\Unit\UnitTe
     protected function setUp()
     {
         $this->singletonInstances = GeneralUtility::getSingletonInstances();
-        $this->createMockedLoggerAndLogManager();
 
         $this->templateServiceMock =
             $this->getMockBuilder(TemplateService::class)
@@ -154,6 +151,9 @@ class ContentObjectRendererTest extends \TYPO3\TestingFramework\Core\Unit\UnitTe
             ['getResourceFactory', 'getEnvironmentVariable'],
             [$this->frontendControllerMock]
         );
+
+        $logger = $this->prophesize(Logger::class);
+        $this->subject->setLogger($logger->reveal());
         $this->subject->setContentObjectClassMap($this->contentObjectMap);
         $this->subject->start([], 'tt_content');
     }
@@ -177,19 +177,6 @@ class ContentObjectRendererTest extends \TYPO3\TestingFramework\Core\Unit\UnitTe
     }
 
     /**
-     * Avoid logging to the file system (file writer is currently the only configured writer)
-     */
-    protected function createMockedLoggerAndLogManager()
-    {
-        $logManagerMock = $this->getMockBuilder(LogManager::class)->getMock();
-        $loggerMock = $this->getMockBuilder(LoggerInterface::class)->getMock();
-        $logManagerMock->expects($this->any())
-            ->method('getLogger')
-            ->willReturn($loggerMock);
-        GeneralUtility::setSingletonInstance(LogManager::class, $logManagerMock);
-    }
-
-    /**
      * Converts the subject and the expected result into utf-8.
      *
      * @param string $subject the subject, will be modified
@@ -3105,12 +3092,9 @@ class ContentObjectRendererTest extends \TYPO3\TestingFramework\Core\Unit\UnitTe
         GeneralUtility::setSingletonInstance(LinkService::class, $linkService->reveal());
         $linkService->resolve('foo')->willThrow(InvalidPathException::class);
 
-        $logManager = $this->prophesize(LogManager::class);
-        GeneralUtility::setSingletonInstance(LogManager::class, $logManager->reveal());
         $logger = $this->prophesize(Logger::class);
         $logger->warning('The link could not be generated', Argument::any())->shouldBeCalled();
-        $logManager->getLogger(Argument::any())->willReturn($logger->reveal());
-
+        $this->subject->setLogger($logger->reveal());
         $this->subject->typoLink('foo', ['parameter' => 'foo']);
     }