Skip to content

Commit

Permalink
[TASK] Use variable argument-list ObjectManager and Container
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
alexanderschnitzler authored and susannemoog committed May 10, 2018
1 parent 89984e4 commit a6c0fe2
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 13 deletions.
12 changes: 6 additions & 6 deletions typo3/sysext/extbase/Classes/Object/Container/Container.php
Expand Up @@ -92,11 +92,12 @@ protected function getInstantiator()
* @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);
}

/**
Expand Down Expand Up @@ -124,7 +125,7 @@ public function getEmptyObject($className)
* @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) {
Expand Down Expand Up @@ -152,7 +153,7 @@ protected function getInstanceInternal($className, $givenConstructorArguments =
}
$this->prototypeObjectsWhichAreCurrentlyInstanciated[$className] = true;
}
$instance = $this->instanciateObject($classSchema, $givenConstructorArguments);
$instance = $this->instanciateObject($classSchema, ...$givenConstructorArguments);
$this->injectDependencies($instance, $classSchema);
$this->initializeObject($instance);
if (!$classIsSingleton) {
Expand All @@ -171,16 +172,15 @@ protected function getInstanceInternal($className, $givenConstructorArguments =
* @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();
if ($classIsSingleton && !empty($givenConstructorArguments)) {
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;
}
Expand Down
10 changes: 4 additions & 6 deletions typo3/sysext/extbase/Classes/Object/ObjectManager.php
Expand Up @@ -84,18 +84,16 @@ public function isRegistered($objectName)
* 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;
}
Expand Down
Expand Up @@ -31,10 +31,11 @@ public function isRegistered($objectName);
* 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
Expand Down

0 comments on commit a6c0fe2

Please sign in to comment.