Skip to content

Commit

Permalink
refactor: deprecate proxy class
Browse files Browse the repository at this point in the history
  • Loading branch information
nikophil committed Nov 4, 2023
1 parent dc871b7 commit 81eb5d2
Show file tree
Hide file tree
Showing 54 changed files with 597 additions and 447 deletions.
2 changes: 2 additions & 0 deletions src/AnonymousFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Zenstruck\Foundry;

use Zenstruck\Foundry\Persistence\Proxy;

/**
* @template TModel of object
* @template-extends Factory<TModel>
Expand Down
2 changes: 1 addition & 1 deletion src/Bundle/Maker/Factory/MakeFactoryData.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use Symfony\Bundle\MakerBundle\Str;
use Symfony\Bundle\MakerBundle\Util\ClassNameDetails;
use Zenstruck\Foundry\Persistence\PersistentProxyObjectFactory;
use Zenstruck\Foundry\Proxy;
use Zenstruck\Foundry\Persistence\Proxy;
use Zenstruck\Foundry\RepositoryProxy;

/**
Expand Down
3 changes: 2 additions & 1 deletion src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Doctrine\Persistence\ObjectManager;
use Faker;
use Zenstruck\Foundry\Exception\FoundryBootException;
use Zenstruck\Foundry\Persistence\Proxy;

/**
* @internal
Expand Down Expand Up @@ -184,7 +185,7 @@ public function repositoryFor(object|string $objectOrClass): RepositoryProxy
}

if ($objectOrClass instanceof Proxy) {
$objectOrClass = $objectOrClass->object();
$objectOrClass = $objectOrClass->_real();
}

if (!\is_string($objectOrClass)) {
Expand Down
21 changes: 11 additions & 10 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Zenstruck\Foundry\Exception\FoundryBootException;
use Zenstruck\Foundry\Persistence\InversedRelationshipPostPersistCallback;
use Zenstruck\Foundry\Persistence\PostPersistCallback;
use Zenstruck\Foundry\Persistence\Proxy;

/**
* @template TObject of object
Expand Down Expand Up @@ -152,8 +153,8 @@ final public function create(array|callable $attributes = []): Proxy
}

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

if (!$callbacks) {
Expand All @@ -164,7 +165,7 @@ final public function create(array|callable $attributes = []): Proxy
$proxy->executeCallback($callback, $attributes);
}

$proxy->save(); // save again as afterPersist events may have modified
$proxy->_save(); // save again as afterPersist events may have modified
})
;
}
Expand Down Expand Up @@ -375,7 +376,7 @@ private function normalizeAttributes(array|callable $attributes): array
private function normalizeAttribute(mixed $value, string $name): mixed
{
if ($value instanceof Proxy) {
return $value->isPersisted() ? $value->refresh()->object() : $value->object();
return $value->isPersisted(calledInternally: true) ? $value->_refresh()->_real() : $value->_real();
}

if ($value instanceof FactoryCollection) {
Expand All @@ -399,25 +400,25 @@ private function normalizeAttribute(mixed $value, string $name): mixed
}

if (!self::configuration()->hasManagerRegistry()) {
return $value->create()->object();
return $value->create()->_real();
}

try {
$objectManager = self::configuration()->objectManagerFor($this->class);

if (!$objectManager instanceof EntityManagerInterface || $objectManager->getClassMetadata($value->class)->isEmbeddedClass) {
// we may deal with ODM document or ORM\Embedded
return $value->create()->object();
return $value->create()->_real();
}
} catch (\Throwable) {
// not persisted object
return $value->create()->object();
return $value->create()->_real();
}

$relationshipMetadata = self::getRelationshipMetadata($objectManager, $this->class, $name);

if (!$relationshipMetadata) {
return $value->create()->object();
return $value->create()->_real();
}

if ($relationshipMetadata['isOwningSide']) {
Expand All @@ -436,13 +437,13 @@ private function normalizeAttribute(mixed $value, string $name): mixed
$value = $value->withCascadePersist();
}

return $value->create()->object();
return $value->create()->_real();
}

private static function normalizeObject(object $object): object
{
try {
return Proxy::createFromPersisted($object)->refresh()->object();
return Proxy::createFromPersisted($object)->_refresh()->_real();
} catch (\RuntimeException) {
return $object;
}
Expand Down
2 changes: 2 additions & 0 deletions src/FactoryCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Zenstruck\Foundry;

use Zenstruck\Foundry\Persistence\Proxy;

/**
* @template TObject of object
*
Expand Down
1 change: 1 addition & 0 deletions src/ModelFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Zenstruck\Foundry;

use Zenstruck\Foundry\Persistence\PersistentProxyObjectFactory;
use Zenstruck\Foundry\Persistence\Proxy;

/**
* @template TModel of object
Expand Down
4 changes: 2 additions & 2 deletions src/Persistence/InversedRelationshipPostPersistCallback.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
namespace Zenstruck\Foundry\Persistence;

use Zenstruck\Foundry\Factory;
use Zenstruck\Foundry\Proxy;
use Zenstruck\Foundry\Persistence\Proxy;

/**
* @internal
Expand All @@ -34,6 +34,6 @@ public function __invoke(Proxy $proxy): void
$this->relationshipField => $this->isCollection ? [$proxy] : $proxy,
]);

$proxy->refresh();
$proxy->_refresh();
}
}
1 change: 0 additions & 1 deletion src/Persistence/PersistentProxyObjectFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Zenstruck\Foundry\Exception\FoundryBootException;
use Zenstruck\Foundry\Factory;
use Zenstruck\Foundry\Proxy;
use Zenstruck\Foundry\RepositoryAssertions;
use Zenstruck\Foundry\RepositoryProxy;

Expand Down
2 changes: 1 addition & 1 deletion src/Persistence/PostPersistCallback.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace Zenstruck\Foundry\Persistence;

use Zenstruck\Foundry\Proxy;
use Zenstruck\Foundry\Persistence\Proxy;

/**
* @internal
Expand Down
Loading

0 comments on commit 81eb5d2

Please sign in to comment.