-
-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathIsProxy.php
More file actions
181 lines (138 loc) · 4.59 KB
/
IsProxy.php
File metadata and controls
181 lines (138 loc) · 4.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
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
<?php
/*
* This file is part of the zenstruck/foundry package.
*
* (c) Kevin Bond <kevinbond@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Zenstruck\Foundry\Persistence;
use Symfony\Component\VarExporter\LazyProxyTrait;
use Zenstruck\Assert;
use Zenstruck\Foundry\Configuration;
use Zenstruck\Foundry\Exception\PersistenceNotAvailable;
use Zenstruck\Foundry\Object\Hydrator;
use Zenstruck\Foundry\Persistence\Exception\NoPersistenceStrategy;
use Zenstruck\Foundry\Persistence\Exception\ObjectNoLongerExist;
/**
* @author Kevin Bond <kevinbond@gmail.com>
*
* @internal
*
* @mixin LazyProxyTrait
*/
trait IsProxy // @phpstan-ignore trait.unused
{
private static array $_autoRefresh = [];
public function _enableAutoRefresh(): static
{
$this->_setAutoRefresh(true);
return $this;
}
public function _disableAutoRefresh(): static
{
$this->_setAutoRefresh(false);
return $this;
}
public function _withoutAutoRefresh(callable $callback): static
{
$original = $this->_getAutoRefresh();
$this->_setAutoRefresh(false);
$callback($this);
$this->_setAutoRefresh($original);
return $this;
}
public function _save(): static
{
Configuration::instance()->persistence()->save($this->initializeLazyObject());
return $this;
}
public function _refresh(): static
{
$this->initializeLazyObject();
$object = $this->lazyObjectState->realInstance;
Configuration::instance()->persistence()->refresh($object);
$this->lazyObjectState->realInstance = $object;
return $this;
}
public function _delete(): static
{
Configuration::instance()->persistence()->delete($this->initializeLazyObject());
return $this;
}
public function _get(string $property): mixed
{
$this->_autoRefresh();
return Hydrator::get($this->initializeLazyObject(), $property);
}
public function _set(string $property, mixed $value): static
{
$this->_autoRefresh();
Hydrator::forceSet($this->initializeLazyObject(), $property, $value);
return $this;
}
public function _real(bool $withAutoRefresh = true): object
{
if ($withAutoRefresh) {
try {
// we don't want the auto-refresh mechanism to break "real" object retrieval
$this->_autoRefresh();
} catch (\Throwable) {
}
}
return $this->initializeLazyObject();
}
public function _repository(): ProxyRepositoryDecorator
{
return new ProxyRepositoryDecorator(parent::class, Configuration::instance()->isInMemoryEnabled());
}
public function _assertPersisted(string $message = '{entity} is not persisted.'): static
{
Assert::that($this->isPersisted())->isTrue($message, ['entity' => parent::class]);
return $this;
}
public function _assertNotPersisted(string $message = '{entity} is persisted but it should not be.'): static
{
Assert::that($this->isPersisted())->isFalse($message, ['entity' => parent::class]);
return $this;
}
public function _initializeLazyObject(): void
{
$this->initializeLazyObject();
}
private function isPersisted(): bool
{
$this->initializeLazyObject();
$object = $this->lazyObjectState->realInstance;
return Configuration::instance()->persistence()->isPersisted($object);
}
private function _autoRefresh(): void
{
if (!$this->_getAutoRefresh()) {
return;
}
try {
// we don't want that "transparent" calls to _refresh() to trigger a PersistenceNotAvailable exception
// or a RefreshObjectFailed exception when the object was deleted
$this->_refresh();
} catch (PersistenceNotAvailable|ObjectNoLongerExist|NoPersistenceStrategy) {
}
}
private function _getAutoRefresh(): bool
{
$real = $this->initializeLazyObject();
static::$_autoRefresh[\spl_object_id($real)] ??= true;
return static::$_autoRefresh[\spl_object_id($real)];
}
private function _setAutoRefresh(bool $autoRefresh): void
{
$real = $this->initializeLazyObject();
static::$_autoRefresh[\spl_object_id($real)] = $autoRefresh;
}
// used in ProxyGenerator
private function unproxyArgs(array $args): array
{
return \array_map(ProxyGenerator::unwrap(...), $args);
}
}