TYPO3 CMS  TYPO3_8-7
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(
95  'Method "' . $reflectionMethod->getName() . '" of class "' . $reflectedClass->getName() . '" is marked as setter for Dependency Injection, but does not have a type annotation',
96  1476108030
97  );
98  }
99  $result[$reflectionMethod->getName()] = $reflectionParameter[0]->getClass()->getName();
100  }
101  }
102  }
103  }
104  return $result;
105  }
106 
113  private function getInjectProperties(\ReflectionClass $reflectedClass)
114  {
115  $result = [];
116  $reflectionProperties = $reflectedClass->getProperties();
117  if (is_array($reflectionProperties)) {
118  foreach ($reflectionProperties as $reflectionProperty) {
119  $reflectedProperty = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Reflection\PropertyReflection::class, $reflectedClass->getName(), $reflectionProperty->getName());
120  if ($reflectedProperty->isTaggedWith('inject') && $reflectedProperty->getName() !== 'settings') {
121  $varValues = $reflectedProperty->getTagValues('var');
122  if (count($varValues) === 1) {
123  $result[$reflectedProperty->getName()] = ltrim($varValues[0], '\\');
124  }
125  }
126  }
127  }
128  return $result;
129  }
130 
137  private function isNameOfInjectMethod($methodName)
138  {
139  if (
140  substr($methodName, 0, 6) === 'inject'
141  && $methodName[6] === strtoupper($methodName[6])
142  && $methodName !== 'injectSettings'
143  ) {
144  return true;
145  }
146  return false;
147  }
148 
155  private function getIsSingleton($classname)
156  {
157  return in_array(\TYPO3\CMS\Core\SingletonInterface::class, class_implements($classname));
158  }
159 
167  private function getIsInitializeable($classname)
168  {
169  return method_exists($classname, 'initializeObject');
170  }
171 }
getConstructorArguments(\ReflectionClass $reflectedClass)
static makeInstance($className,... $constructorArguments)
getInjectMethods(\ReflectionClass $reflectedClass)
getInjectProperties(\ReflectionClass $reflectedClass)