2dbc430809f3e5a64631d3f0d1ca7720634f82f5
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Object / ObjectManager.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Object;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2010-2013 Extbase Team (http://forge.typo3.org/projects/typo3v4-mvc)
8 * Extbase is a backport of TYPO3 Flow. All credits go to the TYPO3 Flow team.
9 * All rights reserved
10 *
11 * This script is part of the TYPO3 project. The TYPO3 project is
12 * free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * The GNU General Public License can be found at
18 * http://www.gnu.org/copyleft/gpl.html.
19 * A copy is found in the textfile GPL.txt and important notices to the license
20 * from the author is found in LICENSE.txt distributed with these scripts.
21 *
22 *
23 * This script is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
27 *
28 * This copyright notice MUST APPEAR in all copies of the script!
29 ***************************************************************/
30 /**
31 * Implementation of the default Extbase Object Manager
32 */
33 class ObjectManager implements ObjectManagerInterface {
34
35 /**
36 * @var \TYPO3\CMS\Extbase\Object\Container\Container
37 */
38 protected $objectContainer;
39
40 /**
41 * Constructs a new Object Manager
42 */
43 public function __construct() {
44 $this->objectContainer = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\Container\\Container');
45 }
46
47 /**
48 * Serialization (sleep) helper.
49 *
50 * Removes properties of this object from serialization.
51 * This action is necessary, since there might be closures used
52 * in the accordant content objects (e.g. in FLUIDTEMPLATE) which
53 * cannot be serialized. It's fine to reset $this->contentObjects
54 * since elements will be recreated and are just a local cache,
55 * but not required for runtime logic and behaviour.
56 *
57 * @see http://forge.typo3.org/issues/36820
58 * @return array Names of the properties to be serialized
59 */
60 public function __sleep() {
61 // Use get_objects_vars() instead of
62 // a much more expensive Reflection:
63 $properties = get_object_vars($this);
64 unset($properties['objectContainer']);
65 return array_keys($properties);
66 }
67
68 /**
69 * Unserialization (wakeup) helper.
70 *
71 * Initializes the properties again that have been removed by
72 * a call to the __sleep() method on serialization before.
73 *
74 * @see http://forge.typo3.org/issues/36820
75 * @return void
76 */
77 public function __wakeup() {
78 $this->__construct();
79 }
80
81 /**
82 * Returns TRUE if an object with the given name is registered
83 *
84 * @param string $objectName Name of the object
85 * @return boolean TRUE if the object has been registered, otherwise FALSE
86 */
87 public function isRegistered($objectName) {
88 return class_exists($objectName, TRUE);
89 }
90
91 /**
92 * Returns a fresh or existing instance of the object specified by $objectName.
93 *
94 * @param string $objectName The name of the object to return an instance of
95 * @return object The object instance
96 * @api
97 */
98 public function get($objectName) {
99 $arguments = func_get_args();
100 array_shift($arguments);
101 if ($objectName === 'DateTime') {
102 array_unshift($arguments, $objectName);
103 $instance = call_user_func_array(array('TYPO3\\CMS\\Core\\Utility\\GeneralUtility', 'makeInstance'), $arguments);
104 } else {
105 $instance = $this->objectContainer->getInstance($objectName, $arguments);
106 }
107 return $instance;
108 }
109
110 /**
111 * Creates a fresh instance of the object specified by $objectName.
112 *
113 * This factory method can only create objects of the scope prototype.
114 * Singleton objects must be either injected by some type of Dependency Injection or
115 * if that is not possible, be retrieved by the get() method of the
116 * Object Manager
117 *
118 * @param string $objectName The name of the object to create
119 * @return object The new object instance
120 * @deprecated since Extbase 6.1.0; will be removed in Extbase 6.3.0
121 */
122 public function create($objectName) {
123 \TYPO3\CMS\Core\Utility\GeneralUtility::logDeprecatedFunction();
124 $arguments = func_get_args();
125 $instance = call_user_func_array(array($this, 'get'), $arguments);
126 return $instance;
127 }
128
129 /**
130 * Create an instance of $className without calling its constructor
131 *
132 * @param string $className
133 * @return object
134 * @api
135 */
136 public function getEmptyObject($className) {
137 return $this->objectContainer->getEmptyObject($className);
138 }
139 }
140
141 ?>