TYPO3 CMS  TYPO3_6-2
ClassInfoFactory.php
Go to the documentation of this file.
1 <?php
3 
22 
30  public function buildClassInfoFromClassName($className) {
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  $reflectionMethod = $reflectedClass->getConstructor();
55  if (!is_object($reflectionMethod)) {
56  return array();
57  }
58  $result = array();
59  foreach ($reflectionMethod->getParameters() as $reflectionParameter) {
60  /* @var $reflectionParameter \ReflectionParameter */
61  $info = array();
62  $info['name'] = $reflectionParameter->getName();
63  if ($reflectionParameter->getClass()) {
64  $info['dependency'] = $reflectionParameter->getClass()->getName();
65  }
66 
67  try {
68  $info['defaultValue'] = $reflectionParameter->getDefaultValue();
69  } catch (\ReflectionException $e) {}
70 
71  $result[] = $info;
72  }
73  return $result;
74  }
75 
83  private function getInjectMethods(\ReflectionClass $reflectedClass) {
84  $result = array();
85  $reflectionMethods = $reflectedClass->getMethods();
86  if (is_array($reflectionMethods)) {
87  foreach ($reflectionMethods as $reflectionMethod) {
88  if ($reflectionMethod->isPublic() && $this->isNameOfInjectMethod($reflectionMethod->getName())) {
89  $reflectionParameter = $reflectionMethod->getParameters();
90  if (isset($reflectionParameter[0])) {
91  if (!$reflectionParameter[0]->getClass()) {
92  throw new \Exception('Method "' . $reflectionMethod->getName() . '" of class "' . $reflectedClass->getName() . '" is marked as setter for Dependency Injection, but does not have a type annotation');
93  }
94  $result[$reflectionMethod->getName()] = $reflectionParameter[0]->getClass()->getName();
95  }
96  }
97  }
98  }
99  return $result;
100  }
101 
108  private function getInjectProperties(\ReflectionClass $reflectedClass) {
109  $result = array();
110  $reflectionProperties = $reflectedClass->getProperties();
111  if (is_array($reflectionProperties)) {
112  foreach ($reflectionProperties as $reflectionProperty) {
113  $reflectedProperty = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Reflection\\PropertyReflection', $reflectedClass->getName(), $reflectionProperty->getName());
114  if ($reflectedProperty->isTaggedWith('inject') && $reflectedProperty->getName() !== 'settings') {
115  $varValues = $reflectedProperty->getTagValues('var');
116  if (count($varValues) == 1) {
117  $result[$reflectedProperty->getName()] = ltrim($varValues[0], '\\');
118  }
119  }
120  }
121  }
122  return $result;
123  }
124 
131  private function isNameOfInjectMethod($methodName) {
132  if (
133  substr($methodName, 0, 6) === 'inject'
134  && $methodName[6] === strtoupper($methodName[6])
135  && $methodName !== 'injectSettings'
136  ) {
137  return TRUE;
138  }
139  return FALSE;
140  }
141 
148  private function getIsSingleton($classname) {
149  return in_array('TYPO3\\CMS\\Core\\SingletonInterface', class_implements($classname));
150  }
151 
159  private function getIsInitializeable($classname) {
160  return method_exists($classname, 'initializeObject');
161  }
162 }
getConstructorArguments(\ReflectionClass $reflectedClass)
getInjectMethods(\ReflectionClass $reflectedClass)
if($list_of_literals) if(!empty($literals)) if(!empty($literals)) $result
Analyse literals to prepend the N char to them if their contents aren&#39;t numeric.
getInjectProperties(\ReflectionClass $reflectedClass)