[CLEANUP] Replace wrong/old file copyright comments
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Object / Container / ClassInfoFactory.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Object\Container;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2010-2012 Extbase Team (http://forge.typo3.org/projects/typo3v4-mvc)
8 * Extbase is a backport of TYPO3 Flow. All credits go to the TYPO3 Flow team.
9 * All rights reserved
10 *
11 * This script is part of the TYPO3 project. The TYPO3 project is
12 * free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * The GNU General Public License can be found at
18 * http://www.gnu.org/copyleft/gpl.html.
19 * A copy is found in the textfile GPL.txt and important notices to the license
20 * from the author is found in LICENSE.txt distributed with these scripts.
21 *
22 *
23 * This script is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
27 *
28 * This copyright notice MUST APPEAR in all copies of the script!
29 ***************************************************************/
30 /**
31 * TYPO3 Dependency Injection container
32 *
33 * @author Daniel Pötzinger
34 */
35 class ClassInfoFactory {
36
37 /**
38 * Factory metod that builds a ClassInfo Object for the given classname - using reflection
39 *
40 * @param string $className The class name to build the class info for
41 * @throws Exception\UnknownObjectException
42 * @return \TYPO3\CMS\Extbase\Object\Container\ClassInfo the class info
43 */
44 public function buildClassInfoFromClassName($className) {
45 try {
46 $reflectedClass = new \ReflectionClass($className);
47 } catch (\Exception $e) {
48 throw new \TYPO3\CMS\Extbase\Object\Container\Exception\UnknownObjectException('Could not analyse class:' . $className . ' maybe not loaded or no autoloader?', 1289386765);
49 }
50 $constructorArguments = $this->getConstructorArguments($reflectedClass);
51 $injectMethods = $this->getInjectMethods($reflectedClass);
52 $injectProperties = $this->getInjectProperties($reflectedClass);
53 $isSingleton = $this->getIsSingleton($className);
54 $isInitializeable = $this->getIsInitializeable($className);
55 return new \TYPO3\CMS\Extbase\Object\Container\ClassInfo($className, $constructorArguments, $injectMethods, $isSingleton, $isInitializeable, $injectProperties);
56 }
57
58 /**
59 * Build a list of constructor arguments
60 *
61 * @param \ReflectionClass $reflectedClass
62 * @return array of parameter infos for constructor
63 */
64 private function getConstructorArguments(\ReflectionClass $reflectedClass) {
65 $reflectionMethod = $reflectedClass->getConstructor();
66 if (!is_object($reflectionMethod)) {
67 return array();
68 }
69 $result = array();
70 foreach ($reflectionMethod->getParameters() as $reflectionParameter) {
71 /* @var $reflectionParameter \ReflectionParameter */
72 $info = array();
73 $info['name'] = $reflectionParameter->getName();
74 if ($reflectionParameter->getClass()) {
75 $info['dependency'] = $reflectionParameter->getClass()->getName();
76 }
77 if ($reflectionParameter->isOptional()) {
78 $info['defaultValue'] = $reflectionParameter->getDefaultValue();
79 }
80 $result[] = $info;
81 }
82 return $result;
83 }
84
85 /**
86 * Build a list of inject methods for the given class.
87 *
88 * @param \ReflectionClass $reflectedClass
89 * @throws \Exception
90 * @return array (nameOfInjectMethod => nameOfClassToBeInjected)
91 */
92 private function getInjectMethods(\ReflectionClass $reflectedClass) {
93 $result = array();
94 $reflectionMethods = $reflectedClass->getMethods();
95 if (is_array($reflectionMethods)) {
96 foreach ($reflectionMethods as $reflectionMethod) {
97 if ($reflectionMethod->isPublic() && substr($reflectionMethod->getName(), 0, 6) === 'inject' && $reflectionMethod->getName() !== 'injectSettings') {
98 $reflectionParameter = $reflectionMethod->getParameters();
99 if (isset($reflectionParameter[0])) {
100 if (!$reflectionParameter[0]->getClass()) {
101 throw new \Exception('Method "' . $reflectionMethod->getName() . '" of class "' . $reflectedClass->getName() . '" is marked as setter for Dependency Injection, but does not have a type annotation');
102 }
103 $result[$reflectionMethod->getName()] = $reflectionParameter[0]->getClass()->getName();
104 }
105 }
106 }
107 }
108 return $result;
109 }
110
111 /**
112 * Build a list of properties to be injected for the given class.
113 *
114 * @param \ReflectionClass $reflectedClass
115 * @return array (nameOfInjectProperty => nameOfClassToBeInjected)
116 */
117 private function getInjectProperties(\ReflectionClass $reflectedClass) {
118 $result = array();
119 $reflectionProperties = $reflectedClass->getProperties();
120 if (is_array($reflectionProperties)) {
121 foreach ($reflectionProperties as $reflectionProperty) {
122 $reflectedProperty = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Reflection\\PropertyReflection', $reflectedClass->getName(), $reflectionProperty->getName());
123 if ($reflectedProperty->isTaggedWith('inject') && $reflectedProperty->getName() !== 'settings') {
124 $varValues = $reflectedProperty->getTagValues('var');
125 if (count($varValues) == 1) {
126 $result[$reflectedProperty->getName()] = ltrim($varValues[0], '\\');
127 }
128 }
129 }
130 }
131 return $result;
132 }
133
134 /**
135 * This method is used to determin if a class is a singleton or not.
136 *
137 * @param string $classname
138 * @return boolean
139 */
140 private function getIsSingleton($classname) {
141 return in_array('TYPO3\\CMS\\Core\\SingletonInterface', class_implements($classname));
142 }
143
144 /**
145 * This method is used to determine of the object is initializeable with the
146 * method initializeObject.
147 *
148 * @param string $classname
149 * @return boolean
150 */
151 private function getIsInitializeable($classname) {
152 return method_exists($classname, 'initializeObject');
153 }
154 }
155
156 ?>