[!!!] [~TASK] BlogExample (Domain): The directory structure of the Domain has changed...
[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 *
60 * @var Tx_Extbase_Persistence_Mapper_DataMap
61 */
62 private $dataMap;
63
64 /**
65 * Constructs this proxy instance.
66 *
67 * @param object $parentObject The object instance this proxy is part of
68 * @param string $propertyName The name of the proxied property in it's parent
69 * @param Tx_Extbase_Persistence_Mapper_DataMap $dataMap The corresponding Data Map of the property
70 * @internal
71 */
72 public function __construct($parentObject, $propertyName, Tx_Extbase_Persistence_Mapper_DataMap $dataMap) {
73 $this->queryFactory = t3lib_div::makeInstance('Tx_Extbase_Persistence_QueryFactory');
74 $this->parentObject = $parentObject;
75 $this->propertyName = $propertyName;
76 $this->dataMap = $dataMap;
77 }
78
79 /**
80 * Populate this proxy by asking the $population closure.
81 *
82 * @return object The instance (hopefully) returned
83 * @internal
84 */
85 public function _loadRealInstance() {
86 $result = NULL;
87 $columnMap = $this->dataMap->getColumnMap($this->propertyName);
88 $objectStorage = new Tx_Extbase_Persistence_ObjectStorage();
89 // TODO This if statement should be further encapsulated to follow the DRY principle (see Data Mapper)
90 if ($columnMap->getTypeOfRelation() === Tx_Extbase_Persistence_Mapper_ColumnMap::RELATION_HAS_ONE) {
91 $query = $this->queryFactory->create($columnMap->getChildClassName(), FALSE);
92 $result = current($query->matching($query->withUid($row[$columnMap->getColumnName()]))->execute());
93 } elseif ($columnMap->getTypeOfRelation() === Tx_Extbase_Persistence_Mapper_ColumnMap::RELATION_HAS_MANY) {
94 $objectStorage = new Tx_Extbase_Persistence_ObjectStorage();
95 $query = $this->queryFactory->create($columnMap->getChildClassName(), FALSE);
96 $parentKeyFieldName = $columnMap->getParentKeyFieldName();
97 if (isset($parentKeyFieldName)) {
98 $objects = $query->matching($query->equals($columnMap->getParentKeyFieldName(), $this->parentObject->getUid()))->execute();
99 } else {
100 $propertyValue = $row[$propertyName];
101 $objects = $query->matching($query->withUid((int)$propertyValue))->execute();
102 }
103 foreach ($objects as $object) {
104 $objectStorage->attach($object);
105 }
106 $result = $objectStorage;
107 } elseif ($columnMap->getTypeOfRelation() === Tx_Extbase_Persistence_Mapper_ColumnMap::RELATION_HAS_AND_BELONGS_TO_MANY) {
108 $objectStorage = new Tx_Extbase_Persistence_ObjectStorage();
109 $relationTableName = $columnMap->getRelationTableName();
110 $left = $this->QOMFactory->selector($relationTableName);
111 $childTableName = $columnMap->getChildTableName();
112 $right = $this->QOMFactory->selector($childTableName);
113 $joinCondition = $this->QOMFactory->equiJoinCondition($relationTableName, $columnMap->getChildKeyFieldName(), $childTableName, 'uid');
114 $source = $this->QOMFactory->join(
115 $left,
116 $right,
117 Tx_Extbase_Persistence_QOM_QueryObjectModelConstantsInterface::JCR_JOIN_TYPE_INNER,
118 $joinCondition
119 );
120 $query = $this->queryFactory->create($columnMap->getChildClassName(), FALSE);
121 $query->setSource($source);
122 $objects = $query->matching($query->equals($columnMap->getParentKeyFieldName(), $this->parentObject->getUid()))->execute();
123 foreach ($objects as $object) {
124 $objectStorage->attach($object);
125 }
126 $result = $objectStorage;
127 }
128 $this->parentObject->_setProperty($this->propertyName, $result);
129 $this->parentObject->_memorizeCleanState($this->propertyName);
130 return $result;
131 }
132
133 /**
134 * Magic method call implementation.
135 *
136 * @param string $methodName The name of the property to get
137 * @param array $arguments The arguments given to the call
138 * @return mixed
139 * @internal
140 */
141 public function __call($methodName, $arguments) {
142 $realInstance = $this->_loadRealInstance();
143 return call_user_func_array(array($realInstance, $methodName), $arguments);
144 }
145
146 /**
147 * Magic get call implementation.
148 *
149 * @param string $propertyName The name of the property to get
150 * @return mixed
151 * @internal
152 */
153 public function __get($propertyName) {
154 $realInstance = $this->_loadRealInstance();
155 return $realInstance->$propertyName;
156 }
157
158 /**
159 * Magic set call implementation.
160 *
161 * @param string $propertyName The name of the property to set
162 * @param mixed $value The value for the property to set
163 * @return void
164 * @internal
165 */
166 public function __set($propertyName, $value) {
167 $realInstance = $this->_loadRealInstance();
168 $realInstance->$propertyName = $value;
169 }
170
171 /**
172 * Magic isset call implementation.
173 *
174 * @param string $propertyName The name of the property to check
175 * @return boolean
176 * @internal
177 */
178 public function __isset($propertyName) {
179 $realInstance = $this->_loadRealInstance();
180 return isset($realInstance->$propertyName);
181 }
182
183 /**
184 * Magic unset call implementation.
185 *
186 * @param string $propertyName The name of the property to unset
187 * @return void
188 * @internal
189 */
190 public function __unset($propertyName) {
191 $realInstance = $this->_loadRealInstance();
192 unset($realInstance->$propertyName);
193 }
194 }
195 ?>