Skip to content

Commit

Permalink
refactor(bc layer): introduce Proxy interface
Browse files Browse the repository at this point in the history
  • Loading branch information
nikophil committed Nov 29, 2023
1 parent 73cd6a9 commit 2e91ad6
Show file tree
Hide file tree
Showing 10 changed files with 452 additions and 424 deletions.
7 changes: 6 additions & 1 deletion phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,12 @@ parameters:
path: src/Persistence/RepositoryDecorator.php

-
message: "#^If condition is always false\\.$#"
message: "#^Method Zenstruck\\\\Foundry\\\\Persistence\\\\RepositoryDecorator\\:\\:proxyResult\\(\\) should return array\\<int, Zenstruck\\\\Foundry\\\\Persistence\\\\Proxy\\<TProxiedObject of object\\>\\>\\|Zenstruck\\\\Foundry\\\\Persistence\\\\Proxy\\<TProxiedObject of object\\> but returns Zenstruck\\\\Foundry\\\\Proxy\\.$#"
count: 1
path: src/Persistence/RepositoryDecorator.php

-
message: "#^PHPDoc tag @return with type Zenstruck\\\\Foundry\\\\Persistence\\\\Proxy\\<TObject of object\\> is not subtype of native type Zenstruck\\\\Foundry\\\\Proxy\\.$#"
count: 1
path: src/Proxy.php

Expand Down
11 changes: 6 additions & 5 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Zenstruck\Foundry\Persistence\PersistentProxyObjectFactory;
use Zenstruck\Foundry\Persistence\PostPersistCallback;
use Zenstruck\Foundry\Persistence\Proxy;
use Zenstruck\Foundry\Proxy as ProxyObject;

/**
* @template TObject of object
Expand Down Expand Up @@ -156,17 +157,17 @@ public function create(
}

if (!$this->isPersisting()) {
return $noProxy ? $object : new Proxy($object);
return $noProxy ? $object : new ProxyObject($object);
}

$proxy = new Proxy($object);
$proxy = new ProxyObject($object);

if ($this->cascadePersist && !$postPersistCallbacks) {
return $proxy;
}

$proxy->_save()
->_withoutAutoRefresh(function(Proxy $proxy) use ($attributes, $postPersistCallbacks): void {
->_withoutAutoRefresh(function(ProxyObject $proxy) use ($attributes, $postPersistCallbacks): void {
$callbacks = [...$postPersistCallbacks, ...$this->afterPersist];

if (!$callbacks) {
Expand Down Expand Up @@ -417,7 +418,7 @@ private function normalizeAttributes(array|callable $attributes): array

private function normalizeAttribute(mixed $value, string $name): mixed
{
if ($value instanceof Proxy) {
if ($value instanceof ProxyObject) {
return $value->isPersisted(calledInternally: true) ? $value->_refresh()->_real() : $value->_real();
}

Expand Down Expand Up @@ -489,7 +490,7 @@ private static function normalizeObject(object $object): object
}

try {
return Proxy::createFromPersisted($object)->_refresh()->_real();
return ProxyObject::createFromPersisted($object)->_refresh()->_real();
} catch (\RuntimeException) {
return $object;
}
Expand Down
Loading

0 comments on commit 2e91ad6

Please sign in to comment.