[FEATURE] Inject logger into newly created objects 01/54101/9
authorMarkus Klein <markus.klein@typo3.org>
Sun, 10 Sep 2017 12:06:11 +0000 (14:06 +0200)
committerHelmut Hummel <typo3@helhum.io>
Mon, 11 Sep 2017 11:23:00 +0000 (13:23 +0200)
Instantiating new objects with makeInstance or the ObjectManager
will automatically inject a logger into the new objects, if those
implement the LoggerAwareInterface.

Resolves: #82441
Releases: master
Change-Id: I38e1a4619857f3fc2d88b0b95a9721b289c679d3
Reviewed-on: https://review.typo3.org/54101
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
Reviewed-by: Stefan Neufeind <typo3.neufeind@speedpartner.de>
Tested-by: Stefan Neufeind <typo3.neufeind@speedpartner.de>
Reviewed-by: Helmut Hummel <typo3@helhum.io>
Tested-by: Helmut Hummel <typo3@helhum.io>
typo3/sysext/core/Classes/Utility/GeneralUtility.php
typo3/sysext/core/Documentation/Changelog/master/Feature-82441-InjectLoggerWhenCreatingObjects.rst [new file with mode: 0644]
typo3/sysext/core/Tests/Unit/Utility/Fixtures/GeneralUtilityMakeInstanceInjectLoggerFixture.php [new file with mode: 0644]
typo3/sysext/core/Tests/Unit/Utility/GeneralUtilityTest.php

index 6e36761..b87b51b 100644 (file)
@@ -15,10 +15,12 @@ namespace TYPO3\CMS\Core\Utility;
  */
 
 use GuzzleHttp\Exception\RequestException;
+use Psr\Log\LoggerAwareInterface;
 use TYPO3\CMS\Core\Core\ApplicationContext;
 use TYPO3\CMS\Core\Core\ClassLoadingInformation;
 use TYPO3\CMS\Core\Database\ConnectionPool;
 use TYPO3\CMS\Core\Http\RequestFactory;
+use TYPO3\CMS\Core\Log\LogManager;
 use TYPO3\CMS\Core\Service\OpcodeCacheService;
 use TYPO3\CMS\Core\SingletonInterface;
 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
@@ -3488,6 +3490,9 @@ class GeneralUtility
         if ($instance instanceof SingletonInterface) {
             self::$singletonInstances[$finalClassName] = $instance;
         }
+        if ($instance instanceof LoggerAwareInterface) {
+            $instance->setLogger(static::makeInstance(LogManager::class)->getLogger($className));
+        }
         return $instance;
     }
 
diff --git a/typo3/sysext/core/Documentation/Changelog/master/Feature-82441-InjectLoggerWhenCreatingObjects.rst b/typo3/sysext/core/Documentation/Changelog/master/Feature-82441-InjectLoggerWhenCreatingObjects.rst
new file mode 100644 (file)
index 0000000..4438502
--- /dev/null
@@ -0,0 +1,36 @@
+.. include:: ../../Includes.txt
+
+=====================================================
+Feature: #82441 - Inject logger when creating objects
+=====================================================
+
+See :issue:`82441`
+
+Description
+===========
+
+Classes that implement :php:`\Psr\Log\LoggerAwareInterface` automatically
+get a logger instance injected when a class instance is created via
+:php:`GeneralUtility::makeInstance()` and :php:`ObjectManger::get()`.
+
+For developer convenience the :php:`\Psr\Log\LoggerAwareTrait` can be used.
+The trait adds a public :php:`setLogger()` and a protected :php:`$logger` property
+to the class, no further code is needed to successfully implement the interface.
+
+A minimal example looks like this (example from a test case fixture):
+
+.. code-block:: php
+
+<?php
+declare(strict_types=1);
+namespace TYPO3\CMS\Core\Tests\Unit\Utility\Fixtures;
+
+use Psr\Log\LoggerAwareInterface;
+use Psr\Log\LoggerAwareTrait;
+
+class GeneralUtilityMakeInstanceInjectLoggerFixture implements LoggerAwareInterface
+{
+    use LoggerAwareTrait;
+}
+
+.. index:: PHP-API
\ No newline at end of file
diff --git a/typo3/sysext/core/Tests/Unit/Utility/Fixtures/GeneralUtilityMakeInstanceInjectLoggerFixture.php b/typo3/sysext/core/Tests/Unit/Utility/Fixtures/GeneralUtilityMakeInstanceInjectLoggerFixture.php
new file mode 100644 (file)
index 0000000..228581a
--- /dev/null
@@ -0,0 +1,36 @@
+<?php
+declare(strict_types=1);
+namespace TYPO3\CMS\Core\Tests\Unit\Utility\Fixtures;
+
+/*
+ * This file is part of the TYPO3 CMS project.
+ *
+ * It is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License, either version 2
+ * of the License, or any later version.
+ *
+ * For the full copyright and license information, please read the
+ * LICENSE.txt file that was distributed with this source code.
+ *
+ * The TYPO3 project - inspiring people to share!
+ */
+
+use Psr\Log\LoggerAwareInterface;
+use Psr\Log\LoggerAwareTrait;
+use Psr\Log\LoggerInterface;
+
+/**
+ * Fixture for a class which gets logger injected
+ */
+class GeneralUtilityMakeInstanceInjectLoggerFixture implements LoggerAwareInterface
+{
+    use LoggerAwareTrait;
+
+    /**
+     * @return LoggerInterface
+     */
+    public function getLogger(): LoggerInterface
+    {
+        return $this->logger;
+    }
+}
index bc708d0..b5eb6ce 100644 (file)
@@ -17,11 +17,13 @@ namespace TYPO3\CMS\Core\Tests\Unit\Utility;
 use org\bovigo\vfs\vfsStream;
 use org\bovigo\vfs\vfsStreamDirectory;
 use org\bovigo\vfs\vfsStreamWrapper;
+use Psr\Log\LoggerInterface;
 use TYPO3\CMS\Core\Package\Package;
 use TYPO3\CMS\Core\Package\PackageManager;
 use TYPO3\CMS\Core\Tests\Unit\Utility\AccessibleProxies\ExtensionManagementUtilityAccessibleProxy;
 use TYPO3\CMS\Core\Tests\Unit\Utility\Fixtures\GeneralUtilityFilesystemFixture;
 use TYPO3\CMS\Core\Tests\Unit\Utility\Fixtures\GeneralUtilityFixture;
+use TYPO3\CMS\Core\Tests\Unit\Utility\Fixtures\GeneralUtilityMakeInstanceInjectLoggerFixture;
 use TYPO3\CMS\Core\Tests\Unit\Utility\Fixtures\OriginalClassFixture;
 use TYPO3\CMS\Core\Tests\Unit\Utility\Fixtures\OtherReplacementClassFixture;
 use TYPO3\CMS\Core\Tests\Unit\Utility\Fixtures\ReplacementClassFixture;
@@ -3854,6 +3856,15 @@ class GeneralUtilityTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
     /**
      * @test
      */
+    public function makeInstanceInjectsLogger()
+    {
+        $instance = GeneralUtility::makeInstance(GeneralUtilityMakeInstanceInjectLoggerFixture::class);
+        $this->assertInstanceOf(LoggerInterface::class, $instance->getLogger());
+    }
+
+    /**
+     * @test
+     */
     public function setSingletonInstanceForEmptyClassNameThrowsException()
     {
         $this->expectException(\InvalidArgumentException::class);