LazyLoadingProxy.php 7.44 KB
Newer Older
Sebastian Kurfürst's avatar
Sebastian Kurfürst committed
1
<?php
2

3
/*
4
 * This file is part of the TYPO3 CMS project.
5
 *
6
7
8
 * It is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License, either version 2
 * of the License, or any later version.
9
 *
10
11
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
12
 *
13
14
 * The TYPO3 project - inspiring people to share!
 */
15

16
17
namespace TYPO3\CMS\Extbase\Persistence\Generic;

18
use TYPO3\CMS\Core\Utility\GeneralUtility;
19
use TYPO3\CMS\Extbase\DomainObject\AbstractDomainObject;
20
use TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface;
21
use TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper;
22

Sebastian Kurfürst's avatar
Sebastian Kurfürst committed
23
24
25
/**
 * A proxy that can replace any object and replaces itself in it's parent on
 * first access (call, get, set, isset, unset).
26
 * @internal only to be used within Extbase, not part of TYPO3 Core API.
Sebastian Kurfürst's avatar
Sebastian Kurfürst committed
27
 */
28
class LazyLoadingProxy implements \Iterator, LoadingStrategyInterface
29
{
30
    protected DataMapper $dataMapper;
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58

    /**
     * The object this property is contained in.
     *
     * @var DomainObjectInterface
     */
    private $parentObject;

    /**
     * The name of the property represented by this proxy.
     *
     * @var string
     */
    private $propertyName;

    /**
     * The raw field value.
     *
     * @var mixed
     */
    private $fieldValue;

    /**
     * Constructs this proxy instance.
     *
     * @param DomainObjectInterface $parentObject The object instance this proxy is part of
     * @param string $propertyName The name of the proxied property in it's parent
     * @param mixed $fieldValue The raw field value.
59
     * @param DataMapper|null $dataMapper
60
     */
61
    public function __construct($parentObject, $propertyName, $fieldValue, ?DataMapper $dataMapper = null)
62
63
64
65
    {
        $this->parentObject = $parentObject;
        $this->propertyName = $propertyName;
        $this->fieldValue = $fieldValue;
66
67
        if ($dataMapper === null) {
            $dataMapper = GeneralUtility::makeInstance(DataMapper::class);
68
        }
69
70
        /** @var DataMapper $dataMapper */
        $this->dataMapper = $dataMapper;
71
72
    }

73
74
75
76
77
78
79
80
81
82
    /**
     * Populate this proxy by asking the $population closure.
     *
     * @return object The instance (hopefully) returned
     */
    public function _loadRealInstance()
    {
        // this check safeguards against a proxy being activated multiple times
        // usually that does not happen, but if the proxy is held from outside
        // its parent ... the result would be weird.
83
84
85
86
        if ($this->parentObject instanceof AbstractDomainObject
            && $this->parentObject->_getProperty($this->propertyName) instanceof LazyLoadingProxy
            && $this->dataMapper
        ) {
87
            $objects = $this->dataMapper->fetchRelated($this->parentObject, $this->propertyName, $this->fieldValue, false);
88
89
90
91
92
            $propertyValue = $this->dataMapper->mapResultToPropertyValue($this->parentObject, $this->propertyName, $objects);
            $this->parentObject->_setProperty($this->propertyName, $propertyValue);
            $this->parentObject->_memorizeCleanState($this->propertyName);
            return $propertyValue;
        }
93
        return $this->parentObject->_getProperty($this->propertyName);
94
95
    }

96
97
98
99
100
101
102
103
104
    /**
     * @return string
     */
    public function _getTypeAndUidString()
    {
        $type = $this->dataMapper->getType(get_class($this->parentObject), $this->propertyName);
        return $type . ':' . $this->fieldValue;
    }

105
106
107
108
109
110
111
112
    /**
     * @return int
     */
    public function getUid(): int
    {
        return (int)$this->fieldValue;
    }

113
114
115
116
117
118
119
120
121
122
123
124
125
    /**
     * Magic method call implementation.
     *
     * @param string $methodName The name of the property to get
     * @param array $arguments The arguments given to the call
     * @return mixed
     */
    public function __call($methodName, $arguments)
    {
        $realInstance = $this->_loadRealInstance();
        if (!is_object($realInstance)) {
            return null;
        }
126
127
        /** @var callable $callable */
        $callable = [$realInstance, $methodName];
128
        return $callable(...$arguments);
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
    }

    /**
     * Magic get call implementation.
     *
     * @param string $propertyName The name of the property to get
     * @return mixed
     */
    public function __get($propertyName)
    {
        $realInstance = $this->_loadRealInstance();
        return $realInstance->{$propertyName};
    }

    /**
     * Magic set call implementation.
     *
     * @param string $propertyName The name of the property to set
     * @param mixed $value The value for the property to set
     */
    public function __set($propertyName, $value)
    {
        $realInstance = $this->_loadRealInstance();
        $realInstance->{$propertyName} = $value;
    }

    /**
     * Magic isset call implementation.
     *
     * @param string $propertyName The name of the property to check
     * @return bool
     */
    public function __isset($propertyName)
    {
        $realInstance = $this->_loadRealInstance();
        return isset($realInstance->{$propertyName});
    }

    /**
     * Magic unset call implementation.
     *
     * @param string $propertyName The name of the property to unset
     */
    public function __unset($propertyName)
    {
        $realInstance = $this->_loadRealInstance();
        unset($realInstance->{$propertyName});
    }

    /**
     * Magic toString call implementation.
     *
     * @return string
     */
    public function __toString()
    {
        $realInstance = $this->_loadRealInstance();
        return $realInstance->__toString();
    }

    /**
     * Returns the current value of the storage array
     *
     * @return mixed
193
     * @todo Set return type to mixed as breaking change in v12 and remove #[\ReturnTypeWillChange].
194
     */
195
    #[\ReturnTypeWillChange]
196
197
    public function current()
    {
198
        // todo: make sure current() can be performed on $realInstance
199
200
201
202
203
204
        $realInstance = $this->_loadRealInstance();
        return current($realInstance);
    }

    /**
     * Returns the current key storage array
205
     * @todo Set return type to int as breaking change in v12 and remove #[\ReturnTypeWillChange].
206
207
     * @return int
     */
208
    #[\ReturnTypeWillChange]
209
210
    public function key()
    {
211
        // todo: make sure key() can be performed on $realInstance
212
213
214
215
216
217
        $realInstance = $this->_loadRealInstance();
        return key($realInstance);
    }

    /**
     * Returns the next position of the storage array
218
     * @todo Set return type to void as breaking change in v12 and remove #[\ReturnTypeWillChange].
219
     */
220
    #[\ReturnTypeWillChange]
221
222
    public function next()
    {
223
        // todo: make sure next() can be performed on $realInstance
224
225
226
227
228
229
        $realInstance = $this->_loadRealInstance();
        next($realInstance);
    }

    /**
     * Resets the array pointer of the storage
230
     * @todo Set return type to void as breaking change in v12 and remove #[\ReturnTypeWillChange].
231
     */
232
    #[\ReturnTypeWillChange]
233
234
    public function rewind()
    {
235
        // todo: make sure reset() can be performed on $realInstance
236
237
238
239
240
241
242
243
        $realInstance = $this->_loadRealInstance();
        reset($realInstance);
    }

    /**
     * Checks if the array pointer of the storage points to a valid position
     *
     * @return bool
244
     * @todo Set return type to bool as breaking change in v12 and remove #[\ReturnTypeWillChange].
245
     */
246
    #[\ReturnTypeWillChange]
247
248
249
250
    public function valid()
    {
        return $this->current() !== false;
    }
Sebastian Kurfürst's avatar
Sebastian Kurfürst committed
251
}