[TASK] Extbase: Removed Extbase_Exception and renamed everything to use Extbase_MVC_E...
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Persistence / LazyLoadingProxy.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 * A proxy that can replace any object and replaces itself in it's parent on
30 * first access (call, get, set, isset, unset).
31 *
32 * @package Extbase
33 * @subpackage Persistence
34 * @version $Id: LazyLoadingProxy.php 2591 2009-06-09 19:23:47Z k-fish $
35 */
36 // TODO Implement support for CountableInterface
37 class Tx_Extbase_Persistence_LazyLoadingProxy {
38
39 /**
40 * @var Tx_Extbase_Persistence_QueryFactoryInterface
41 */
42 protected $queryFactory;
43
44 /**
45 * The object this property is contained in.
46 *
47 * @var object
48 */
49 private $parentObject;
50
51 /**
52 * The name of the property represented by this proxy.
53 *
54 * @var string
55 */
56 private $propertyName;
57
58 /**
59 * The raw field value.
60 *
61 * @var mixed
62 */
63 private $fieldValue;
64
65 /**
66 *
67 * @var Tx_Extbase_Persistence_Mapper_ColumnMap
68 */
69 private $columnMap;
70
71 /**
72 * Constructs this proxy instance.
73 *
74 * @param object $parentObject The object instance this proxy is part of
75 * @param string $propertyName The name of the proxied property in it's parent
76 * @param mixed $fieldValue The raw field value.
77 * @param Tx_Extbase_Persistence_Mapper_DataMap $dataMap The corresponding Data Map of the property
78
79 */
80 public function __construct($parentObject, $propertyName, $fieldValue, Tx_Extbase_Persistence_Mapper_ColumnMap $columnMap) {
81 $this->queryFactory = t3lib_div::makeInstance('Tx_Extbase_Persistence_QueryFactory');
82 $this->parentObject = $parentObject;
83 $this->propertyName = $propertyName;
84 $this->fieldValue = $fieldValue;
85 $this->columnMap = $columnMap;
86 }
87
88 /**
89 * Populate this proxy by asking the $population closure.
90 *
91 * @return object The instance (hopefully) returned
92
93 */
94 public function _loadRealInstance() {
95 $dataMapper = Tx_Extbase_Dispatcher::getPersistenceManager()->getBackend()->getDataMapper();
96 $result = $dataMapper->fetchRelatedObjects($this->parentObject, $this->propertyName, $this->fieldValue, $this->columnMap);
97 $this->parentObject->_setProperty($this->propertyName, $result);
98 $this->parentObject->_memorizeCleanState($this->propertyName);
99 return $result;
100 }
101
102 /**
103 * Magic method call implementation.
104 *
105 * @param string $methodName The name of the property to get
106 * @param array $arguments The arguments given to the call
107 * @return mixed
108
109 */
110 public function __call($methodName, $arguments) {
111 $realInstance = $this->_loadRealInstance();
112 return call_user_func_array(array($realInstance, $methodName), $arguments);
113 }
114
115 /**
116 * Magic get call implementation.
117 *
118 * @param string $propertyName The name of the property to get
119 * @return mixed
120
121 */
122 public function __get($propertyName) {
123 $realInstance = $this->_loadRealInstance();
124 return $realInstance->$propertyName;
125 }
126
127 /**
128 * Magic set call implementation.
129 *
130 * @param string $propertyName The name of the property to set
131 * @param mixed $value The value for the property to set
132 * @return void
133
134 */
135 public function __set($propertyName, $value) {
136 $realInstance = $this->_loadRealInstance();
137 $realInstance->$propertyName = $value;
138 }
139
140 /**
141 * Magic isset call implementation.
142 *
143 * @param string $propertyName The name of the property to check
144 * @return boolean
145
146 */
147 public function __isset($propertyName) {
148 $realInstance = $this->_loadRealInstance();
149 return isset($realInstance->$propertyName);
150 }
151
152 /**
153 * Magic unset call implementation.
154 *
155 * @param string $propertyName The name of the property to unset
156 * @return void
157
158 */
159 public function __unset($propertyName) {
160 $realInstance = $this->_loadRealInstance();
161 unset($realInstance->$propertyName);
162 }
163 }
164 ?>