[BUGFIX] ObjectContainer does not support namespaces
authorAndreas Wolf <andreas.wolf@ikt-werk.de>
Tue, 14 Jun 2011 13:25:08 +0000 (15:25 +0200)
committerFelix Oertel <typo3@foertel.com>
Fri, 16 Mar 2012 22:29:37 +0000 (23:29 +0100)
The object container (more specificially: the class info cache it uses)
fails when trying to get an instance of a namespaced class. This comes
from the namespace separator \, which is seen as an invalid character
for cache identifiers. Therefore the class name will be stored as sha1
hash.

Change-Id: Iaab7c635e733530dec26643078824a1d70557faf
Resolves: #27438
Releases: 1.4, 4.7, 4.8

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/NamespaceTestclasses.php [new file with mode: 0644]

index 87d1b47..d8567c3 100644 (file)
@@ -336,12 +336,14 @@ class Tx_Extbase_Object_Container_Container implements t3lib_Singleton {
         * @return Tx_Extbase_Object_Container_ClassInfo
         */
        private function getClassInfo($className) {
+               $classNameHash = sha1($className);
+
                        // we also need to make sure that the cache is returning a vaild object
                        // in case something went wrong with unserialization etc..
-               if (!$this->getClassInfoCache()->has($className) || !is_object($this->getClassInfoCache()->get($className))) {
-                       $this->getClassInfoCache()->set($className, $this->getClassInfoFactory()->buildClassInfoFromClassName($className));
+               if (!$this->getClassInfoCache()->has($classNameHash) || !is_object($this->getClassInfoCache()->get($classNameHash))) {
+                       $this->getClassInfoCache()->set($classNameHash, $this->getClassInfoFactory()->buildClassInfoFromClassName($className));
                }
 
-               return $this->getClassInfoCache()->get($className);
+               return $this->getClassInfoCache()->get($classNameHash);
        }
 }
\ No newline at end of file
index 2007cfd..8076b71 100644 (file)
@@ -23,6 +23,7 @@
 ***************************************************************/
 
 require_once(t3lib_extMgm::extPath('extbase') . 'Tests/Unit/Object/Container/Fixtures/Testclasses.php');
+require_once(t3lib_extMgm::extPath('extbase') . 'Tests/Unit/Object/Container/Fixtures/NamespaceTestclasses.php');
 
 /**
  * Testcase for class t3lib_object_Container.
@@ -34,6 +35,9 @@ require_once(t3lib_extMgm::extPath('extbase') . 'Tests/Unit/Object/Container/Fix
  */
 class Tx_Extbase_Tests_Unit_Object_Container_ContainerTest extends Tx_Extbase_Tests_Unit_BaseTestCase {
 
+       /**
+        * @var Tx_Extbase_Object_Container_Container
+        */
        private $container;
 
        public function setUp() {
@@ -56,6 +60,14 @@ class Tx_Extbase_Tests_Unit_Object_Container_ContainerTest extends Tx_Extbase_Te
        /**
         * @test
         */
+       public function getInstanceReturnsInstanceOfSimpleNamespacedClass() {
+               $object = $this->container->getInstance('Tx\Extbase\Object\Container\Fixtures\NamespacedClass');
+               $this->assertInstanceOf('Tx\Extbase\Object\Container\Fixtures\NamespacedClass', $object);
+       }
+
+       /**
+        * @test
+        */
        public function getInstanceReturnsInstanceOfAClassWithConstructorInjection() {
                $object = $this->container->getInstance('t3lib_object_tests_b');
                $this->assertInstanceOf('t3lib_object_tests_b', $object);
@@ -165,6 +177,23 @@ class Tx_Extbase_Tests_Unit_Object_Container_ContainerTest extends Tx_Extbase_Te
        /**
         * @test
         */
+       public function getInstanceUsesClassNameSha1AsCacheKey() {
+               $className = 'Tx\Extbase\Object\Container\Fixtures\NamespacedClass';
+               $classNameHash = sha1($className);
+
+               $mockedCache = $this->getMock('Tx_Extbase_Object_Container_ClassInfoCache',array('has', 'set'));
+               $this->container = $this->getMock('Tx_Extbase_Object_Container_Container', array('log','getClassInfoCache'));
+               $this->container->expects($this->any())->method('getClassInfoCache')->will($this->returnValue($mockedCache));
+
+               $mockedCache->expects($this->any())->method('has')->will($this->returnValue(FALSE));
+               $mockedCache->expects($this->once())->method('set')->with($classNameHash, $this->anything());
+
+               $this->container->getInstance($className);
+       }
+
+       /**
+        * @test
+        */
        public function getEmptyObjectReturnsInstanceOfSimpleClass() {
                $object = $this->container->getEmptyObject('t3lib_object_tests_c');
                $this->assertInstanceOf('t3lib_object_tests_c', $object);
diff --git a/typo3/sysext/extbase/Tests/Unit/Object/Container/Fixtures/NamespaceTestclasses.php b/typo3/sysext/extbase/Tests/Unit/Object/Container/Fixtures/NamespaceTestclasses.php
new file mode 100644 (file)
index 0000000..1e80487
--- /dev/null
@@ -0,0 +1,5 @@
+<?php
+
+namespace Tx\Extbase\Object\Container\Fixtures;
+
+class NamespacedClass {}