Skip to content

Commit

Permalink
refactor: fix proxy system
Browse files Browse the repository at this point in the history
  • Loading branch information
nikophil committed Oct 18, 2024
1 parent 0f72ea5 commit ca1cc1a
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 121 deletions.
4 changes: 3 additions & 1 deletion src/Persistence/PersistentObjectFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,9 @@ public static function repository(): ObjectRepository
{
Configuration::instance()->assertPersistanceEnabled();

return new RepositoryDecorator(static::class()); // @phpstan-ignore-line
return is_a(static::class, PersistentProxyObjectFactory::class, allow_string: true) // @phpstan-ignore return.type
? new ProxyRepositoryDecorator(static::class()) // @phpstan-ignore argument.type
: new RepositoryDecorator(static::class());
}

final public static function assert(): RepositoryAssertions
Expand Down
120 changes: 0 additions & 120 deletions src/Persistence/PersistentProxyObjectFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,136 +11,16 @@

namespace Zenstruck\Foundry\Persistence;

use Doctrine\Persistence\ObjectRepository;
use Zenstruck\Foundry\Configuration;
use Zenstruck\Foundry\Factory;
use Zenstruck\Foundry\Object\Instantiator;
use Zenstruck\Foundry\FactoryCollection; // keep me!

/**
* @author Kevin Bond <[email protected]>
*
* @template T of object
* @extends PersistentObjectFactory<T&Proxy<T>>
*
* @phpstan-type InstantiatorCallable = Instantiator|callable(Parameters,class-string<T>):T
* @phpstan-import-type Parameters from Factory
* @phpstan-import-type Attributes from Factory
*
* @phpstan-method $this instantiateWith(InstantiatorCallable $instantiator)
*
* @phpstan-method FactoryCollection<T&Proxy<T>> sequence(iterable<array<string, mixed>>|callable(): iterable<array<string, mixed>> $sequence)
* @phpstan-method FactoryCollection<T&Proxy<T>> many(int $min, int|null $max = null)
*
* @phpstan-method static list<T&Proxy<T>> createSequence(iterable<array<string, mixed>>|callable(): iterable<array<string, mixed>> $sequence)
* @phpstan-method static list<T&Proxy<T>> createMany(int $number, array|callable $attributes = [])
*
* @method static Proxy<T> createOne(Attributes $attributes = [])
* @phpstan-method static T&Proxy<T> createOne(Attributes $attributes = [])
*
* @method Proxy<T> create(Attributes $attributes = [])
* @phpstan-method T&Proxy<T> create(Attributes $attributes = [])
*/
abstract class PersistentProxyObjectFactory extends PersistentObjectFactory
{
/**
* @return class-string<T>
*/
abstract public static function class(): string;

/**
* @return Proxy<T>
* @phpstan-return T&Proxy<T>
*/
final public static function find(mixed $criteriaOrId): object
{
return proxy(parent::find($criteriaOrId)); // @phpstan-ignore-line
}

/**
* @return Proxy<T>
* @phpstan-return T&Proxy<T>
*/
final public static function findOrCreate(array $criteria): object
{
return proxy(parent::findOrCreate($criteria)); // @phpstan-ignore-line
}

/**
* @return Proxy<T>
* @phpstan-return T&Proxy<T>
*/
final public static function randomOrCreate(array $criteria = []): object
{
return proxy(parent::randomOrCreate($criteria)); // @phpstan-ignore-line
}

/**
* @return list<T&Proxy<T>>
*/
final public static function randomSet(int $count, array $criteria = []): array
{
return \array_map(proxy(...), parent::randomSet($count, $criteria)); // @phpstan-ignore-line
}

/**
* @return list<T&Proxy<T>>
*/
final public static function randomRange(int $min, int $max, array $criteria = []): array
{
return \array_map(proxy(...), parent::randomRange($min, $max, $criteria)); // @phpstan-ignore-line
}

/**
* @return list<T&Proxy<T>>
*/
final public static function findBy(array $criteria): array
{
return \array_map(proxy(...), parent::findBy($criteria)); // @phpstan-ignore-line
}

/**
* @return Proxy<T>
* @phpstan-return T&Proxy<T>
*/
final public static function random(array $criteria = []): object
{
return proxy(parent::random($criteria)); // @phpstan-ignore-line
}

/**
* @return Proxy<T>
* @phpstan-return T&Proxy<T>
*/
final public static function first(string $sortBy = 'id'): object
{
return proxy(parent::first($sortBy)); // @phpstan-ignore-line
}

/**
* @return Proxy<T>
* @phpstan-return T&Proxy<T>
*/
final public static function last(string $sortBy = 'id'): object
{
return proxy(parent::last($sortBy)); // @phpstan-ignore-line
}

/**
* @return list<T&Proxy<T>>
*/
final public static function all(): array
{
return \array_map(proxy(...), parent::all()); // @phpstan-ignore-line
}

/**
* @return ProxyRepositoryDecorator<T,ObjectRepository<T>>
*/
final public static function repository(): ObjectRepository
{
Configuration::instance()->assertPersistanceEnabled();

return new ProxyRepositoryDecorator(static::class()); // @phpstan-ignore-line
}
}

0 comments on commit ca1cc1a

Please sign in to comment.