[~CONFIGURATION] Extbase: Set lockType to empty string instead of 'system'.
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Persistence / LazyObjectStorage.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 class Tx_Extbase_Persistence_LazyObjectStorage extends Tx_Extbase_Persistence_ObjectStorage implements Tx_Extbase_Persistence_LoadingStrategyInterface {
37
38 /**
39 * @var Tx_Extbase_Persistence_QueryFactoryInterface
40 */
41 protected $queryFactory;
42
43 /**
44 * The object this property is contained in.
45 *
46 * @var object
47 */
48 private $parentObject;
49
50 /**
51 * The name of the property represented by this proxy.
52 *
53 * @var string
54 */
55 private $propertyName;
56
57 /**
58 * The raw field value.
59 *
60 * @var mixed
61 */
62 private $fieldValue;
63
64 /**
65 *
66 * @var Tx_Extbase_Persistence_Mapper_ColumnMap
67 */
68 private $columnMap;
69
70 /**
71 * Constructs this proxy instance.
72 *
73 * @param object $parentObject The object instance this proxy is part of
74 * @param string $propertyName The name of the proxied property in it's parent
75 * @param mixed $fieldValue The raw field value.
76 * @param Tx_Extbase_Persistence_Mapper_DataMap $dataMap The corresponding Data Map of the property
77 */
78 public function __construct($parentObject, $propertyName, $fieldValue, Tx_Extbase_Persistence_Mapper_ColumnMap $columnMap) {
79 $this->queryFactory = t3lib_div::makeInstance('Tx_Extbase_Persistence_QueryFactory');
80 $this->parentObject = $parentObject;
81 $this->propertyName = $propertyName;
82 $this->fieldValue = $fieldValue;
83 $this->columnMap = $columnMap;
84 $this->storage = NULL; // TODO
85 }
86
87 /**
88 * This is a function lazy load implementation.
89 *
90 * @return void
91 */
92 protected function initializeStorage() {
93 if (is_null($this->storage)) {
94 $dataMapper = Tx_Extbase_Dispatcher::getPersistenceManager()->getBackend()->getDataMapper();
95 $objects = $dataMapper->fetchRelatedObjects($this->parentObject, $this->propertyName, $this->fieldValue, $this->columnMap);
96 $storage = array();
97 foreach ($objects as $object) {
98 $storage[spl_object_hash($object)] = $object;
99 }
100 $this->storage = $storage;
101 $this->parentObject->_memorizeCleanState($this->propertyName);
102 }
103 if (!is_array($this->storage)) {
104 throw new Tx_Extbase_Persistence_Exception('The storage could not be initialized.', 1252393014); // TODO
105 }
106 }
107
108 /**
109 * Counts the elements in the storage array
110 *
111 * @return void
112 */
113 public function count() {
114 $numberOfElements = NULL;
115 if ($this->columnMap->getTypeOfRelation() === Tx_Extbase_Persistence_Mapper_ColumnMap::RELATION_HAS_MANY) {
116 $parentKeyFieldName = $this->columnMap->getParentKeyFieldName();
117 if (!empty($parentKeyFieldName)) {
118 $numberOfElements = $this->fieldValue;
119 } else {
120 $this->initializeStorage();
121 $numberOfElements = count($this->storage);
122 // FIXME Count on comma separated lists does not respect hidden objects
123 // if (empty($this->fieldValue)) {
124 // $numberOfElements = 0;
125 // }
126 // $numberOfElements = count(explode(',', $this->fieldValue));
127 }
128 } elseif ($this->columnMap->getTypeOfRelation() === Tx_Extbase_Persistence_Mapper_ColumnMap::RELATION_HAS_AND_BELONGS_TO_MANY) {
129 $numberOfElements = $this->fieldValue;
130 } else {
131 $this->initializeStorage();
132 $numberOfElements = count($this->storage);
133 }
134 if (is_null($numberOfElements)) {
135 throw new Tx_Extbase_Persistence_Exception('The number of elements could not be determined.', 1252514486);
136 }
137 return $numberOfElements;
138 }
139
140
141 }
142 ?>