[TASK] Remove leading slash from use statements
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Object / ObjectManager.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Object;
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
17 use TYPO3\CMS\Extbase\Object\Container\Container;
18
19 /**
20 * Implementation of the default Extbase Object Manager
21 */
22 class ObjectManager implements ObjectManagerInterface {
23
24 /**
25 * @var \TYPO3\CMS\Extbase\Object\Container\Container
26 */
27 protected $objectContainer;
28
29 /**
30 * Constructs a new Object Manager
31 */
32 public function __construct() {
33 $this->objectContainer = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Object\Container\Container::class);
34 }
35
36 /**
37 * Serialization (sleep) helper.
38 *
39 * Removes properties of this object from serialization.
40 * This action is necessary, since there might be closures used
41 * in the accordant content objects (e.g. in FLUIDTEMPLATE) which
42 * cannot be serialized. It's fine to reset $this->contentObjects
43 * since elements will be recreated and are just a local cache,
44 * but not required for runtime logic and behaviour.
45 *
46 * @see http://forge.typo3.org/issues/36820
47 * @return array Names of the properties to be serialized
48 */
49 public function __sleep() {
50 // Use get_objects_vars() instead of
51 // a much more expensive Reflection:
52 $properties = get_object_vars($this);
53 unset($properties['objectContainer']);
54 return array_keys($properties);
55 }
56
57 /**
58 * Unserialization (wakeup) helper.
59 *
60 * Initializes the properties again that have been removed by
61 * a call to the __sleep() method on serialization before.
62 *
63 * @see http://forge.typo3.org/issues/36820
64 * @return void
65 */
66 public function __wakeup() {
67 $this->__construct();
68 }
69
70 /**
71 * Returns TRUE if an object with the given name is registered
72 *
73 * @param string $objectName Name of the object
74 * @return bool TRUE if the object has been registered, otherwise FALSE
75 */
76 public function isRegistered($objectName) {
77 return class_exists($objectName, TRUE);
78 }
79
80 /**
81 * Returns a fresh or existing instance of the object specified by $objectName.
82 *
83 * @param string $objectName The name of the object to return an instance of
84 * @return object The object instance
85 * @api
86 */
87 public function get($objectName) {
88 $arguments = func_get_args();
89 array_shift($arguments);
90 if ($objectName === 'DateTime') {
91 array_unshift($arguments, $objectName);
92 $instance = call_user_func_array(array(\TYPO3\CMS\Core\Utility\GeneralUtility::class, 'makeInstance'), $arguments);
93 } else {
94 $instance = $this->objectContainer->getInstance($objectName, $arguments);
95 }
96 return $instance;
97 }
98
99 /**
100 * Returns the scope of the specified object.
101 *
102 * @param string $objectName The object name
103 * @return int One of the Container::SCOPE_ constants
104 * @throws \TYPO3\CMS\Extbase\Object\Container\Exception\UnknownObjectException
105 * @api
106 */
107 public function getScope($objectName) {
108 if (!$this->isRegistered($objectName)) {
109 throw new \TYPO3\CMS\Extbase\Object\Container\Exception\UnknownObjectException('Object "' . $objectName . '" is not registered.', 1265367590);
110 }
111 return $this->objectContainer->isSingleton($objectName) ? Container::SCOPE_SINGLETON : Container::SCOPE_PROTOTYPE;
112 }
113
114 /**
115 * Create an instance of $className without calling its constructor
116 *
117 * @param string $className
118 * @return object
119 * @api
120 */
121 public function getEmptyObject($className) {
122 return $this->objectContainer->getEmptyObject($className);
123 }
124
125 }