-
-
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
16 changed files
with
427 additions
and
14 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
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,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Zenstruck\Foundry\Exception; | ||
|
||
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
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,29 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the zenstruck/foundry package. | ||
* | ||
* (c) Kevin Bond <kevinbond@gmail.com> | ||
* | ||
* 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 <nikophil@gmail.com> | ||
* | ||
* @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,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Zenstruck\Foundry\InMemory; | ||
|
||
// todo: remove this attribute in favor to interface? | ||
#[\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,51 @@ | ||
<?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 | ||
*/ | ||
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->register('.zenstruck_foundry.in_memory.factory_registry') | ||
->setClass(InMemoryFactoryRegistry::class) | ||
->setDecoratedService('.zenstruck_foundry.factory_registry') | ||
->setArgument('$decorated', $container->getDefinition('.zenstruck_foundry.factory_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,69 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Zenstruck\Foundry\InMemory; | ||
|
||
use Symfony\Component\DependencyInjection\ServiceLocator; | ||
use Zenstruck\Foundry\Configuration; | ||
use Zenstruck\Foundry\Factory; | ||
use Zenstruck\Foundry\FactoryRegistryInterface; | ||
use Zenstruck\Foundry\Persistence\PersistentObjectFactory; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class InMemoryFactoryRegistry implements FactoryRegistryInterface | ||
{ | ||
public function __construct( // @phpstan-ignore-line | ||
private readonly FactoryRegistryInterface $decorated, | ||
/** @var ServiceLocator<InMemoryRepository> */ | ||
private readonly ServiceLocator $inMemoryRepositories, | ||
) { | ||
} | ||
|
||
public function get(string $class): Factory | ||
{ | ||
$factory = $this->decorated->get($class); | ||
|
||
if (!$factory instanceof PersistentObjectFactory) { | ||
return $factory; | ||
} | ||
|
||
$configuration = Configuration::instance(); | ||
|
||
if (!$configuration->isInMemoryEnabled()) { | ||
return $factory; | ||
} | ||
|
||
$factory = $factory->withoutPersisting(); | ||
|
||
if ($inMemoryRepository = $this->findInMemoryRepository($factory)) { | ||
$factory = $factory->afterInstantiate( | ||
static fn(object $object) => $inMemoryRepository->_save($object) | ||
); | ||
} | ||
|
||
return $factory->withoutPersisting(); | ||
} | ||
|
||
/** | ||
* @template T of object | ||
* | ||
* @param PersistentObjectFactory<T> $factory | ||
* | ||
* @return InMemoryRepository<T>|null | ||
*/ | ||
private function findInMemoryRepository(PersistentObjectFactory $factory): InMemoryRepository|null | ||
{ | ||
$targetClass = $factory::class(); | ||
if (!$this->inMemoryRepositories->has($targetClass)) { | ||
// todo: should we return null if no "in-memory" repository? | ||
// ... or throw an exception? | ||
// ... or provide a `GenericInMemoryRepository`? | ||
return null; | ||
} | ||
|
||
return $this->inMemoryRepositories->get($targetClass); | ||
} | ||
} |
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\InMemory; | ||
|
||
/** | ||
* @template T of object | ||
*/ | ||
interface InMemoryRepository | ||
{ | ||
/** | ||
* @param T $element | ||
*/ | ||
public function _save(object $element): void; | ||
} |
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,22 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the zenstruck/foundry package. | ||
* | ||
* (c) Kevin Bond <kevinbond@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Zenstruck\Foundry\InMemory; | ||
|
||
use Zenstruck\Foundry\Configuration; | ||
|
||
/** | ||
* Enable "in memory" repositories globally. | ||
*/ | ||
function enable_in_memory(): void | ||
{ | ||
Configuration::instance()->enableInMemory(); | ||
} |
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
Oops, something went wrong.