-
-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: introduce "in-memory" behavior
- Loading branch information
Showing
20 changed files
with
560 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
namespace Symfony\Component\DependencyInjection\Loader\Configurator; | ||
|
||
use Zenstruck\Foundry\InMemory\InMemoryFactoryRegistry; | ||
use Zenstruck\Foundry\InMemory\InMemoryRepositoryRegistry; | ||
|
||
return static function (ContainerConfigurator $container): void { | ||
$container->services() | ||
->set('.zenstruck_foundry.in_memory.factory_registry', InMemoryFactoryRegistry::class) | ||
->decorate('.zenstruck_foundry.factory_registry') | ||
->arg('$decorated', service('.inner')) | ||
->arg('$inMemoryRepositoryRegistry', service('.zenstruck_foundry.in_memory.repository_registry')); | ||
|
||
$container->services() | ||
->set('.zenstruck_foundry.in_memory.repository_registry', InMemoryRepositoryRegistry::class); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Zenstruck\Foundry\Exception; | ||
|
||
/** | ||
* @author Nicolas PHILIPPE <[email protected]> | ||
*/ | ||
final class CannotCreateFactory extends \LogicException | ||
{ | ||
public static function argumentCountError(\ArgumentCountError $e): static | ||
{ | ||
return new self('Factories with dependencies (services) cannot be created before foundry is booted.', previous: $e); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ | |
namespace Zenstruck\Foundry; | ||
|
||
use Faker; | ||
use Zenstruck\Foundry\Exception\CannotCreateFactory; | ||
|
||
/** | ||
* @author Kevin Bond <[email protected]> | ||
|
@@ -33,7 +34,6 @@ public function __construct() | |
{ | ||
} | ||
|
||
|
||
/** | ||
* @param Attributes $attributes | ||
*/ | ||
|
@@ -46,7 +46,7 @@ final public static function new(array|callable $attributes = []): static | |
try { | ||
$factory ??= new static(); // @phpstan-ignore-line | ||
} catch (\ArgumentCountError $e) { | ||
throw new \LogicException('Factories with dependencies (services) cannot be created before foundry is booted.', previous: $e); | ||
throw CannotCreateFactory::argumentCountError($e); | ||
} | ||
|
||
return $factory->initialize()->with($attributes); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,12 +11,14 @@ | |
|
||
namespace Zenstruck\Foundry; | ||
|
||
use Zenstruck\Foundry\Exception\CannotCreateFactory; | ||
|
||
/** | ||
* @author Kevin Bond <[email protected]> | ||
* | ||
* @internal | ||
*/ | ||
final class FactoryRegistry | ||
final class FactoryRegistry implements FactoryRegistryInterface | ||
{ | ||
/** | ||
* @param Factory<mixed>[] $factories | ||
|
@@ -25,21 +27,18 @@ public function __construct(private iterable $factories) | |
{ | ||
} | ||
|
||
/** | ||
* @template T of Factory | ||
* | ||
* @param class-string<T> $class | ||
* | ||
* @return T|null | ||
*/ | ||
public function get(string $class): ?Factory | ||
public function get(string $class): Factory | ||
{ | ||
foreach ($this->factories as $factory) { | ||
if ($class === $factory::class) { | ||
return $factory; // @phpstan-ignore-line | ||
} | ||
} | ||
|
||
return null; | ||
try { | ||
return new $class(); | ||
} catch (\ArgumentCountError $e) { | ||
throw CannotCreateFactory::argumentCountError($e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the zenstruck/foundry package. | ||
* | ||
* (c) Kevin Bond <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Zenstruck\Foundry; | ||
|
||
/** | ||
* @author Nicolas PHILIPPE <[email protected]> | ||
* | ||
* @internal | ||
*/ | ||
interface FactoryRegistryInterface | ||
{ | ||
/** | ||
* @template T of Factory | ||
* | ||
* @param class-string<T> $class | ||
* | ||
* @return T | ||
*/ | ||
public function get(string $class): Factory; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Zenstruck\Foundry\InMemory; | ||
|
||
/** | ||
* @author Nicolas PHILIPPE <[email protected]> | ||
*/ | ||
#[\Attribute(\Attribute::TARGET_CLASS)] | ||
final class AsInMemoryRepository | ||
{ | ||
public function __construct( | ||
public readonly string $class | ||
) | ||
{ | ||
if (!class_exists($this->class)) { | ||
throw new \InvalidArgumentException("Wrong definition for \"AsInMemoryRepository\" attribute: class \"{$this->class}\" does not exist."); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Zenstruck\Foundry\InMemory\DependencyInjection; | ||
|
||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
use Zenstruck\Foundry\InMemory\InMemoryFactoryRegistry; | ||
|
||
/** | ||
* @internal | ||
* @author Nicolas PHILIPPE <[email protected]> | ||
*/ | ||
final class InMemoryCompilerPass implements CompilerPassInterface | ||
{ | ||
public function process(ContainerBuilder $container): void | ||
{ | ||
// create a service locator with all "in memory" repositories, indexed by target class | ||
$inMemoryRepositoriesServices = $container->findTaggedServiceIds('foundry.in_memory.repository'); | ||
$inMemoryRepositoriesLocator = ServiceLocatorTagPass::register( | ||
$container, | ||
array_combine( | ||
array_map( | ||
static function (array $tags) { | ||
if (\count($tags) !== 1) { | ||
throw new \LogicException('Cannot have multiple tags "foundry.in_memory.repository" on a service!'); | ||
} | ||
|
||
return $tags[0]['class'] ?? throw new \LogicException('Invalid tag definition of "foundry.in_memory.repository".'); | ||
}, | ||
array_values($inMemoryRepositoriesServices) | ||
), | ||
array_map( | ||
static fn(string $inMemoryRepositoryId) => new Reference($inMemoryRepositoryId), | ||
array_keys($inMemoryRepositoriesServices) | ||
), | ||
) | ||
); | ||
|
||
// todo: should we check we only have a 1 repository per class? | ||
$container->findDefinition('.zenstruck_foundry.in_memory.repository_registry') | ||
->setArgument('$inMemoryRepositories', $inMemoryRepositoriesLocator) | ||
; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Zenstruck\Foundry\InMemory; | ||
|
||
/** | ||
* @template T of object | ||
* @implements InMemoryRepository<T> | ||
* @author Nicolas PHILIPPE <[email protected]> | ||
* | ||
* This class will be used when a specific "in-memory" repository does not exist for a given class. | ||
*/ | ||
final class GenericInMemoryRepository implements InMemoryRepository | ||
{ | ||
/** | ||
* @var list<T> | ||
*/ | ||
private array $elements = []; | ||
|
||
/** | ||
* @param class-string<T> $class | ||
*/ | ||
public function __construct( | ||
private readonly string $class | ||
) | ||
{ | ||
} | ||
|
||
/** | ||
* @param T $element | ||
*/ | ||
public function _save(object $element): void | ||
{ | ||
if (!$element instanceof $this->class) { | ||
throw new \InvalidArgumentException(sprintf('Given object of class "%s" is not an instance of expected "%s"', $element::class, $this->class)); | ||
} | ||
|
||
if (!in_array($element, $this->elements, true)) { | ||
$this->elements[] = $element; | ||
} | ||
} | ||
|
||
public function _all(): array | ||
{ | ||
return $this->elements; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Zenstruck\Foundry\InMemory; | ||
|
||
use Zenstruck\Foundry\Configuration; | ||
use Zenstruck\Foundry\Factory; | ||
use Zenstruck\Foundry\FactoryRegistryInterface; | ||
use Zenstruck\Foundry\ObjectFactory; | ||
use Zenstruck\Foundry\Persistence\PersistentObjectFactory; | ||
|
||
/** | ||
* @internal | ||
* @template T of object | ||
* @author Nicolas PHILIPPE <[email protected]> | ||
*/ | ||
final class InMemoryFactoryRegistry implements FactoryRegistryInterface | ||
{ | ||
public function __construct( // @phpstan-ignore-line | ||
private readonly FactoryRegistryInterface $decorated, | ||
private readonly InMemoryRepositoryRegistry $inMemoryRepositoryRegistry, | ||
) { | ||
} | ||
|
||
/** | ||
* @template TFactory of Factory | ||
* | ||
* @param class-string<TFactory> $class | ||
* | ||
* @return TFactory | ||
*/ | ||
public function get(string $class): Factory | ||
{ | ||
$factory = $this->decorated->get($class); | ||
|
||
if (!$factory instanceof ObjectFactory || !Configuration::instance()->isInMemoryEnabled()) { | ||
return $factory; | ||
} | ||
|
||
$inMemoryRepository = $this->inMemoryRepositoryRegistry->get($factory::class()); | ||
|
||
if ($factory instanceof PersistentObjectFactory) { | ||
$factory = $factory->withoutPersisting(); | ||
} | ||
|
||
return $factory | ||
->afterInstantiate( | ||
function (object $object) use ($inMemoryRepository) { | ||
$inMemoryRepository->_save($object); | ||
} | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Zenstruck\Foundry\InMemory; | ||
|
||
/** | ||
* @author Nicolas PHILIPPE <[email protected]> | ||
* | ||
* @template T of object | ||
*/ | ||
interface InMemoryRepository | ||
{ | ||
/** | ||
* @param T $element | ||
*/ | ||
public function _save(object $element): void; | ||
|
||
/** | ||
* @return list<T> | ||
*/ | ||
public function _all(): array; | ||
} |
Oops, something went wrong.