[TASK] Use variable argument-list ObjectManager and Container 02/56902/5
authorAlexander Schnitzler <git@alexanderschnitzler.de>
Wed, 9 May 2018 15:12:36 +0000 (17:12 +0200)
committerSusanne Moog <susanne.moog@typo3.org>
Thu, 10 May 2018 10:14:59 +0000 (12:14 +0200)
Supported since PHP 5.6. This makes the code easier to read
and allows to properly document using phpdoc.

https://secure.php.net/manual/en/functions.arguments.php#functions.variable-arg-list

Releases: master
Resolves: #84956
Change-Id: I73256207c114ef3c35a64518dd039dac2e33976a
Reviewed-on: https://review.typo3.org/56902
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Mathias Brodala <mbrodala@pagemachine.de>
Tested-by: Mathias Brodala <mbrodala@pagemachine.de>
Reviewed-by: Mathias Schreiber <mathias.schreiber@typo3.com>
Tested-by: Mathias Schreiber <mathias.schreiber@typo3.com>
Reviewed-by: Susanne Moog <susanne.moog@typo3.org>
Tested-by: Susanne Moog <susanne.moog@typo3.org>
typo3/sysext/extbase/Classes/Object/Container/Container.php
typo3/sysext/extbase/Classes/Object/ObjectManager.php
typo3/sysext/extbase/Classes/Object/ObjectManagerInterface.php

index 24a86b2..c185dbf 100644 (file)
@@ -92,11 +92,12 @@ class Container implements \TYPO3\CMS\Core\SingletonInterface
      * @param string $className
      * @param array $givenConstructorArguments the list of constructor arguments as array
      * @return object the built object
+     * @internal
      */
     public function getInstance($className, $givenConstructorArguments = [])
     {
         $this->prototypeObjectsWhichAreCurrentlyInstanciated = [];
-        return $this->getInstanceInternal($className, $givenConstructorArguments);
+        return $this->getInstanceInternal($className, ...$givenConstructorArguments);
     }
 
     /**
@@ -124,7 +125,7 @@ class Container implements \TYPO3\CMS\Core\SingletonInterface
      * @throws \TYPO3\CMS\Extbase\Object\Exception\CannotBuildObjectException
      * @return object the built object
      */
-    protected function getInstanceInternal($className, $givenConstructorArguments = [])
+    protected function getInstanceInternal($className, ...$givenConstructorArguments)
     {
         $className = $this->getImplementationClassName($className);
         if ($className === \TYPO3\CMS\Extbase\Object\Container\Container::class) {
@@ -152,7 +153,7 @@ class Container implements \TYPO3\CMS\Core\SingletonInterface
             }
             $this->prototypeObjectsWhichAreCurrentlyInstanciated[$className] = true;
         }
-        $instance = $this->instanciateObject($classSchema, $givenConstructorArguments);
+        $instance = $this->instanciateObject($classSchema, ...$givenConstructorArguments);
         $this->injectDependencies($instance, $classSchema);
         $this->initializeObject($instance);
         if (!$classIsSingleton) {
@@ -171,7 +172,7 @@ class Container implements \TYPO3\CMS\Core\SingletonInterface
      * @throws \TYPO3\CMS\Extbase\Object\Exception
      * @return object the new instance
      */
-    protected function instanciateObject(ClassSchema $classSchema, array $givenConstructorArguments)
+    protected function instanciateObject(ClassSchema $classSchema, ...$givenConstructorArguments)
     {
         $className = $classSchema->getClassName();
         $classIsSingleton = $classSchema->isSingleton();
@@ -179,8 +180,7 @@ class Container implements \TYPO3\CMS\Core\SingletonInterface
             throw new \TYPO3\CMS\Extbase\Object\Exception('Object "' . $className . '" has explicit constructor arguments but is a singleton; this is not allowed.', 1292858051);
         }
         $constructorArguments = $this->getConstructorArguments($className, $classSchema, $givenConstructorArguments);
-        array_unshift($constructorArguments, $className);
-        $instance = call_user_func_array([GeneralUtility::class, 'makeInstance'], $constructorArguments);
+        $instance = GeneralUtility::makeInstance($className, ...$constructorArguments);
         if ($classIsSingleton) {
             $this->singletonInstances[$className] = $instance;
         }
index b3c0a46..3fb2ed3 100644 (file)
@@ -84,18 +84,16 @@ class ObjectManager implements ObjectManagerInterface
      * Returns a fresh or existing instance of the object specified by $objectName.
      *
      * @param string $objectName The name of the object to return an instance of
+     * @param array $constructorArguments
      * @return object The object instance
      * @api
      */
-    public function get($objectName)
+    public function get($objectName, ...$constructorArguments)
     {
-        $arguments = func_get_args();
-        array_shift($arguments);
         if ($objectName === 'DateTime') {
-            array_unshift($arguments, $objectName);
-            $instance = call_user_func_array([\TYPO3\CMS\Core\Utility\GeneralUtility::class, 'makeInstance'], $arguments);
+            $instance = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance($objectName, ...$constructorArguments);
         } else {
-            $instance = $this->objectContainer->getInstance($objectName, $arguments);
+            $instance = $this->objectContainer->getInstance($objectName, $constructorArguments);
         }
         return $instance;
     }
index da88e9c..23c03c6 100644 (file)
@@ -31,10 +31,11 @@ interface ObjectManagerInterface extends \TYPO3\CMS\Core\SingletonInterface
      * Returns a fresh or existing instance of the object specified by $objectName.
      *
      * @param string $objectName The name of the object to return an instance of
+     * @param array ...$constructorArguments
      * @return object The object instance
      * @api
      */
-    public function get($objectName);
+    public function get($objectName, ...$constructorArguments);
 
     /**
      * Create an instance of $className without calling its constructor