5e7fdd2b6cdf18476649dd78f78fcd359423d691
[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_Container_Container
38 */
39 protected $objectContainer;
40
41 /**
42 * Constructs a new Object Manager
43 */
44 public function __construct() {
45 $this->objectContainer = Tx_Container_Container::getContainer();
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 return call_user_func_array(array($this->objectContainer, 'getInstance'), $arguments);
64 }
65
66 /**
67 * Creates a fresh instance of the object specified by $objectName.
68 *
69 * This factory method can only create objects of the scope prototype.
70 * Singleton objects must be either injected by some type of Dependency Injection or
71 * if that is not possible, be retrieved by the get() method of the
72 * Object Manager
73 *
74 * @param string $objectName The name of the object to create
75 * @return object The new object instance
76 * @throws Tx_Extbase_Object_Exception_WrongScropeException if the created object is not of scope prototype
77 * @api
78 */
79 public function create($objectName) {
80 $arguments = func_get_args();
81 $instance = call_user_func_array(array($this->objectContainer, 'getInstance'), $arguments);
82
83 if ($instance instanceof t3lib_Singleton) {
84 throw new Tx_Extbase_Object_Exception_WrongScope('Object "' . $objectName . '" is of not of scope prototype, but only prototype is supported by create()', 1265203124);
85 }
86
87 return $instance;
88 }
89
90 /**
91 * Registers a classname that should be used to resolve a given interface.
92 *
93 * Per default the interface's name stripped of "Interface" will be used.
94 * @param string $className
95 * @param string $alternativeClassName
96 */
97 static public function registerImplementation($className, $alternativeClassName) {
98 return Tx_Container_Container::getContainer()->registerImplementation($className, $alternativeClassName);
99 }
100 }
101
102 ?>