4fe5a3600b76b4125f0ca8449a6dc3f6b3539371
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Object / ObjectManagerException.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 /**
18 * Implementation of the default Extbase Object Manager
19 */
20 class ObjectManagerException implements \TYPO3\CMS\Extbase\Object\ObjectManagerInterfaceException
21 {
22 /**
23 * @var \TYPO3\CMS\Extbase\Object\Container\Container
24 */
25 protected $objectContainer;
26
27 /**
28 * Constructs a new Object Manager
29 */
30 public function __construct()
31 {
32 $this->objectContainer = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Object\Container\Container::class);
33 }
34
35 /**
36 * Serialization (sleep) helper.
37 *
38 * Removes properties of this object from serialization.
39 * This action is necessary, since there might be closures used
40 * in the accordant content objects (e.g. in FLUIDTEMPLATE) which
41 * cannot be serialized. It's fine to reset $this->contentObjects
42 * since elements will be recreated and are just a local cache,
43 * but not required for runtime logic and behaviour.
44 *
45 * @see http://forge.typo3.org/issues/36820
46 * @return array Names of the properties to be serialized
47 */
48 public function __sleep()
49 {
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 */
65 public function __wakeup()
66 {
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 {
78 return class_exists($objectName, true);
79 }
80
81 /**
82 * Returns a fresh or existing instance of the object specified by $objectName.
83 *
84 * Important:
85 *
86 * If possible, instances of Prototype objects should always be created with the
87 * Object Manager's create() method and Singleton objects should rather be
88 * injected by some type of Dependency Injection.
89 *
90 * @param string $objectName The name of the object to return an instance of
91 * @return object The object instance
92 * @api
93 */
94 public function get($objectName)
95 {
96 $arguments = func_get_args();
97 array_shift($arguments);
98 return $this->objectContainer->getInstance($objectName, $arguments);
99 }
100
101 /**
102 * Creates a fresh instance of the object specified by $objectName.
103 *
104 * This factory method can only create objects of the scope prototype.
105 * Singleton objects must be either injected by some type of Dependency Injection or
106 * if that is not possible, be retrieved by the get() method of the
107 * Object Manager
108 *
109 * @param string $objectName The name of the object to create
110 * @throws Exception\WrongScopeException
111 * @return object The new object instance
112 * @api
113 */
114 public function create($objectName)
115 {
116 $arguments = func_get_args();
117 array_shift($arguments);
118 if ($objectName === 'DateTime') {
119 array_unshift($arguments, $objectName);
120 $instance = call_user_func_array([\TYPO3\CMS\Core\Utility\GeneralUtility::class, 'makeInstance'], $arguments);
121 } else {
122 $instance = $this->objectContainer->getInstance($objectName, $arguments);
123 }
124 if ($instance instanceof \TYPO3\CMS\Core\SingletonInterface) {
125 throw new \TYPO3\CMS\Extbase\Object\Exception\WrongScopeException('Object "' . $objectName . '" is of not of scope prototype, but only prototype is supported by create()', 1265203124);
126 }
127 return $instance;
128 }
129
130 /**
131 * Create an instance of $className without calling its constructor
132 *
133 * @param string $className
134 * @return object
135 * @api
136 */
137 public function getEmptyObject($className)
138 {
139 return $this->objectContainer->getEmptyObject($className);
140 }
141 }