-
-
Notifications
You must be signed in to change notification settings - Fork 85
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
feat: "in memory" behavior #590
Open
nikophil
wants to merge
1
commit into
zenstruck:2.x
Choose a base branch
from
nikophil:feat/in-memory-behavior
base: 2.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?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')); | ||
|
||
$container->services() | ||
->set('.zenstruck_foundry.in_memory.repository_registry', InMemoryRepositoryRegistry::class) | ||
->arg('$inMemoryRepositories', abstract_arg('inMemoryRepositories')) | ||
; | ||
}; |
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,26 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* 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\Exception; | ||
|
||
/** | ||
* @author Nicolas PHILIPPE <[email protected]> | ||
* @internal | ||
*/ | ||
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]> | ||
|
@@ -53,7 +54,7 @@ final public static function new(array|callable $attributes = []): static | |
try { | ||
$factory ??= new static(); // @phpstan-ignore new.static | ||
} 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 | ||
|
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 return.type | ||
} | ||
} | ||
|
||
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,36 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* 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\InMemory; | ||
|
||
/** | ||
* @author Nicolas PHILIPPE <[email protected]> | ||
*/ | ||
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD)] | ||
final class AsInMemoryTest | ||
{ | ||
/** | ||
* @param class-string $class | ||
* @internal | ||
*/ | ||
public static function shouldEnableInMemory(string $class, string $method): bool | ||
{ | ||
$classReflection = new \ReflectionClass($class); | ||
|
||
if ($classReflection->getAttributes(static::class)) { | ||
return true; | ||
} | ||
|
||
return (bool) $classReflection->getMethod($method)->getAttributes(static::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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* 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\InMemory; | ||
|
||
final class CannotEnableInMemory extends \LogicException | ||
{ | ||
public static function testIsNotAKernelTestCase(string $testName): self | ||
{ | ||
return new self("{$testName}: Cannot use the #[AsInMemoryTest] attribute without extending KernelTestCase."); | ||
} | ||
|
||
public static function noInMemoryRepositoryRegistry(): self | ||
{ | ||
return new self('Cannot enable "in memory": maybe not in a KernelTestCase?'); | ||
} | ||
} |
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,55 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* 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\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\InMemoryRepository; | ||
|
||
/** | ||
* @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(string $serviceId) use ($container) { | ||
/** @var class-string<InMemoryRepository<object>> $inMemoryRepositoryClass */ | ||
$inMemoryRepositoryClass = $container->getDefinition($serviceId)->getClass() ?? throw new \LogicException("Service \"{$serviceId}\" should have a class."); | ||
|
||
return $inMemoryRepositoryClass::_class(); | ||
}, | ||
\array_keys($inMemoryRepositoriesServices) | ||
), | ||
\array_map( | ||
static fn(string $inMemoryRepositoryId) => new Reference($inMemoryRepositoryId), | ||
\array_keys($inMemoryRepositoriesServices) | ||
), | ||
) | ||
); | ||
|
||
$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,61 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* 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\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 $item | ||
*/ | ||
public function _save(object $item): void | ||
{ | ||
if (!$item instanceof $this->class) { | ||
throw new \InvalidArgumentException(\sprintf('Given object of class "%s" is not an instance of expected "%s"', $item::class, $this->class)); | ||
} | ||
|
||
if (!\in_array($item, $this->elements, true)) { | ||
$this->elements[] = $item; | ||
} | ||
} | ||
|
||
public function _all(): array | ||
{ | ||
return $this->elements; | ||
} | ||
|
||
public static function _class(): string | ||
{ | ||
throw new \BadMethodCallException('This method should not be called on a GenericInMemoryRepository.'); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😍