Skip to content

Commit c28b9d7

Browse files
authored
minor: add parameter "withAutoRefresh" to unproxy() function (#840)
1 parent 6edd8c2 commit c28b9d7

File tree

3 files changed

+9
-15
lines changed

3 files changed

+9
-15
lines changed

src/Persistence/PersistentObjectFactory.php

+4-10
Original file line numberDiff line numberDiff line change
@@ -296,11 +296,7 @@ protected function normalizeParameter(string $field, mixed $value): mixed
296296
$inversedObject = $value->withPersistMode(PersistMode::NO_PERSIST_BUT_SCHEDULE_FOR_INSERT)
297297
->create([$inverseField => $placeholder = (new \ReflectionClass(static::class()))->newInstanceWithoutConstructor()]);
298298

299-
// auto-refresh computes changeset and prevents the placeholder object to be cleanly
300-
// forgotten fom the persistence manager
301-
if ($inversedObject instanceof Proxy) {
302-
$inversedObject = $inversedObject->_real(withAutoRefresh: false);
303-
}
299+
$inversedObject = unproxy($inversedObject, withAutoRefresh: false);
304300

305301
$this->tempAfterInstantiate[] = static function(object $object) use ($inversedObject, $inverseField, $pm, $placeholder) {
306302
$pm->forget($placeholder);
@@ -311,7 +307,7 @@ protected function normalizeParameter(string $field, mixed $value): mixed
311307
}
312308
}
313309

314-
return unproxy(parent::normalizeParameter($field, $value));
310+
return unproxy(parent::normalizeParameter($field, $value), withAutoRefresh: false);
315311
}
316312

317313
protected function normalizeCollection(string $field, FactoryCollection $collection): array
@@ -330,7 +326,7 @@ protected function normalizeCollection(string $field, FactoryCollection $collect
330326

331327
$inverseObjects = $collection->withPersistMode(PersistMode::NO_PERSIST_BUT_SCHEDULE_FOR_INSERT)->create([$inverseField => $object]);
332328

333-
$inverseObjects = unproxy($inverseObjects);
329+
$inverseObjects = unproxy($inverseObjects, withAutoRefresh: false);
334330

335331
// if the collection is indexed by a field, index the array
336332
if ($inverseRelationshipMetadata->collectionIndexedBy) {
@@ -366,9 +362,7 @@ protected function normalizeObject(object $object): object
366362
return $object;
367363
}
368364

369-
if ($object instanceof Proxy) {
370-
$object = $object->_real(withAutoRefresh: false);
371-
}
365+
$object = unproxy($object, withAutoRefresh: false);
372366

373367
$persistenceManager = $configuration->persistence();
374368
if (!$persistenceManager->hasPersistenceFor($object)) {

src/Persistence/ProxyGenerator.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -66,18 +66,18 @@ public static function wrapFactory(PersistentProxyObjectFactory $factory, callab
6666
*
6767
* @return T
6868
*/
69-
public static function unwrap(mixed $what): mixed
69+
public static function unwrap(mixed $what, bool $withAutoRefresh = true): mixed
7070
{
7171
if (\is_array($what)) {
72-
return \array_map(self::unwrap(...), $what); // @phpstan-ignore return.type
72+
return \array_map(static fn(mixed $w) => self::unwrap($w, $withAutoRefresh), $what); // @phpstan-ignore return.type
7373
}
7474

7575
if (\is_string($what) && \is_a($what, Proxy::class, true)) {
7676
return \get_parent_class($what) ?: throw new \LogicException('Could not unwrap proxy.'); // @phpstan-ignore return.type
7777
}
7878

7979
if ($what instanceof Proxy) {
80-
return $what->_real(); // @phpstan-ignore return.type
80+
return $what->_real($withAutoRefresh); // @phpstan-ignore return.type
8181
}
8282

8383
return $what;

src/Persistence/functions.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,9 @@ function proxy(object $object): object
107107
*
108108
* @return T
109109
*/
110-
function unproxy(mixed $what): mixed
110+
function unproxy(mixed $what, bool $withAutoRefresh = true): mixed
111111
{
112-
return ProxyGenerator::unwrap($what);
112+
return ProxyGenerator::unwrap($what, $withAutoRefresh);
113113
}
114114

115115
/**

0 commit comments

Comments
 (0)