[CLEANUP] Replace wrong/old file copyright comments
[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-2012 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 * Important:
95 *
96 * If possible, instances of Prototype objects should always be created with the
97 * Object Manager's create() method and Singleton objects should rather be
98 * injected by some type of Dependency Injection.
99 *
100 * @param string $objectName The name of the object to return an instance of
101 * @return object The object instance
102 * @api
103 */
104 public function get($objectName) {
105 $arguments = func_get_args();
106 array_shift($arguments);
107 return $this->objectContainer->getInstance($objectName, $arguments);
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 * @throws \TYPO3\CMS\Extbase\Object\Exception\WrongScopeException if the created object is not of scope prototype
121 * @api
122 */
123 public function create($objectName) {
124 $arguments = func_get_args();
125 array_shift($arguments);
126 if ($objectName === 'DateTime') {
127 array_unshift($arguments, $objectName);
128 $instance = call_user_func_array(array('TYPO3\\CMS\\Core\\Utility\\GeneralUtility', 'makeInstance'), $arguments);
129 } else {
130 $instance = $this->objectContainer->getInstance($objectName, $arguments);
131 }
132 if ($instance instanceof \TYPO3\CMS\Core\SingletonInterface) {
133 throw new \TYPO3\CMS\Extbase\Object\Exception\WrongScopeException('Object "' . $objectName . '" is of not of scope prototype, but only prototype is supported by create()', 1265203124);
134 }
135 return $instance;
136 }
137
138 /**
139 * Create an instance of $className without calling its constructor
140 *
141 * @param string $className
142 * @return object
143 * @api
144 */
145 public function getEmptyObject($className) {
146 return $this->objectContainer->getEmptyObject($className);
147 }
148 }
149
150 ?>