From 94c58159ddbed7b25f725abd61409d2e6cdd8eab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Sch=C3=A4dlich?= Date: Sat, 15 May 2021 15:34:16 +0100 Subject: [PATCH] Do not show entities which have factories already --- src/Bundle/Maker/MakeFactory.php | 19 ++++++++++++++++--- src/Bundle/Resources/config/services.xml | 1 + src/Bundle/Resources/skeleton/Factory.tpl.php | 2 +- src/ModelFactory.php | 2 +- 4 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/Bundle/Maker/MakeFactory.php b/src/Bundle/Maker/MakeFactory.php index 4229e77f1..5b7541914 100644 --- a/src/Bundle/Maker/MakeFactory.php +++ b/src/Bundle/Maker/MakeFactory.php @@ -13,6 +13,7 @@ use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; +use Zenstruck\Foundry\ModelFactory; /** * @author Kevin Bond @@ -22,9 +23,19 @@ final class MakeFactory extends AbstractMaker /** @var ManagerRegistry */ private $managerRegistry; - public function __construct(ManagerRegistry $managerRegistry) - { + /** @var string[] */ + private $entitiesWithFactories; + + public function __construct( + ManagerRegistry $managerRegistry, + iterable $factories + ) { $this->managerRegistry = $managerRegistry; + + $factories = \iterator_to_array($factories); + $this->entitiesWithFactories = \array_map(static function(ModelFactory $factory) { + return $factory::getClass(); + }, $factories); } public static function getCommandName(): string @@ -121,7 +132,9 @@ private function entityChoices(): array foreach ($this->managerRegistry->getManagers() as $manager) { foreach ($manager->getMetadataFactory()->getAllMetadata() as $metadata) { - $choices[] = $metadata->getName(); + if (!\in_array($metadata->getName(), $this->entitiesWithFactories, true)) { + $choices[] = $metadata->getName(); + } } } diff --git a/src/Bundle/Resources/config/services.xml b/src/Bundle/Resources/config/services.xml index b133e0a45..307a02ec0 100644 --- a/src/Bundle/Resources/config/services.xml +++ b/src/Bundle/Resources/config/services.xml @@ -38,6 +38,7 @@ + diff --git a/src/Bundle/Resources/skeleton/Factory.tpl.php b/src/Bundle/Resources/skeleton/Factory.tpl.php index b01cc6ec2..7f13377e3 100644 --- a/src/Bundle/Resources/skeleton/Factory.tpl.php +++ b/src/Bundle/Resources/skeleton/Factory.tpl.php @@ -50,7 +50,7 @@ protected function initialize(): self ; } - protected static function getClass(): string + public static function getClass(): string { return getShortName() ?>::class; } diff --git a/src/ModelFactory.php b/src/ModelFactory.php index 8a0850b5d..91ef6a42b 100644 --- a/src/ModelFactory.php +++ b/src/ModelFactory.php @@ -240,7 +240,7 @@ final protected function addState($attributes = []): self } /** @psalm-return class-string */ - abstract protected static function getClass(): string; + abstract public static function getClass(): string; abstract protected function getDefaults(): array; }