Skip to content

"make:entity" root directory location improvment #265

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public function getConfigTreeBuilder()
$rootNode
->children()
->scalarNode('root_namespace')->defaultValue('App')->end()
->scalarNode('root_entity_directory')->defaultValue('src/Entity/')->end()
->end()
;

Expand Down
5 changes: 5 additions & 0 deletions src/DependencyInjection/MakerExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ public function load(array $configs, ContainerBuilder $container)
$configuration = $this->getConfiguration($configs, $container);
$config = $this->processConfiguration($configuration, $configs);

$rootEntityDirectory = trim($config['root_entity_directory']);

$makeEntityCommandDefinition = $container->getDefinition('maker.maker.make_entity');
$makeEntityCommandDefinition->replaceArgument(3, $rootEntityDirectory);

$rootNamespace = trim($config['root_namespace'], '\\');

$makeCommandDefinition = $container->getDefinition('maker.generator');
Expand Down
23 changes: 13 additions & 10 deletions src/Maker/MakeEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,13 @@ final class MakeEntity extends AbstractMaker implements InputAwareMakerInterface
private $fileManager;
private $doctrineHelper;
private $generator;
private $rootEntityDirectory;

public function __construct(FileManager $fileManager, DoctrineHelper $doctrineHelper, string $projectDirectory, Generator $generator = null)
public function __construct(FileManager $fileManager, DoctrineHelper $doctrineHelper, string $projectDirectory, string $rootEntityDirectory, Generator $generator = null)
{
$this->fileManager = $fileManager;
$this->doctrineHelper = $doctrineHelper;
$this->rootEntityDirectory = $rootEntityDirectory;
// $projectDirectory is unused, argument kept for BC

if (null === $generator) {
Expand All @@ -75,6 +77,7 @@ public function configureCommand(Command $command, InputConfiguration $inputConf
->addOption('api-resource', 'a', InputOption::VALUE_NONE, 'Mark this class as an API Platform resource (expose a CRUD API for it)')
->addOption('regenerate', null, InputOption::VALUE_NONE, 'Instead of adding new fields, simply generate the methods (e.g. getter/setter) for existing fields')
->addOption('overwrite', null, InputOption::VALUE_NONE, 'Overwrite any existing getter/setter methods')
->addOption('rootdir', null, InputOption::VALUE_OPTIONAL, 'Allow you to define a custom root directory, instead of the one by default. Can also be configured with "root_entity_directory" from the bundle conf', $this->rootEntityDirectory)
->setHelp(file_get_contents(__DIR__.'/../Resources/help/MakeEntity.txt'))
;

Expand All @@ -99,7 +102,7 @@ public function interact(InputInterface $input, ConsoleStyle $io, Command $comma
return;
}

$entityFinder = $this->fileManager->createFinder('src/Entity/')
$entityFinder = $this->fileManager->createFinder($input->getOption('rootdir'))
// remove if/when we allow entities in subdirectories
->depth('<1')
->name('*.php');
Expand All @@ -114,7 +117,7 @@ public function interact(InputInterface $input, ConsoleStyle $io, Command $comma
}

$argument = $command->getDefinition()->getArgument('name');
$question = $this->createEntityClassQuestion($argument->getDescription());
$question = $this->createEntityClassQuestion($argument->getDescription(), $input);
$value = $io->askQuestion($question);

$input->setArgument('name', $value);
Expand Down Expand Up @@ -182,7 +185,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen

$isFirstField = true;
while (true) {
$newField = $this->askForNextField($io, $currentFields, $entityClassDetails->getFullName(), $isFirstField);
$newField = $this->askForNextField($input, $io, $currentFields, $entityClassDetails->getFullName(), $isFirstField);
$isFirstField = false;

if (null === $newField) {
Expand Down Expand Up @@ -284,7 +287,7 @@ public function configureDependencies(DependencyBuilder $dependencies, InputInte
ORMDependencyBuilder::buildDependencies($dependencies);
}

private function askForNextField(ConsoleStyle $io, array $fields, string $entityClass, bool $isFirstField)
private function askForNextField(InputInterface $input, ConsoleStyle $io, array $fields, string $entityClass, bool $isFirstField)
{
$io->writeln('');

Expand Down Expand Up @@ -352,7 +355,7 @@ private function askForNextField(ConsoleStyle $io, array $fields, string $entity
}

if ('relation' === $type || \in_array($type, EntityRelation::getValidRelationTypes())) {
return $this->askRelationDetails($io, $entityClass, $type, $fieldName);
return $this->askRelationDetails($input, $io, $entityClass, $type, $fieldName);
}

// this is a normal field
Expand Down Expand Up @@ -453,9 +456,9 @@ private function printAvailableTypes(ConsoleStyle $io)
$printSection($allTypes);
}

private function createEntityClassQuestion(string $questionText): Question
private function createEntityClassQuestion(string $questionText, InputInterface $input): Question
{
$entityFinder = $this->fileManager->createFinder('src/Entity/')
$entityFinder = $this->fileManager->createFinder($input->getOption('rootdir'))
// remove if/when we allow entities in subdirectories
->depth('<1')
->name('*.php');
Expand All @@ -476,12 +479,12 @@ private function createEntityClassQuestion(string $questionText): Question
return $question;
}

private function askRelationDetails(ConsoleStyle $io, string $generatedEntityClass, string $type, string $newFieldName)
private function askRelationDetails(InputInterface $input, ConsoleStyle $io, string $generatedEntityClass, string $type, string $newFieldName)
{
// ask the targetEntity
$targetEntityClass = null;
while (null === $targetEntityClass) {
$question = $this->createEntityClassQuestion('What class should this entity be related to?');
$question = $this->createEntityClassQuestion('What class should this entity be related to?', $input);

$targetEntityClass = $io->askQuestion($question);

Expand Down
1 change: 1 addition & 0 deletions src/Resources/config/makers.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
<argument type="service" id="maker.file_manager" />
<argument type="service" id="maker.doctrine_helper" />
<argument>%kernel.project_dir%</argument>
<argument /> <!-- root entity directory-->
<argument type="service" id="maker.generator" />
<tag name="maker.command" />
</service>
Expand Down