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