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