Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(bc layer): introduce Proxy interface #528

Merged
merged 1 commit into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,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