1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the eZ Publish Kernel package. |
5
|
|
|
* |
6
|
|
|
* @copyright Copyright (C) eZ Systems AS. All rights reserved. |
7
|
|
|
* @license For full copyright and license information view LICENSE file distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
namespace eZ\Publish\Core\Repository\Values; |
10
|
|
|
|
11
|
|
|
use Generator; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Trait for proxies, covers all relevant magic methods and exposes private method to load object. |
15
|
|
|
* |
16
|
|
|
* Uses generator internally to be able to cleanly execute object load async on-demand. |
17
|
|
|
* |
18
|
|
|
* @internal Meant for internal use by Repository! |
19
|
|
|
*/ |
20
|
|
|
trait GeneratorProxyTrait |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Overload this for correct type hint on object type. |
24
|
|
|
* |
25
|
|
|
* @var object|null |
26
|
|
|
*/ |
27
|
|
|
protected $object; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var mixed |
31
|
|
|
*/ |
32
|
|
|
protected $id; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var Generator|null |
36
|
|
|
*/ |
37
|
|
|
private $generator; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* GeneratorProxyTrait constructor. |
41
|
|
|
* |
42
|
|
|
* @param Generator $generator |
43
|
|
|
* @param mixed $id Object id to use for loading the object on demand. |
44
|
|
|
*/ |
45
|
|
|
public function __construct(Generator $generator, $id) |
46
|
|
|
{ |
47
|
|
|
$this->generator = $generator; |
48
|
|
|
$this->id = $id; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function __call($name, $arguments) |
52
|
|
|
{ |
53
|
|
|
if ($this->object === null) { |
54
|
|
|
$this->loadObject(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return $this->object->$name(...$arguments); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function __invoke(...$args) |
61
|
|
|
{ |
62
|
|
|
if ($this->object === null) { |
63
|
|
|
$this->loadObject(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return ($this->object)(...$args); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
View Code Duplication |
public function __get($name) |
70
|
|
|
{ |
71
|
|
|
if ($name === 'id') { |
72
|
|
|
return $this->id; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
if ($this->object === null) { |
76
|
|
|
$this->loadObject(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $this->object->$name; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
View Code Duplication |
public function __isset($name) |
83
|
|
|
{ |
84
|
|
|
if ($name === 'id') { |
85
|
|
|
return true; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
if ($this->object === null) { |
89
|
|
|
$this->loadObject(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return isset($this->object->$name); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function __unset($name) |
96
|
|
|
{ |
97
|
|
|
if ($this->object === null) { |
98
|
|
|
$this->loadObject(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
unset($this->object->$name); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function __set($name, $value) |
105
|
|
|
{ |
106
|
|
|
if ($this->object === null) { |
107
|
|
|
$this->loadObject(); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$this->object->$name = $value; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function __toString() |
114
|
|
|
{ |
115
|
|
|
if ($this->object === null) { |
116
|
|
|
$this->loadObject(); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return (string)$this->object; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function __sleep() |
123
|
|
|
{ |
124
|
|
|
if ($this->object === null) { |
125
|
|
|
$this->loadObject(); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return ['object', 'id']; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function __debugInfo() |
132
|
|
|
{ |
133
|
|
|
if ($this->object === null) { |
134
|
|
|
$this->loadObject(); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return [ |
138
|
|
|
'object' => $this->object, |
139
|
|
|
]; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Loads the generator to object value and unset's generator. |
144
|
|
|
* |
145
|
|
|
* Can only be called once, so check presence of $this->object before using it. |
146
|
|
|
* |
147
|
|
|
* This assumes generator is made to support bulk loading of several objects, and thus takes object id as input |
148
|
|
|
* in order to return right object at a time, and thus performs two yields per item. |
149
|
|
|
* -> See PR that came with this trait for example generators. |
150
|
|
|
*/ |
151
|
|
|
protected function loadObject() |
152
|
|
|
{ |
153
|
|
|
$this->object = $this->generator->send($this->id); |
154
|
|
|
$this->generator->next(); |
155
|
|
|
unset($this->generator); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|