[BUGFIX] Omit constructor injection for optional parameters
[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 use \TYPO3\CMS\Extbase\Object\Container\Container;
32
33 /**
34 * Implementation of the default Extbase Object Manager
35 */
36 class ObjectManager implements ObjectManagerInterface {
37
38 /**
39 * @var \TYPO3\CMS\Extbase\Object\Container\Container
40 */
41 protected $objectContainer;
42
43 /**
44 * Constructs a new Object Manager
45 */
46 public function __construct() {
47 $this->objectContainer = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\Container\\Container');
48 }
49
50 /**
51 * Serialization (sleep) helper.
52 *
53 * Removes properties of this object from serialization.
54 * This action is necessary, since there might be closures used
55 * in the accordant content objects (e.g. in FLUIDTEMPLATE) which
56 * cannot be serialized. It's fine to reset $this->contentObjects
57 * since elements will be recreated and are just a local cache,
58 * but not required for runtime logic and behaviour.
59 *
60 * @see http://forge.typo3.org/issues/36820
61 * @return array Names of the properties to be serialized
62 */
63 public function __sleep() {
64 // Use get_objects_vars() instead of
65 // a much more expensive Reflection:
66 $properties = get_object_vars($this);
67 unset($properties['objectContainer']);
68 return array_keys($properties);
69 }
70
71 /**
72 * Unserialization (wakeup) helper.
73 *
74 * Initializes the properties again that have been removed by
75 * a call to the __sleep() method on serialization before.
76 *
77 * @see http://forge.typo3.org/issues/36820
78 * @return void
79 */
80 public function __wakeup() {
81 $this->__construct();
82 }
83
84 /**
85 * Returns TRUE if an object with the given name is registered
86 *
87 * @param string $objectName Name of the object
88 * @return boolean TRUE if the object has been registered, otherwise FALSE
89 */
90 public function isRegistered($objectName) {
91 return class_exists($objectName, TRUE);
92 }
93
94 /**
95 * Returns a fresh or existing instance of the object specified by $objectName.
96 *
97 * @param string $objectName The name of the object to return an instance of
98 * @return object The object instance
99 * @api
100 */
101 public function get($objectName) {
102 $arguments = func_get_args();
103 array_shift($arguments);
104 if ($objectName === 'DateTime') {
105 array_unshift($arguments, $objectName);
106 $instance = call_user_func_array(array('TYPO3\\CMS\\Core\\Utility\\GeneralUtility', 'makeInstance'), $arguments);
107 } else {
108 $instance = $this->objectContainer->getInstance($objectName, $arguments);
109 }
110 return $instance;
111 }
112
113 /**
114 * Returns the scope of the specified object.
115 *
116 * @param string $objectName The object name
117 * @return integer One of the Container::SCOPE_ constants
118 * @throws \TYPO3\CMS\Extbase\Object\Container\Exception\UnknownObjectException
119 * @api
120 */
121 public function getScope($objectName) {
122 if (!$this->isRegistered($objectName)) {
123 throw new \TYPO3\CMS\Extbase\Object\Container\Exception\UnknownObjectException('Object "' . $objectName . '" is not registered.', 1265367590);
124 }
125 return $this->objectContainer->isSingleton($objectName) ? Container::SCOPE_SINGLETON : Container::SCOPE_PROTOTYPE;
126 }
127
128 /**
129 * Creates a fresh instance of the object specified by $objectName.
130 *
131 * This factory method can only create objects of the scope prototype.
132 * Singleton objects must be either injected by some type of Dependency Injection or
133 * if that is not possible, be retrieved by the get() method of the
134 * Object Manager
135 *
136 * @param string $objectName The name of the object to create
137 * @return object The new object instance
138 * @deprecated since Extbase 6.1.0; will be removed in Extbase 6.3.0
139 */
140 public function create($objectName) {
141 \TYPO3\CMS\Core\Utility\GeneralUtility::logDeprecatedFunction();
142 $arguments = func_get_args();
143 $instance = call_user_func_array(array($this, 'get'), $arguments);
144 return $instance;
145 }
146
147 /**
148 * Create an instance of $className without calling its constructor
149 *
150 * @param string $className
151 * @return object
152 * @api
153 */
154 public function getEmptyObject($className) {
155 return $this->objectContainer->getEmptyObject($className);
156 }
157 }