[BUGFIX] Call initializeObject() after instantiating empty objects 14/39914/5
authorMorton Jonuschat <m.jonuschat@mojocode.de>
Wed, 3 Jun 2015 09:38:22 +0000 (11:38 +0200)
committerAndreas Wolf <andreas.wolf@typo3.org>
Fri, 5 Jun 2015 07:38:49 +0000 (09:38 +0200)
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 <mbrodala@pagemachine.de>
Tested-by: Mathias Brodala <mbrodala@pagemachine.de>
Reviewed-by: Stefan Neufeind <typo3.neufeind@speedpartner.de>
Reviewed-by: Andreas Fernandez <typo3@scripting-base.de>
Reviewed-by: Frans Saris <franssaris@gmail.com>
Tested-by: Frans Saris <franssaris@gmail.com>
Reviewed-by: Andreas Wolf <andreas.wolf@typo3.org>
Tested-by: Andreas Wolf <andreas.wolf@typo3.org>
typo3/sysext/extbase/Classes/Object/Container/Container.php
typo3/sysext/extbase/Tests/Unit/Object/Container/ContainerTest.php
typo3/sysext/extbase/Tests/Unit/Object/Container/Fixtures/Testclasses.php

index 5888ef0..2e6d6f9 100644 (file)
@@ -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]);
                }
@@ -243,6 +242,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
         *
         * @param string $message Message (in english).
@@ -347,5 +358,4 @@ class Container implements \TYPO3\CMS\Core\SingletonInterface {
        public function isPrototype($className) {
                return !$this->isSingleton($className);
        }
-
 }
index cfd5d02..9eadf13 100644 (file)
@@ -181,6 +181,14 @@ class ContainerTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
        }
 
        /**
+        * @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
         *
         * @param string $id
@@ -210,6 +218,14 @@ class ContainerTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
        /**
         * @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
+        */
        public function test_canGetChildClass() {
                $object = $this->container->getInstance('t3lib_object_tests_b_child');
                $this->assertInstanceOf('t3lib_object_tests_b_child', $object);
index 7c67c44..ee37cf3 100644 (file)
@@ -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;
+       }
+}