TYPO3 CMS  TYPO3_7-6
ClassInfoFactory.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
21 {
29  public function buildClassInfoFromClassName($className)
30  {
31  if ($className === 'DateTime') {
32  return new \TYPO3\CMS\Extbase\Object\Container\ClassInfo($className, [], [], false, false, []);
33  }
34  try {
35  $reflectedClass = new \ReflectionClass($className);
36  } catch (\Exception $e) {
37  throw new \TYPO3\CMS\Extbase\Object\Container\Exception\UnknownObjectException('Could not analyse class: "' . $className . '" maybe not loaded or no autoloader? ' . $e->getMessage(), 1289386765, $e);
38  }
39  $constructorArguments = $this->getConstructorArguments($reflectedClass);
40  $injectMethods = $this->getInjectMethods($reflectedClass);
41  $injectProperties = $this->getInjectProperties($reflectedClass);
42  $isSingleton = $this->getIsSingleton($className);
43  $isInitializeable = $this->getIsInitializeable($className);
44  return new \TYPO3\CMS\Extbase\Object\Container\ClassInfo($className, $constructorArguments, $injectMethods, $isSingleton, $isInitializeable, $injectProperties);
45  }
46 
53  private function getConstructorArguments(\ReflectionClass $reflectedClass)
54  {
55  $reflectionMethod = $reflectedClass->getConstructor();
56  if (!is_object($reflectionMethod)) {
57  return [];
58  }
59  $result = [];
60  foreach ($reflectionMethod->getParameters() as $reflectionParameter) {
61  /* @var $reflectionParameter \ReflectionParameter */
62  $info = [];
63  $info['name'] = $reflectionParameter->getName();
64  if ($reflectionParameter->getClass()) {
65  $info['dependency'] = $reflectionParameter->getClass()->getName();
66  }
67 
68  if ($reflectionParameter->isDefaultValueAvailable()) {
69  $info['defaultValue'] = $reflectionParameter->getDefaultValue();
70  }
71 
72  $result[] = $info;
73  }
74  return $result;
75  }
76 
84  private function getInjectMethods(\ReflectionClass $reflectedClass)
85  {
86  $result = [];
87  $reflectionMethods = $reflectedClass->getMethods();
88  if (is_array($reflectionMethods)) {
89  foreach ($reflectionMethods as $reflectionMethod) {
90  if ($reflectionMethod->isPublic() && $this->isNameOfInjectMethod($reflectionMethod->getName())) {
91  $reflectionParameter = $reflectionMethod->getParameters();
92  if (isset($reflectionParameter[0])) {
93  if (!$reflectionParameter[0]->getClass()) {
94  throw new \Exception('Method "' . $reflectionMethod->getName() . '" of class "' . $reflectedClass->getName() . '" is marked as setter for Dependency Injection, but does not have a type annotation');
95  }
96  $result[$reflectionMethod->getName()] = $reflectionParameter[0]->getClass()->getName();
97  }
98  }
99  }
100  }
101  return $result;
102  }
103 
110  private function getInjectProperties(\ReflectionClass $reflectedClass)
111  {
112  $result = [];
113  $reflectionProperties = $reflectedClass->getProperties();
114  if (is_array($reflectionProperties)) {
115  foreach ($reflectionProperties as $reflectionProperty) {
116  $reflectedProperty = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Reflection\PropertyReflection::class, $reflectedClass->getName(), $reflectionProperty->getName());
117  if ($reflectedProperty->isTaggedWith('inject') && $reflectedProperty->getName() !== 'settings') {
118  $varValues = $reflectedProperty->getTagValues('var');
119  if (count($varValues) === 1) {
120  $result[$reflectedProperty->getName()] = ltrim($varValues[0], '\\');
121  }
122  }
123  }
124  }
125  return $result;
126  }
127 
134  private function isNameOfInjectMethod($methodName)
135  {
136  if (
137  substr($methodName, 0, 6) === 'inject'
138  && $methodName[6] === strtoupper($methodName[6])
139  && $methodName !== 'injectSettings'
140  ) {
141  return true;
142  }
143  return false;
144  }
145 
152  private function getIsSingleton($classname)
153  {
154  return in_array(\TYPO3\CMS\Core\SingletonInterface::class, class_implements($classname));
155  }
156 
164  private function getIsInitializeable($classname)
165  {
166  return method_exists($classname, 'initializeObject');
167  }
168 }
getConstructorArguments(\ReflectionClass $reflectedClass)
getInjectMethods(\ReflectionClass $reflectedClass)
getInjectProperties(\ReflectionClass $reflectedClass)