1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Zenstruck\Foundry; |
4
|
|
|
|
5
|
|
|
use Doctrine\Persistence\ObjectManager; |
6
|
|
|
use PHPUnit\Framework\Assert; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @author Kevin Bond <[email protected]> |
10
|
|
|
*/ |
11
|
|
|
final class Proxy |
12
|
|
|
{ |
13
|
|
|
/** @var object */ |
14
|
|
|
private $object; |
15
|
|
|
|
16
|
|
|
/** @var string */ |
17
|
|
|
private $class; |
18
|
|
|
|
19
|
|
|
/** @var bool */ |
20
|
|
|
private $autoRefresh = false; |
21
|
|
|
|
22
|
|
|
/** @var bool */ |
23
|
|
|
private $persisted = false; |
24
|
|
|
|
25
|
258 |
|
public function __construct(object $object) |
26
|
|
|
{ |
27
|
258 |
|
$this->object = $object; |
28
|
258 |
|
$this->class = \get_class($object); |
29
|
258 |
|
} |
30
|
|
|
|
31
|
126 |
|
public function __call(string $method, array $arguments) |
32
|
|
|
{ |
33
|
126 |
|
return $this->object()->{$method}(...$arguments); |
34
|
|
|
} |
35
|
|
|
|
36
|
4 |
|
public function __get(string $name) |
37
|
|
|
{ |
38
|
4 |
|
return $this->object()->{$name}; |
39
|
|
|
} |
40
|
|
|
|
41
|
4 |
|
public function __set(string $name, $value): void |
42
|
|
|
{ |
43
|
4 |
|
$this->object()->{$name} = $value; |
44
|
4 |
|
} |
45
|
|
|
|
46
|
4 |
|
public function __unset(string $name): void |
47
|
|
|
{ |
48
|
4 |
|
unset($this->object()->{$name}); |
49
|
4 |
|
} |
50
|
|
|
|
51
|
4 |
|
public function __isset(string $name): bool |
52
|
|
|
{ |
53
|
4 |
|
return isset($this->object()->{$name}); |
54
|
|
|
} |
55
|
|
|
|
56
|
8 |
|
public function __toString(): string |
57
|
|
|
{ |
58
|
8 |
|
if (!\method_exists($this->object, '__toString')) { |
59
|
4 |
|
if (\PHP_VERSION_ID < 70400) { |
60
|
|
|
return '(no __toString)'; |
61
|
|
|
} |
62
|
|
|
|
63
|
4 |
|
throw new \RuntimeException(\sprintf('Proxied object "%s" cannot be converted to a string.', $this->class)); |
64
|
|
|
} |
65
|
|
|
|
66
|
4 |
|
return $this->object()->__toString(); |
67
|
|
|
} |
68
|
|
|
|
69
|
52 |
|
public static function persisted(object $object): self |
70
|
|
|
{ |
71
|
52 |
|
$proxy = new self($object); |
72
|
52 |
|
$proxy->persisted = $proxy->autoRefresh = true; |
73
|
|
|
|
74
|
52 |
|
return $proxy; |
75
|
|
|
} |
76
|
|
|
|
77
|
178 |
|
public function isPersisted(): bool |
78
|
|
|
{ |
79
|
178 |
|
return $this->persisted; |
80
|
|
|
} |
81
|
|
|
|
82
|
158 |
|
public function object(): object |
83
|
|
|
{ |
84
|
158 |
|
if ($this->autoRefresh && $this->persisted) { |
85
|
78 |
|
$this->refresh(); |
86
|
|
|
} |
87
|
|
|
|
88
|
158 |
|
return $this->object; |
89
|
|
|
} |
90
|
|
|
|
91
|
186 |
|
public function save(): self |
92
|
|
|
{ |
93
|
186 |
|
$this->objectManager()->persist($this->object); |
94
|
186 |
|
$this->objectManager()->flush(); |
95
|
186 |
|
$this->autoRefresh = $this->persisted = true; |
96
|
|
|
|
97
|
186 |
|
return $this; |
98
|
|
|
} |
99
|
|
|
|
100
|
4 |
|
public function remove(): self |
101
|
|
|
{ |
102
|
4 |
|
$this->objectManager()->remove($this->object); |
103
|
4 |
|
$this->objectManager()->flush(); |
104
|
4 |
|
$this->autoRefresh = $this->persisted = false; |
105
|
|
|
|
106
|
4 |
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
86 |
|
public function refresh(): self |
110
|
|
|
{ |
111
|
86 |
|
if (!$this->persisted) { |
112
|
4 |
|
throw new \RuntimeException(\sprintf('Cannot refresh unpersisted object (%s).', $this->class)); |
113
|
|
|
} |
114
|
|
|
|
115
|
82 |
|
if ($this->objectManager()->contains($this->object)) { |
116
|
78 |
|
$this->objectManager()->refresh($this->object); |
117
|
|
|
|
118
|
78 |
|
return $this; |
119
|
|
|
} |
120
|
|
|
|
121
|
8 |
|
if (!$object = $this->fetchObject()) { |
122
|
4 |
|
throw new \RuntimeException('The object no longer exists.'); |
123
|
|
|
} |
124
|
|
|
|
125
|
4 |
|
$this->object = $object; |
126
|
|
|
|
127
|
4 |
|
return $this; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param mixed $value |
132
|
|
|
*/ |
133
|
12 |
|
public function forceSet(string $property, $value): self |
134
|
|
|
{ |
135
|
12 |
|
return $this->forceSetAll([$property => $value]); |
136
|
|
|
} |
137
|
|
|
|
138
|
12 |
|
public function forceSetAll(array $properties): self |
139
|
|
|
{ |
140
|
12 |
|
$object = $this->object(); |
141
|
|
|
|
142
|
12 |
|
foreach ($properties as $property => $value) { |
143
|
12 |
|
Instantiator::forceSet($object, $property, $value); |
144
|
|
|
} |
145
|
|
|
|
146
|
12 |
|
return $this; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @return mixed |
151
|
|
|
*/ |
152
|
4 |
|
public function forceGet(string $property) |
153
|
|
|
{ |
154
|
4 |
|
return Instantiator::forceGet($this->object(), $property); |
155
|
|
|
} |
156
|
|
|
|
157
|
4 |
|
public function repository(): RepositoryProxy |
158
|
|
|
{ |
159
|
4 |
|
return Factory::configuration()->repositoryFor($this->class); |
160
|
|
|
} |
161
|
|
|
|
162
|
182 |
|
public function enableAutoRefresh(): self |
163
|
|
|
{ |
164
|
182 |
|
if (!$this->persisted) { |
165
|
|
|
throw new \RuntimeException(\sprintf('Cannot enable auto-refresh on unpersisted object (%s).', $this->class)); |
166
|
|
|
} |
167
|
|
|
|
168
|
182 |
|
$this->autoRefresh = true; |
169
|
|
|
|
170
|
182 |
|
return $this; |
171
|
|
|
} |
172
|
|
|
|
173
|
186 |
|
public function disableAutoRefresh(): self |
174
|
|
|
{ |
175
|
186 |
|
$this->autoRefresh = false; |
176
|
|
|
|
177
|
186 |
|
return $this; |
178
|
|
|
} |
179
|
|
|
|
180
|
4 |
|
public function withoutAutoRefresh(callable $callback): self |
181
|
|
|
{ |
182
|
4 |
|
$this->disableAutoRefresh(); |
183
|
|
|
|
184
|
4 |
|
$this->executeCallback($callback); |
185
|
|
|
|
186
|
4 |
|
return $this->isPersisted() ? $this->enableAutoRefresh() : $this; |
187
|
|
|
} |
188
|
|
|
|
189
|
4 |
|
public function assertPersisted(): self |
190
|
|
|
{ |
191
|
|
|
// todo improve message |
192
|
4 |
|
Assert::assertNotNull($this->fetchObject(), 'The object is not persisted.'); |
193
|
|
|
|
194
|
4 |
|
return $this; |
195
|
|
|
} |
196
|
|
|
|
197
|
4 |
|
public function assertNotPersisted(): self |
198
|
|
|
{ |
199
|
|
|
// todo improve message |
200
|
4 |
|
Assert::assertNull($this->fetchObject(), 'The object is persisted but it should not be.'); |
201
|
|
|
|
202
|
4 |
|
return $this; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* @internal |
207
|
|
|
*/ |
208
|
8 |
|
public function executeCallback(callable $callback, ...$arguments): void |
209
|
|
|
{ |
210
|
8 |
|
$object = $this; |
211
|
8 |
|
$parameters = (new \ReflectionFunction($callback))->getParameters(); |
212
|
|
|
|
213
|
8 |
|
if (isset($parameters[0]) && $parameters[0]->getType() && $this->class === $parameters[0]->getType()->getName()) { |
214
|
8 |
|
$object = $object->object(); |
215
|
|
|
} |
216
|
|
|
|
217
|
8 |
|
$callback($object, ...$arguments); |
218
|
8 |
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Todo - move to RepositoryProxy? |
222
|
|
|
*/ |
223
|
16 |
|
private function fetchObject(): ?object |
224
|
|
|
{ |
225
|
16 |
|
$id = $this->objectManager()->getClassMetadata($this->class)->getIdentifierValues($this->object); |
226
|
|
|
|
227
|
16 |
|
return empty($id) ? null : $this->objectManager()->find($this->class, $id); |
228
|
|
|
} |
229
|
|
|
|
230
|
186 |
|
private function objectManager(): ObjectManager |
231
|
|
|
{ |
232
|
186 |
|
return Factory::configuration()->objectManagerFor($this->class); |
233
|
|
|
} |
234
|
|
|
} |
235
|
|
|
|