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