Skip to content

make:entity ApiResource question not called when entity pass as an argument #975

Open
@weaverryan

Description

@weaverryan

Described well here: https://symfonycasts.com/screencast/api-platform/api-resource#comment-5541407296

Basically, if you pass the entity class name to make:entity - e.g. bin/console make:entity CheeseListing, then it does NOT ask:

Mark this class as an API Platform resource?

The problem is that, in interact(), if the name argument is passed, we immediately return:

public function interact(InputInterface $input, ConsoleStyle $io, Command $command)
{
if ($input->getArgument('name')) {
return;
}

That should not happen. If the name argument is passed, then:

A) We obviously should not ask for the entity name -

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

B) We should also not ask for it here:
$io->block([
'This command will generate any missing methods (e.g. getters & setters) for a class or all classes in a namespace.',
'To overwrite any existing methods, re-run this command with the --overwrite flag',
], null, 'fg=yellow');
$classOrNamespace = $io->ask('Enter a class or namespace to regenerate', $this->getEntityNamespace(), [Validator::class, 'notBlank']);
$input->setArgument('name', $classOrNamespace);

C) But we should ask about the api-resource and broadcast:
if (
!$input->getOption('api-resource') &&
class_exists(ApiResource::class) &&
!class_exists($this->generator->createClassNameDetails($entityClassName, 'Entity\\')->getFullName())
) {
$description = $command->getDefinition()->getOption('api-resource')->getDescription();
$question = new ConfirmationQuestion($description, false);
$isApiResource = $io->askQuestion($question);
$input->setOption('api-resource', $isApiResource);
}
if (
!$input->getOption('broadcast') &&
class_exists(Broadcast::class) &&
!class_exists($this->generator->createClassNameDetails($entityClassName, 'Entity\\')->getFullName())
) {
$description = $command->getDefinition()->getOption('broadcast')->getDescription();
$question = new ConfirmationQuestion($description, false);
$isBroadcast = $io->askQuestion($question);
$input->setOption('broadcast', $isBroadcast);
}

So basically, this if (

if ($input->getArgument('name')) {
return;
}
) statement should not have a return... the first two sections should probably just be moved inside of it:
if ($input->getOption('regenerate')) {
$io->block([
'This command will generate any missing methods (e.g. getters & setters) for a class or all classes in a namespace.',
'To overwrite any existing methods, re-run this command with the --overwrite flag',
], null, 'fg=yellow');
$classOrNamespace = $io->ask('Enter a class or namespace to regenerate', $this->getEntityNamespace(), [Validator::class, 'notBlank']);
$input->setArgument('name', $classOrNamespace);
return;
}
$argument = $command->getDefinition()->getArgument('name');
$question = $this->createEntityClassQuestion($argument->getDescription());
$entityClassName = $io->askQuestion($question);
$input->setArgument('name', $entityClassName);

Cheers!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions