‪TYPO3CMS  10.4
ObjectManager.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use Psr\Container\ContainerInterface;
23 
28 {
32  private ‪$container;
33 
37  protected ‪$objectContainer;
38 
45  public function ‪__construct(ContainerInterface ‪$container, ExtbaseContainer ‪$objectContainer)
46  {
47  $this->container = ‪$container;
48  $this->objectContainer = ‪$objectContainer;
49  }
50 
65  public function ‪__sleep(): array
66  {
67  return [];
68  }
69 
79  public function ‪__wakeup()
80  {
81  $this->‪__construct(
82  GeneralUtility::getContainer(),
83  GeneralUtility::getContainer()->get(ExtbaseContainer::class)
84  );
85  }
86 
99  public function get(string $className, ...$constructorArguments): object
100  {
101  // todo: This method needs to trigger a deprecation error as soon as the core does not use this method any more.
102 
103  if ($className === \DateTime::class) {
104  return GeneralUtility::makeInstance($className, ...$constructorArguments);
105  }
106 
107  if ($this->container->has($className)) {
108  if ($constructorArguments === []) {
109  $instance = $this->container->get($className);
110  if (!is_object($instance)) {
111  throw new ‪Exception('Invalid object name "' . $className . '". The PSR-11 container entry resolves to a non object.', 1562357346);
112  }
113  return $instance;
114  }
115  trigger_error($className . ' is available in the PSR-11 container. That means you should not try to instanciate it using constructor arguments. Falling back to legacy extbase based injection.', E_USER_DEPRECATED);
116  }
117 
118  return $this->objectContainer->getInstance($className, $constructorArguments);
119  }
120 
130  public function ‪getEmptyObject(string $className): object
131  {
132  return $this->objectContainer->getEmptyObject($className);
133  }
134 }
‪TYPO3\CMS\Extbase\Object\ObjectManager\__construct
‪__construct(ContainerInterface $container, ExtbaseContainer $objectContainer)
Definition: ObjectManager.php:43
‪TYPO3\CMS\Extbase\Object\ObjectManager\$container
‪ContainerInterface $container
Definition: ObjectManager.php:31
‪TYPO3\CMS\Extbase\Object\Container\Container
Definition: Container.php:39
‪TYPO3\CMS\Extbase\Object\ObjectManager\__sleep
‪array __sleep()
Definition: ObjectManager.php:63
‪TYPO3\CMS\Extbase\Object\ObjectManager\$objectContainer
‪ExtbaseContainer $objectContainer
Definition: ObjectManager.php:35
‪TYPO3\CMS\Extbase\Object\ObjectManagerInterface
Definition: ObjectManagerInterface.php:26
‪TYPO3\CMS\Extbase\Object\ObjectManager\getEmptyObject
‪object &T getEmptyObject(string $className)
Definition: ObjectManager.php:128
‪TYPO3\CMS\Extbase\Object\ObjectManager\__wakeup
‪__wakeup()
Definition: ObjectManager.php:77
‪TYPO3\CMS\Extbase\Object\Exception
Definition: Exception.php:26
‪TYPO3\CMS\Extbase\Object
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Extbase\Object\ObjectManager
Definition: ObjectManager.php:28