Skip to content

Commit

Permalink
Do not show entities which have factories already
Browse files Browse the repository at this point in the history
  • Loading branch information
jschaedl committed May 15, 2021
1 parent 6001e7e commit 94c5815
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 5 deletions.
19 changes: 16 additions & 3 deletions src/Bundle/Maker/MakeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <kevinbond@gmail.com>
Expand All @@ -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
Expand Down Expand Up @@ -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();
}
}
}

Expand Down
1 change: 1 addition & 0 deletions src/Bundle/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@

<service id="Zenstruck\Foundry\Bundle\Maker\MakeFactory">
<argument type="service" id="doctrine" />
<argument type="tagged_iterator" tag="foundry.factory" />
<tag name="maker.command" />
</service>

Expand Down
2 changes: 1 addition & 1 deletion src/Bundle/Resources/skeleton/Factory.tpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ protected function initialize(): self
;
}

protected static function getClass(): string
public static function getClass(): string
{
return <?= $entity->getShortName() ?>::class;
}
Expand Down
2 changes: 1 addition & 1 deletion src/ModelFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ final protected function addState($attributes = []): self
}

/** @psalm-return class-string<TModel> */
abstract protected static function getClass(): string;
abstract public static function getClass(): string;

abstract protected function getDefaults(): array;
}

0 comments on commit 94c5815

Please sign in to comment.