TYPO3 CMS  TYPO3_8-7
ObjectManager.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 
18 
23 {
27  protected $objectContainer;
28 
32  public function __construct()
33  {
34  $this->objectContainer = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Object\Container\Container::class);
35  }
36 
50  public function __sleep()
51  {
52  // Use get_objects_vars() instead of
53  // a much more expensive Reflection:
54  $properties = get_object_vars($this);
55  unset($properties['objectContainer']);
56  return array_keys($properties);
57  }
58 
67  public function __wakeup()
68  {
69  $this->__construct();
70  }
71 
78  public function isRegistered($objectName)
79  {
80  return class_exists($objectName, true);
81  }
82 
90  public function get($objectName)
91  {
92  $arguments = func_get_args();
93  array_shift($arguments);
94  if ($objectName === 'DateTime') {
95  array_unshift($arguments, $objectName);
96  $instance = call_user_func_array([\TYPO3\CMS\Core\Utility\GeneralUtility::class, 'makeInstance'], $arguments);
97  } else {
98  $instance = $this->objectContainer->getInstance($objectName, $arguments);
99  }
100  return $instance;
101  }
102 
111  public function getScope($objectName)
112  {
113  if (!$this->isRegistered($objectName)) {
114  throw new \TYPO3\CMS\Extbase\Object\Container\Exception\UnknownObjectException('Object "' . $objectName . '" is not registered.', 1265367590);
115  }
116  return $this->objectContainer->isSingleton($objectName) ? Container::SCOPE_SINGLETON : Container::SCOPE_PROTOTYPE;
117  }
118 
126  public function getEmptyObject($className)
127  {
128  return $this->objectContainer->getEmptyObject($className);
129  }
130 }
static makeInstance($className,... $constructorArguments)