[TASK] Remove last part of singleton pattern in getUserObj() 71/47771/5
authorBenni Mack <benni@typo3.org>
Tue, 19 Apr 2016 06:03:41 +0000 (08:03 +0200)
committerWouter Wolters <typo3@wouterwolters.nl>
Tue, 19 Apr 2016 08:49:54 +0000 (10:49 +0200)
In 8.0, the logic for having getUserObj() called with a "&" to
implement a singleton pattern has been removed, however
some code is still left and now unused.

Resolves: #75767
Releases: master
Change-Id: I76d5eb211662b60d9b34b38b3c0efb77a3b33fc4
Reviewed-on: https://review.typo3.org/47771
Reviewed-by: Nicole Cordes <typo3@cordes.co>
Tested-by: Nicole Cordes <typo3@cordes.co>
Reviewed-by: Wouter Wolters <typo3@wouterwolters.nl>
Tested-by: Wouter Wolters <typo3@wouterwolters.nl>
typo3/sysext/core/Classes/Utility/GeneralUtility.php
typo3/sysext/core/Tests/Unit/DataHandling/DataHandlerTest.php
typo3/sysext/frontend/Tests/Unit/ContentObject/ContentObjectRendererTest.php
typo3/sysext/frontend/Tests/Unit/Page/PageRepositoryTest.php
typo3/sysext/frontend/Tests/Unit/View/AdminPanelViewTest.php

index a97d22b..eef021e 100755 (executable)
@@ -3754,43 +3754,32 @@ class GeneralUtility
     }
 
     /**
+     * This method should be avoided, as it will be deprecated soon. Instead use makeInstance() directly.
+     *
      * Creates and returns reference to a user defined object.
      * This function can return an object reference if you like.
-     * Just prefix the function call with "&": "$objRef = &\TYPO3\CMS\Core\Utility\GeneralUtility::getUserObj('EXT:myext/class.tx_myext_myclass.php:&tx_myext_myclass');".
-     * This will work ONLY if you prefix the class name with "&" as well. See description of function arguments.
-     *
-     * @todo Deprecate the whole method in several steps:
-     *      1. Deprecated singleton pattern (was removed in TYPO3 CMS 8)
-     *      2. Deprecate file prefix/ require file,
-     *      3. Deprecate usage without valid class name.
-     *      4. The last step should be to deprecate the method itself.
      *
-     * @param string $classRef Class reference, '[file-reference":"]["&"]class-name'. You can prefix the class name with "[file-reference]:" and \TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName() will then be used to resolve the filename and subsequently include it by "require_once()" which means you don't have to worry about including the class file either! Example: "EXT:realurl/class.tx_realurl.php:&tx_realurl". Finally; for the class name you can prefix it with "&" and you will reuse the previous instance of the object identified by the full reference string (meaning; if you ask for the same $classRef later in another place in the code you will get a reference to the first created one!).
+     * @param string $classRef Class reference, '[file-reference":"]class-name'. You can prefix the class name with "[file-reference]:" and \TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName() will then be used to resolve the filename and subsequently include it by "require_once()" which means you don't have to worry about including the class file either! Example: "EXT:realurl/class.tx_realurl.php:tx_realurl".
      * @return object The instance of the class asked for. Instance is created with \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance
      * @see callUserFunction()
      */
     public static function getUserObj($classRef)
     {
-        // Check persistent object and if found, call directly and exit.
-        if (is_object($GLOBALS['T3_VAR']['getUserObj'][$classRef])) {
-            return $GLOBALS['T3_VAR']['getUserObj'][$classRef];
-        } else {
-            // Check file-reference prefix; if found, require_once() the file (should be library of code)
-            if (strpos($classRef, ':') !== false) {
-                list($file, $class) = self::revExplode(':', $classRef, 2);
-                $requireFile = self::getFileAbsFileName($file);
-                if ($requireFile) {
-                    require_once $requireFile;
-                }
-            } else {
-                $class = $classRef;
+        // Check file-reference prefix; if found, require_once() the file (should be library of code)
+        if (strpos($classRef, ':') !== false) {
+            list($file, $class) = self::revExplode(':', $classRef, 2);
+            $requireFile = self::getFileAbsFileName($file);
+            if ($requireFile) {
+                require_once $requireFile;
             }
+        } else {
+            $class = $classRef;
+        }
 
-            // Check if class exists:
-            if (class_exists($class)) {
-                $classObj = self::makeInstance($class);
-                return $classObj;
-            }
+        // Check if class exists:
+        if (class_exists($class)) {
+            $classObj = self::makeInstance($class);
+            return $classObj;
         }
     }
 
index b7ce5ca..3215d2a 100644 (file)
@@ -361,7 +361,7 @@ class DataHandlerTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
         $hookMock = $this->getMock(\TYPO3\CMS\Core\DataHandling\DataHandlerCheckModifyAccessListHookInterface::class, array('checkModifyAccessList'), array(), $hookClass);
         $hookMock->expects($this->once())->method('checkModifyAccessList');
         $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['checkModifyAccessList'][] = $hookClass;
-        $GLOBALS['T3_VAR']['getUserObj'][$hookClass] = $hookMock;
+        GeneralUtility::addInstance($hookClass, $hookMock);
         $this->subject->checkModifyAccessList('tt_content');
     }
 
@@ -460,7 +460,7 @@ class DataHandlerTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
         $hookMock = $this->getMock($hookClass, array('checkFlexFormValue_beforeMerge'));
         $hookMock->expects($this->once())->method('checkFlexFormValue_beforeMerge');
         $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['checkFlexFormValue'][] = $hookClass;
-        $GLOBALS['T3_VAR']['getUserObj'][$hookClass] = $hookMock;
+        GeneralUtility::addInstance($hookClass, $hookMock);
         $this->subject->_call('checkValueForFlex', [], [], [], '', 0, '', '', 0, 0, 0, [], '');
     }
 
index d5cf253..4572f2b 100755 (executable)
@@ -3518,7 +3518,7 @@ class ContentObjectRendererTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
 
         $className = $this->getUniqueId('tx_coretest_getImageSourceCollectionHookCalled');
         $getImageSourceCollectionHookMock = $this->getMock(\TYPO3\CMS\Frontend\ContentObject\ContentObjectOneSourceCollectionHookInterface::class, array('getOneSourceCollection'), array(), $className);
-        $GLOBALS['T3_VAR']['getUserObj'][$className] = $getImageSourceCollectionHookMock;
+        GeneralUtility::addInstance($className, $getImageSourceCollectionHookMock);
         $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_content.php']['getImageSourceCollection'][] = $className;
 
         $getImageSourceCollectionHookMock
index 0811c52..816c758 100644 (file)
@@ -13,6 +13,7 @@ namespace TYPO3\CMS\Frontend\Tests\Unit\Page;
  *
  * The TYPO3 project - inspiring people to share!
  */
+use TYPO3\CMS\Core\Utility\GeneralUtility;
 
 /**
  * Test case
@@ -64,7 +65,7 @@ class PageRepositoryTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
         $className = $this->getUniqueId('tx_coretest');
         $getPageHookMock = $this->getMock(\TYPO3\CMS\Frontend\Page\PageRepositoryGetPageHookInterface::class, array('getPage_preProcess'), array(), $className);
         // Register hook mock object
-        $GLOBALS['T3_VAR']['getUserObj'][$className] = $getPageHookMock;
+        GeneralUtility::addInstance($className, $getPageHookMock);
         $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_page.php']['getPage'][] = $className;
         // Test if hook is called and register a callback method to check given arguments
         $getPageHookMock->expects($this->once())->method('getPage_preProcess')->will($this->returnCallback(array($this, 'isGetPagePreProcessCalledCallback')));
index d3b31fe..9e5e14a 100644 (file)
@@ -13,6 +13,7 @@ namespace TYPO3\CMS\Frontend\Tests\Unit\View;
  *
  * The TYPO3 project - inspiring people to share!
  */
+use TYPO3\CMS\Core\Utility\GeneralUtility;
 
 /**
  * Test case
@@ -72,7 +73,7 @@ class AdminPanelViewTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
     {
         $hookClass = $this->getUniqueId('tx_coretest');
         $hookMock = $this->getMock(\TYPO3\CMS\Frontend\View\AdminPanelViewHookInterface::class, array(), array(), $hookClass);
-        $GLOBALS['T3_VAR']['getUserObj'][$hookClass] = $hookMock;
+        GeneralUtility::addInstance($hookClass, $hookMock);
         $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_adminpanel.php']['extendAdminPanel'][] = $hookClass;
         /** @var $adminPanelMock \PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Frontend\View\AdminPanelView */
         $adminPanelMock = $this->getMock(\TYPO3\CMS\Frontend\View\AdminPanelView::class, array('extGetLL'), array(), '', false);