[TASK] Extbase (Persistence): Removed PreparedQuery for now, as it is not used at...
[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 public function __construct($parentObject, $propertyName, $fieldValue, Tx_Extbase_Persistence_Mapper_ColumnMap $columnMap) {
80 $this->queryFactory = t3lib_div::makeInstance('Tx_Extbase_Persistence_QueryFactory');
81 $this->parentObject = $parentObject;
82 $this->propertyName = $propertyName;
83 $this->fieldValue = $fieldValue;
84 $this->columnMap = $columnMap;
85 }
86
87 /**
88 * Populate this proxy by asking the $population closure.
89 *
90 * @return object The instance (hopefully) returned
91 */
92 public function _loadRealInstance() {
93 $dataMapper = Tx_Extbase_Dispatcher::getPersistenceManager()->getBackend()->getDataMapper();
94 $result = $dataMapper->fetchRelatedObjects($this->parentObject, $this->propertyName, $this->fieldValue, $this->columnMap);
95 $this->parentObject->_setProperty($this->propertyName, $result);
96 $this->parentObject->_memorizeCleanState($this->propertyName);
97 return $result;
98 }
99
100 /**
101 * Magic method call implementation.
102 *
103 * @param string $methodName The name of the property to get
104 * @param array $arguments The arguments given to the call
105 * @return mixed
106 */
107 public function __call($methodName, $arguments) {
108 $realInstance = $this->_loadRealInstance();
109 return call_user_func_array(array($realInstance, $methodName), $arguments);
110 }
111
112 /**
113 * Magic get call implementation.
114 *
115 * @param string $propertyName The name of the property to get
116 * @return mixed
117 */
118 public function __get($propertyName) {
119 $realInstance = $this->_loadRealInstance();
120 return $realInstance->$propertyName;
121 }
122
123 /**
124 * Magic set call implementation.
125 *
126 * @param string $propertyName The name of the property to set
127 * @param mixed $value The value for the property to set
128 * @return void
129 */
130 public function __set($propertyName, $value) {
131 $realInstance = $this->_loadRealInstance();
132 $realInstance->$propertyName = $value;
133 }
134
135 /**
136 * Magic isset call implementation.
137 *
138 * @param string $propertyName The name of the property to check
139 * @return boolean
140 */
141 public function __isset($propertyName) {
142 $realInstance = $this->_loadRealInstance();
143 return isset($realInstance->$propertyName);
144 }
145
146 /**
147 * Magic unset call implementation.
148 *
149 * @param string $propertyName The name of the property to unset
150 * @return void
151 */
152 public function __unset($propertyName) {
153 $realInstance = $this->_loadRealInstance();
154 unset($realInstance->$propertyName);
155 }
156 }
157 ?>