[!!!][+TASK] Extbase (Object): Rewritten Object Container
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Object / ObjectManager.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009 Jochen Rau <jochen.rau@typoplanet.de>
6 * All rights reserved
7 *
8 * This class is a backport of the corresponding class of FLOW3.
9 * All credits go to the v5 team.
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 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27
28 /**
29 * Implementation of the default Extbase Object Manager
30 *
31 * @package Extbase
32 * @subpackage Object
33 */
34 class Tx_Extbase_Object_ObjectManager implements Tx_Extbase_Object_ObjectManagerInterface {
35
36 /**
37 * @var Tx_Extbase_Object_Container_Container
38 */
39 protected $objectContainer;
40
41 /**
42 * Constructs a new Object Manager
43 */
44 public function __construct() {
45 $this->objectContainer = t3lib_div::makeInstance('Tx_Extbase_Object_Container_Container'); // Singleton
46 }
47
48 /**
49 * Returns a fresh or existing instance of the object specified by $objectName.
50 *
51 * Important:
52 *
53 * If possible, instances of Prototype objects should always be created with the
54 * Object Manager's create() method and Singleton objects should rather be
55 * injected by some type of Dependency Injection.
56 *
57 * @param string $objectName The name of the object to return an instance of
58 * @return object The object instance
59 * @api
60 */
61 public function get($objectName) {
62 $arguments = func_get_args();
63 array_shift($arguments);
64 return $this->objectContainer->getInstance($objectName, $arguments);
65 }
66
67 /**
68 * Creates a fresh instance of the object specified by $objectName.
69 *
70 * This factory method can only create objects of the scope prototype.
71 * Singleton objects must be either injected by some type of Dependency Injection or
72 * if that is not possible, be retrieved by the get() method of the
73 * Object Manager
74 *
75 * @param string $objectName The name of the object to create
76 * @return object The new object instance
77 * @throws Tx_Extbase_Object_Exception_WrongScropeException if the created object is not of scope prototype
78 * @api
79 */
80 public function create($objectName) {
81 $arguments = func_get_args();
82 array_shift($arguments);
83 $instance = $this->objectContainer->getInstance($objectName, $arguments);
84
85 if ($instance instanceof t3lib_Singleton) {
86 throw new Tx_Extbase_Object_Exception_WrongScope('Object "' . $objectName . '" is of not of scope prototype, but only prototype is supported by create()', 1265203124);
87 }
88
89 return $instance;
90 }
91 }
92
93 ?>