2 namespace TYPO3\CMS\Extbase\
Object;
4 /***************************************************************
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.
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.
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.
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.
28 * This copyright notice MUST APPEAR in all copies of the script!
29 ***************************************************************/
31 use \TYPO3\CMS\Extbase\
Object\Container\Container
;
34 * Implementation of the default Extbase Object Manager
36 class ObjectManager
implements ObjectManagerInterface
{
39 * @var \TYPO3\CMS\Extbase\Object\Container\Container
41 protected $objectContainer;
44 * Constructs a new Object Manager
46 public function __construct() {
47 $this->objectContainer
= \TYPO3\CMS\Core\Utility\GeneralUtility
::makeInstance('TYPO3\\CMS\\Extbase\\Object\\Container\\Container');
51 * Serialization (sleep) helper.
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.
60 * @see http://forge.typo3.org/issues/36820
61 * @return array Names of the properties to be serialized
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);
72 * Unserialization (wakeup) helper.
74 * Initializes the properties again that have been removed by
75 * a call to the __sleep() method on serialization before.
77 * @see http://forge.typo3.org/issues/36820
80 public function __wakeup() {
85 * Returns TRUE if an object with the given name is registered
87 * @param string $objectName Name of the object
88 * @return boolean TRUE if the object has been registered, otherwise FALSE
90 public function isRegistered($objectName) {
91 return class_exists($objectName, TRUE);
95 * Returns a fresh or existing instance of the object specified by $objectName.
97 * @param string $objectName The name of the object to return an instance of
98 * @return object The object instance
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);
108 $instance = $this->objectContainer
->getInstance($objectName, $arguments);
114 * Returns the scope of the specified object.
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
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);
125 return $this->objectContainer
->isSingleton($objectName) ? Container
::SCOPE_SINGLETON
: Container
::SCOPE_PROTOTYPE
;
129 * Creates a fresh instance of the object specified by $objectName.
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
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
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);
148 * Create an instance of $className without calling its constructor
150 * @param string $className
154 public function getEmptyObject($className) {
155 return $this->objectContainer
->getEmptyObject($className);