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