83 if ($this->classInfoFactory == null) {
84 $this->classInfoFactory = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Object\
Container\ClassInfoFactory::class);
96 if ($this->cache == null) {
97 $this->cache = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Object\
Container\ClassInfoCache::class);
109 if ($this->instantiator == null) {
110 $this->instantiator = new \Doctrine\Instantiator\Instantiator();
123 public function getInstance($className, $givenConstructorArguments = array())
125 $this->prototypeObjectsWhichAreCurrentlyInstanciated = array();
157 if ($className === \TYPO3\CMS\Extbase\Object\
Container\Container::class) {
160 if ($className === \TYPO3\CMS\Core\Cache\CacheManager::class) {
161 return \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Cache\CacheManager::class);
163 if ($className === \TYPO3\CMS\Core\Package\PackageManager::class) {
164 return \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Package\PackageManager::class);
166 $className = \TYPO3\CMS\Core\Core\ClassLoadingInformation::getClassNameForAlias($className);
167 if (isset($this->singletonInstances[$className])) {
168 if (!empty($givenConstructorArguments)) {
169 throw new \TYPO3\CMS\Extbase\Object\Exception(
'Object "' . $className .
'" fetched from singleton cache, thus, explicit constructor arguments are not allowed.', 1292857934);
171 return $this->singletonInstances[$className];
174 $classIsSingleton = $classInfo->getIsSingleton();
175 if (!$classIsSingleton) {
176 if (array_key_exists($className, $this->prototypeObjectsWhichAreCurrentlyInstanciated) !==
false) {
177 throw new \TYPO3\CMS\Extbase\Object\Exception\CannotBuildObjectException(
'Cyclic dependency in prototype object, for class "' . $className .
'".', 1295611406);
179 $this->prototypeObjectsWhichAreCurrentlyInstanciated[$className] =
true;
184 if (!$classIsSingleton) {
185 unset($this->prototypeObjectsWhichAreCurrentlyInstanciated[$className]);
202 $className = $classInfo->getClassName();
203 $classIsSingleton = $classInfo->getIsSingleton();
204 if ($classIsSingleton && !empty($givenConstructorArguments)) {
205 throw new \TYPO3\CMS\Extbase\Object\Exception(
'Object "' . $className .
'" has explicit constructor arguments but is a singleton; this is not allowed.', 1292858051);
208 array_unshift($constructorArguments, $className);
209 $instance = call_user_func_array(array(\TYPO3\CMS\Core\Utility\GeneralUtility::class,
'makeInstance'), $constructorArguments);
210 if ($classIsSingleton) {
211 $this->singletonInstances[$className] = $instance;
225 if (!$classInfo->hasInjectMethods() && !$classInfo->hasInjectProperties()) {
228 foreach ($classInfo->getInjectMethods() as $injectMethodName => $classNameToInject) {
230 if ($classInfo->getIsSingleton() && !$instanceToInject instanceof \TYPO3\CMS\Core\SingletonInterface) {
231 $this->
log(
'The singleton "' . $classInfo->getClassName() .
'" needs a prototype in "' . $injectMethodName .
'". This is often a bad code smell; often you rather want to inject a singleton.', 1);
233 if (is_callable(array($instance, $injectMethodName))) {
234 $instance->{$injectMethodName}($instanceToInject);
237 foreach ($classInfo->getInjectProperties() as $injectPropertyName => $classNameToInject) {
239 if ($classInfo->getIsSingleton() && !$instanceToInject instanceof \TYPO3\CMS\Core\SingletonInterface) {
240 $this->
log(
'The singleton "' . $classInfo->getClassName() .
'" needs a prototype in "' . $injectPropertyName .
'". This is often a bad code smell; often you rather want to inject a singleton.', 1);
242 $propertyReflection = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Reflection\PropertyReflection::class, $instance, $injectPropertyName);
244 $propertyReflection->setAccessible(
true);
245 $propertyReflection->setValue($instance, $instanceToInject);
257 if ($classInfo->getIsInitializeable() && is_callable(array($instance,
'initializeObject'))) {
258 $instance->initializeObject();
269 protected function log($message, $severity)
271 \TYPO3\CMS\Core\Utility\GeneralUtility::devLog($message,
'extbase', $severity);
283 $this->alternativeImplementation[$className] = $alternativeClassName;
297 $parameters = array();
298 $constructorArgumentInformation = $classInfo->getConstructorArguments();
299 foreach ($constructorArgumentInformation as $index => $argumentInformation) {
301 if (array_key_exists($index, $givenConstructorArguments) && (!isset($argumentInformation[
'dependency']) || is_a($givenConstructorArguments[$index], $argumentInformation[
'dependency']))) {
302 $parameter = $givenConstructorArguments[$index];
304 if (isset($argumentInformation[
'dependency']) && !array_key_exists(
'defaultValue', $argumentInformation)) {
306 if ($classInfo->getIsSingleton() && !$parameter instanceof \TYPO3\CMS\Core\SingletonInterface) {
307 $this->
log(
'The singleton "' . $className .
'" needs a prototype in the constructor. This is often a bad code smell; often you rather want to inject a singleton.', 1);
309 }
elseif (array_key_exists(
'defaultValue', $argumentInformation)) {
310 $parameter = $argumentInformation[
'defaultValue'];
312 throw new \InvalidArgumentException(
'not a correct info array of constructor dependencies was passed!');
315 $parameters[] = $parameter;
329 if (isset($this->alternativeImplementation[$className])) {
330 $className = $this->alternativeImplementation[$className];
332 if (substr($className, -9) ===
'Interface') {
333 $className = substr($className, 0, -9);
346 $classNameHash = md5($className);
362 return $this->
getClassInfo($className)->getIsSingleton();