TYPO3 API  SVNRelease
ClassInfoFactory.php
Go to the documentation of this file.
00001 <?php
00002 /***************************************************************
00003 *  Copyright notice
00004 *
00005 *  (c) 2010 Extbase Team
00006 *  All rights reserved
00007 *
00008 *  This class is a backport of the corresponding class of FLOW3.
00009 *  All credits go to the v5 team.
00010 *
00011 *  This script is part of the TYPO3 project. The TYPO3 project is
00012 *  free software; you can redistribute it and/or modify
00013 *  it under the terms of the GNU General Public License as published by
00014 *  the Free Software Foundation; either version 2 of the License, or
00015 *  (at your option) any later version.
00016 *
00017 *  The GNU General Public License can be found at
00018 *  http://www.gnu.org/copyleft/gpl.html.
00019 *
00020 *  This script is distributed in the hope that it will be useful,
00021 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00022 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023 *  GNU General Public License for more details.
00024 *
00025 *  This copyright notice MUST APPEAR in all copies of the script!
00026 ***************************************************************/
00027 
00028 /**
00029  * TYPO3 Dependency Injection container
00030  *
00031  * @author Daniel Pötzinger
00032  */
00033 class Tx_Extbase_Object_Container_ClassInfoFactory {
00034 
00035     /**
00036      * Factory metod that builds a ClassInfo Object for the given classname - using reflection
00037      *
00038      * @param string $className The class name to build the class info for
00039      * @return Tx_Extbase_Object_Container_ClassInfo the class info
00040      */
00041     public function buildClassInfoFromClassName($className) {
00042         try {
00043             $reflectedClass = new ReflectionClass($className);
00044         } catch (Exception $e) {
00045             throw new Tx_Extbase_Object_Container_Exception_UnknownObjectException('Could not analyse class:' . $className . ' maybe not loaded or no autoloader?', 1289386765);
00046         }
00047         $constructorArguments = $this->getConstructorArguments($reflectedClass);
00048         $injectMethods = $this->getInjectMethods($reflectedClass);
00049         return new Tx_Extbase_Object_Container_ClassInfo($className, $constructorArguments, $injectMethods);
00050     }
00051 
00052     /**
00053      * Build a list of constructor arguments
00054      *
00055      * @param ReflectionClass $reflectedClass
00056      * @return array of parameter infos for constructor
00057      */
00058     private function getConstructorArguments(ReflectionClass $reflectedClass) {
00059         $reflectionMethod = $reflectedClass->getConstructor();
00060         if (!is_object($reflectionMethod)) {
00061             return array();
00062         }
00063         $result = array();
00064         foreach ($reflectionMethod->getParameters() as $reflectionParameter) {
00065             /* @var $reflectionParameter ReflectionParameter */
00066             $info = array();
00067 
00068             $info['name'] = $reflectionParameter->getName();
00069 
00070             if ($reflectionParameter->getClass()) {
00071                 $info['dependency'] = $reflectionParameter->getClass()->getName();
00072             }
00073             if ($reflectionParameter->isOptional()) {
00074                 $info['defaultValue'] = $reflectionParameter->getDefaultValue();
00075             }
00076 
00077             $result[] = $info;
00078         }
00079         return $result;
00080     }
00081 
00082     /**
00083      * Build a list of inject methods for the given class.
00084 
00085      * @param ReflectionClass $reflectedClass
00086      * @return array (nameOfInjectMethod => nameOfClassToBeInjected)
00087      */
00088     private function getInjectMethods(ReflectionClass $reflectedClass) {
00089         $result = array();
00090         $reflectionMethods = $reflectedClass->getMethods();
00091 
00092         if (is_array($reflectionMethods)) {
00093             foreach ($reflectionMethods as $reflectionMethod) {
00094                 if ($reflectionMethod->isPublic()
00095                     && substr($reflectionMethod->getName(), 0, 6) === 'inject'
00096                     && $reflectionMethod->getName() !== 'injectSettings') {
00097 
00098                     $reflectionParameter = $reflectionMethod->getParameters();
00099                     if (isset($reflectionParameter[0])) {
00100                         if (!$reflectionParameter[0]->getClass()) {
00101                             throw new Exception('Method "' . $reflectionMethod->getName(). '" of class "' . $reflectedClass->getName() . '" is marked as setter for Dependency Injection, but does not have a type annotation');
00102                         }
00103                         $result[$reflectionMethod->getName()] = $reflectionParameter[0]->getClass()->getName();
00104                     }
00105                 }
00106             }
00107         }
00108         return $result;
00109     }
00110 }