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, array(), array(), false, false, array());
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?', 1289386765);
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 array();
58  }
59  $result = array();
60  foreach ($reflectionMethod->getParameters() as $reflectionParameter) {
61  /* @var $reflectionParameter \ReflectionParameter */
62  $info = array();
63  $info['name'] = $reflectionParameter->getName();
64  if ($reflectionParameter->getClass()) {
65  $info['dependency'] = $reflectionParameter->getClass()->getName();
66  }
67 
68  try {
69  $info['defaultValue'] = $reflectionParameter->getDefaultValue();
70  } catch (\ReflectionException $e) {
71  }
72 
73  $result[] = $info;
74  }
75  return $result;
76  }
77 
85  private function getInjectMethods(\ReflectionClass $reflectedClass)
86  {
87  $result = array();
88  $reflectionMethods = $reflectedClass->getMethods();
89  if (is_array($reflectionMethods)) {
90  foreach ($reflectionMethods as $reflectionMethod) {
91  if ($reflectionMethod->isPublic() && $this->isNameOfInjectMethod($reflectionMethod->getName())) {
92  $reflectionParameter = $reflectionMethod->getParameters();
93  if (isset($reflectionParameter[0])) {
94  if (!$reflectionParameter[0]->getClass()) {
95  throw new \Exception('Method "' . $reflectionMethod->getName() . '" of class "' . $reflectedClass->getName() . '" is marked as setter for Dependency Injection, but does not have a type annotation');
96  }
97  $result[$reflectionMethod->getName()] = $reflectionParameter[0]->getClass()->getName();
98  }
99  }
100  }
101  }
102  return $result;
103  }
104 
111  private function getInjectProperties(\ReflectionClass $reflectedClass)
112  {
113  $result = array();
114  $reflectionProperties = $reflectedClass->getProperties();
115  if (is_array($reflectionProperties)) {
116  foreach ($reflectionProperties as $reflectionProperty) {
117  $reflectedProperty = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Reflection\PropertyReflection::class, $reflectedClass->getName(), $reflectionProperty->getName());
118  if ($reflectedProperty->isTaggedWith('inject') && $reflectedProperty->getName() !== 'settings') {
119  $varValues = $reflectedProperty->getTagValues('var');
120  if (count($varValues) === 1) {
121  $result[$reflectedProperty->getName()] = ltrim($varValues[0], '\\');
122  }
123  }
124  }
125  }
126  return $result;
127  }
128 
135  private function isNameOfInjectMethod($methodName)
136  {
137  if (
138  substr($methodName, 0, 6) === 'inject'
139  && $methodName[6] === strtoupper($methodName[6])
140  && $methodName !== 'injectSettings'
141  ) {
142  return true;
143  }
144  return false;
145  }
146 
153  private function getIsSingleton($classname)
154  {
155  return in_array(\TYPO3\CMS\Core\SingletonInterface::class, class_implements($classname));
156  }
157 
165  private function getIsInitializeable($classname)
166  {
167  return method_exists($classname, 'initializeObject');
168  }
169 }