Commit ec4f49f6 authored by Morton Jonuschat's avatar Morton Jonuschat Committed by Andreas Wolf
Browse files

[BUGFIX] Call initializeObject() after instantiating empty objects

If the object is initializable and has a callable initializeObject()
method run that so that initialization work will be done.

This affects model instances retrieved from the persistence.

Resolves: #67272
Resolves: #67274
Related: #66473
Releases: master, 6.2
Change-Id: I7b0278855270e8fa6ef75a424d339d072af29c54
Reviewed-on: http://review.typo3.org/39914


Reviewed-by: Mathias Brodala's avatarMathias Brodala <mbrodala@pagemachine.de>
Tested-by: Mathias Brodala's avatarMathias Brodala <mbrodala@pagemachine.de>
Reviewed-by: default avatarStefan Neufeind <typo3.neufeind@speedpartner.de>
Reviewed-by: Andreas Fernandez's avatarAndreas Fernandez <typo3@scripting-base.de>
Reviewed-by: Frans Saris's avatarFrans Saris <franssaris@gmail.com>
Tested-by: Frans Saris's avatarFrans Saris <franssaris@gmail.com>
Reviewed-by: Andreas Wolf's avatarAndreas Wolf <andreas.wolf@typo3.org>
Tested-by: Andreas Wolf's avatarAndreas Wolf <andreas.wolf@typo3.org>
parent 8edc2bef
......@@ -136,6 +136,7 @@ class Container implements \TYPO3\CMS\Core\SingletonInterface {
$classInfo = $this->getClassInfo($className);
$object = $this->getInstantiator()->instantiate($className);
$this->injectDependencies($object, $classInfo);
$this->initializeObject($object, $classInfo);
return $object;
}
......@@ -176,9 +177,7 @@ class Container implements \TYPO3\CMS\Core\SingletonInterface {
}
$instance = $this->instanciateObject($classInfo, $givenConstructorArguments);
$this->injectDependencies($instance, $classInfo);
if ($classInfo->getIsInitializeable() && is_callable(array($instance, 'initializeObject'))) {
$instance->initializeObject();
}
$this->initializeObject($instance, $classInfo);
if (!$classIsSingleton) {
unset($this->prototypeObjectsWhichAreCurrentlyInstanciated[$className]);
}
......@@ -242,6 +241,18 @@ class Container implements \TYPO3\CMS\Core\SingletonInterface {
}
}
/**
* Initialize $instance after injecting all dependencies
*
* @param object $instance
* @param \TYPO3\CMS\Extbase\Object\Container\ClassInfo $classInfo
*/
protected function initializeObject($instance, \TYPO3\CMS\Extbase\Object\Container\ClassInfo $classInfo) {
if ($classInfo->getIsInitializeable() && is_callable(array($instance, 'initializeObject'))) {
$instance->initializeObject();
}
}
/**
* Wrapper for dev log, in order to ease testing
*
......@@ -347,5 +358,4 @@ class Container implements \TYPO3\CMS\Core\SingletonInterface {
public function isPrototype($className) {
return !$this->isSingleton($className);
}
}
......@@ -180,6 +180,14 @@ class ContainerTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
$this->assertEquals($className, $this->cachedClassInfo->getClassName());
}
/**
* @test
*/
public function getInstanceInitializesObjects() {
$instance = $this->container->getInstance('t3lib_object_tests_initializable');
$this->assertTrue($instance->isInitialized(), 't3lib_object_tests_initializable was not completely initialized');
}
/**
* Callback for getInstanceUsesClassNameSha1AsCacheKey
*
......@@ -207,6 +215,14 @@ class ContainerTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
$this->assertInstanceOf('t3lib_object_tests_serializable', $object);
}
/**
* @test
*/
public function getEmptyObjectInitializesObjects() {
$object = $this->container->getEmptyObject('t3lib_object_tests_initializable');
$this->assertTrue($object->isInitialized(), 't3lib_object_tests_initializable was not completely initialized');
}
/**
* @test
*/
......
......@@ -476,3 +476,24 @@ class t3lib_object_prototypeNeedsSingletonInConstructor {
}
}
/**
* Class that needs initialization after instantiation
*/
class t3lib_object_tests_initializable {
/**
* @var bool
*/
protected $initialized = FALSE;
public function initializeObject() {
$this->initialized = TRUE;
}
/**
* @return boolean
*/
public function isInitialized() {
return $this->initialized;
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment