[+BUGFIX] Extbase (Object): Disabling Dependency Injection for injectSettings method
authorSebastian Kurfürst <sebastian@typo3.org>
Thu, 13 Jan 2011 05:33:01 +0000 (05:33 +0000)
committerSebastian Kurfürst <sebastian@typo3.org>
Thu, 13 Jan 2011 05:33:01 +0000 (05:33 +0000)
Tx_Extbase_MVC_Controller_ActionController->resolveView() checks for the existence of
an injectSettings() method in a view class and calls it if found, passing on the
settings.
As Tx_Extbase_Object_Container_ClassInfoFactory->getInjectMethods() though
classifies that method as a method for dependency injection which of course fails
leading to an exception "Method is marked as setter for Dependency Injection, but
doesnt have a type annotation".

Bugfix: Make the DI container skip methods with the name
injectSettings(). This has the benefit of making sure nobody
will use that function for injections lateron, making upgrade to
FLOW3 more easy.

Resolves: #11011

typo3/sysext/extbase/Classes/Object/Container/ClassInfoFactory.php
typo3/sysext/extbase/Tests/Unit/Object/Container/ClassInfoFactoryTest.php
typo3/sysext/extbase/Tests/Unit/Object/Container/Fixtures/Testclasses.php

index 3985f54..7a06f3f 100644 (file)
@@ -80,10 +80,10 @@ class Tx_Extbase_Object_Container_ClassInfoFactory {
        }
 
        /**
-        * Build a list of inject methods
+        * Build a list of inject methods for the given class.
 
         * @param ReflectionClass $reflectedClass
-        * @retrn array of inject methods
+        * @return array (nameOfInjectMethod => nameOfClassToBeInjected)
         */
        private function getInjectMethods(ReflectionClass $reflectedClass) {
                $result = array();
@@ -91,11 +91,14 @@ class Tx_Extbase_Object_Container_ClassInfoFactory {
 
                if (is_array($reflectionMethods)) {
                        foreach ($reflectionMethods as $reflectionMethod) {
-                               if ($reflectionMethod->isPublic() && substr($reflectionMethod->getName(), 0, 6) === 'inject') {
+                               if ($reflectionMethod->isPublic()
+                                       && substr($reflectionMethod->getName(), 0, 6) === 'inject'
+                                       && $reflectionMethod->getName() !== 'injectSettings') {
+
                                        $reflectionParameter = $reflectionMethod->getParameters();
                                        if (isset($reflectionParameter[0])) {
                                                if (!$reflectionParameter[0]->getClass()) {
-                                                       throw new Exception('Method is marked as setter for Dependency Injection, but doesnt have a type annotation');
+                                                       throw new Exception('Method "' . $reflectionMethod->getName(). '" of class "' . $reflectedClass->getName() . '" is marked as setter for Dependency Injection, but does not have a type annotation');
                                                }
                                                $result[$reflectionMethod->getName()] = $reflectionParameter[0]->getClass()->getName();
                                        }
index c377798..f82428a 100644 (file)
@@ -35,7 +35,7 @@ require_once(t3lib_extMgm::extPath('extbase') . 'Tests/Unit/Object/Container/Fix
 class Tx_Extbase_Tests_Unit_Object_Container_ClassInfoFactoryTest extends Tx_Extbase_Tests_Unit_BaseTestCase {
 
        /**
-        * @var t3lib_object_ClassInfoFactory
+        * @var Tx_Extbase_Object_Container_ClassInfoFactory
         */
        private $classInfoFactory;
 
@@ -57,6 +57,14 @@ class Tx_Extbase_Tests_Unit_Object_Container_ClassInfoFactoryTest extends Tx_Ext
        /**
         * @test
         */
+       public function buildClassInfoDoesNotIncludeInjectSettingsMethodInListOfInjectMethods() {
+               $classInfo = $this->classInfoFactory->buildClassInfoFromClassName('t3lib_object_tests_class_with_injectsettings');
+               $this->assertEquals(array('injectFoo' => 't3lib_object_tests_resolveablecyclic1'), $classInfo->getInjectMethods());
+       }
+
+       /**
+        * @test
+        */
        public function moreTestsNeedToBeWritten() {
                $this->markTestIncomplete('More tests need to be written!');
        }
index 8a05096..cb741b7 100644 (file)
@@ -191,4 +191,11 @@ class t3lib_object_tests_resolveablecyclic3 implements t3lib_Singleton {
        }
 }
 
+class t3lib_object_tests_class_with_injectsettings {
+       public function injectFoo(t3lib_object_tests_resolveablecyclic1 $c1) {
+       }
+
+       public function injectSettings(array $settings) {
+       }
+}