Skip to content
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

Rafactoring and remove duplicate code in entity generator command #183

Closed
wants to merge 5 commits 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ $ ./bin/console --shell
| generator:module | router:debug | container:debug | drush
| generator:controller | router:rebuild | |
| generator:form:config | | |
| generator:entity:config | | |
| generator:entity:content | | |
| generator:command | | |
| generator:plugin:block | | |
| generator:plugin:imageeffect | | |
Expand Down
116 changes: 116 additions & 0 deletions src/Command/GeneratorEntityCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php
/**
* @file
* Contains \Drupal\AppConsole\Command\GeneratorEntityCommand.
*/

namespace Drupal\AppConsole\Command;

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Drupal\AppConsole\Command\Helper\ModuleTrait;
use Drupal\AppConsole\Generator\EntityConfigGenerator;
use Drupal\AppConsole\Generator\EntityContentGenerator;

abstract class GeneratorEntityCommand extends GeneratorCommand
{
use ModuleTrait;

protected $entity_type;

/**
* {@inheritdoc}
*/
protected function configure($entity_type, $command_name)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{
$this->entity_type = $entity_type;
$this
->setDefinition(array(
new InputOption('module',null,InputOption::VALUE_REQUIRED, 'The name of the module'),
new InputOption('entity-class',null,InputOption::VALUE_REQUIRED, 'The entity class name'),
new InputOption('entity-name',null,InputOption::VALUE_REQUIRED, 'The name of the entity'),
))
->setName($command_name)
->setDescription('Generate '.$entity_type)
->setHelp('The <info>'.$command_name.'</info> command helps you generate a new '. $entity_type);
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$dialog = $this->getDialogHelper();

$module = $input->getOption('module');
$entity_class = $input->getOption('entity-class');
$entity_name = $input->getOption('entity-name');

$entity_type = $this->getStringUtils()->camelCaseToUnderscore($this->entity_type);

$this
->getGenerator()
->generate($module, $entity_name, $entity_class, $entity_type);

$errors = [];
$dialog->writeGeneratorSummary($output, $errors);
}


/**
* {@inheritdoc}
*/
protected function interact(InputInterface $input, OutputInterface $output)
{
$dialog = $this->getDialogHelper();
$dialog->writeSection($output, 'Welcome to the Drupal entity generator');
$utils = $this->getStringUtils();

// --module option
$module = $input->getOption('module');
if (!$module) {
// @see Drupal\AppConsole\Command\Helper\ModuleTrait::moduleQuestion
$module = $this->moduleQuestion($output, $dialog);
}
$input->setOption('module', $module);

// --entity-class option
$entity_class = $input->getOption('entity-class');
if (!$entity_class) {
$entity_class = $dialog->ask(
$output,
$dialog->getQuestion('Enter the entity class name', 'DefaultEntity'),
'DefaultEntity',
null
);
}
$input->setOption('entity-class', $entity_class);

$machine_name = $utils->camelCaseToMachineName($entity_class);

// --entity-name option
$entity_name = $input->getOption('entity-name');
if (!$entity_name) {
$entity_name = $dialog->ask(
$output,
$dialog->getQuestion('Enter the entity name', $machine_name),
$machine_name,
null
);
}
$input->setOption('entity-name', $entity_name);
}


protected function createGenerator()
{
switch ($entity_name) {
case 'entity_content':
$generator = new EntityContentGenerator;
break;
default:
$generator = new EntityConfigGenerator;
break;
}

return $generator;
}
}
94 changes: 2 additions & 92 deletions src/Command/GeneratorEntityConfigCommand.php
Original file line number Diff line number Diff line change
@@ -1,101 +1,11 @@
<?php
/**
* @file
* Contains \Drupal\AppConsole\Command\GeneratorEntityCommand.
*/

namespace Drupal\AppConsole\Command;

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Drupal\AppConsole\Command\Helper\ModuleTrait;
use Drupal\AppConsole\Generator\EntityConfigGenerator;
class GeneratorEntityConfigCommand extends GeneratorEntityCommand {

class GeneratorEntityConfigCommand extends GeneratorCommand
{
use ModuleTrait;

/**
* {@inheritdoc}
*/
protected function configure()
{
$this
->setDefinition(array(
new InputOption('module',null,InputOption::VALUE_REQUIRED, 'The name of the module'),
new InputOption('entity-class',null,InputOption::VALUE_REQUIRED, 'The entity class name'),
new InputOption('entity-name',null,InputOption::VALUE_REQUIRED, 'The name of the entity'),
))
->setName('generate:entity:config')
->setDescription('Generate entity configuration')
->setHelp('The <info>generate:entity:config</info> command helps you generate a new entity.');
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$dialog = $this->getDialogHelper();

$module = $input->getOption('module');
$entity_class = $input->getOption('entity-class');
$entity_name = $input->getOption('entity-name');

$this
->getGenerator()
->generate($module, $entity_name, $entity_class);

$errors = [];
$dialog->writeGeneratorSummary($output, $errors);
}


/**
* {@inheritdoc}
*/
protected function interact(InputInterface $input, OutputInterface $output)
{
$dialog = $this->getDialogHelper();
$dialog->writeSection($output, 'Welcome to the Drupal entity generator');
$utils = $this->getStringUtils();

// --module option
$module = $input->getOption('module');
if (!$module) {
// @see Drupal\AppConsole\Command\Helper\ModuleTrait::moduleQuestion
$module = $this->moduleQuestion($output, $dialog);
}
$input->setOption('module', $module);

// --entity-class option
$entity_class = $input->getOption('entity-class');
if (!$entity_class) {
$entity_class = $dialog->ask(
$output,
$dialog->getQuestion('Enter the entity class name', 'DefaultEntity'),
'DefaultEntity',
null
);
}
$input->setOption('entity-class', $entity_class);

$machine_name = $utils->camelCaseToMachineName($entity_class);

// --entity-name option
$entity_name = $input->getOption('entity-name');
if (!$entity_name) {
$entity_name = $dialog->ask(
$output,
$dialog->getQuestion('Enter the entity name', $machine_name),
$machine_name,
null
);
}
$input->setOption('entity-name', $entity_name);
}


protected function createGenerator()
{
return new EntityConfigGenerator();
parent::configure('EntityConfig', 'generate:entity:config');
}
}
94 changes: 2 additions & 92 deletions src/Command/GeneratorEntityContentCommand.php
Original file line number Diff line number Diff line change
@@ -1,101 +1,11 @@
<?php
/**
* @file
* Contains \Drupal\AppConsole\Command\GeneratorEntityContentCommand.
*/

namespace Drupal\AppConsole\Command;

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Drupal\AppConsole\Command\Helper\ModuleTrait;
use Drupal\AppConsole\Generator\EntityContentGenerator;
class GeneratorEntityContentCommand extends GeneratorEntityCommand {

class GeneratorEntityContentCommand extends GeneratorCommand
{
use ModuleTrait;

/**
* {@inheritdoc}
*/
protected function configure()
{
$this
->setDefinition(array(
new InputOption('module',null,InputOption::VALUE_REQUIRED, 'The name of the module'),
new InputOption('entity-class',null,InputOption::VALUE_REQUIRED, 'The entity class name'),
new InputOption('entity-name',null,InputOption::VALUE_REQUIRED, 'The name of the entity'),
))
->setName('generate:entity:content')
->setDescription('Generate entity configuration')
->setHelp('The <info>generate:entity:content</info> command helps you generate a new entity.');
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$dialog = $this->getDialogHelper();

$module = $input->getOption('module');
$entity_class = $input->getOption('entity-class');
$entity_name = $input->getOption('entity-name');

$this
->getGenerator()
->generate($module, $entity_name, $entity_class);

$errors = [];
$dialog->writeGeneratorSummary($output, $errors);
}


/**
* {@inheritdoc}
*/
protected function interact(InputInterface $input, OutputInterface $output)
{
$dialog = $this->getDialogHelper();
$dialog->writeSection($output, 'Welcome to the Drupal content entity generator');
$utils = $this->getStringUtils();

// --module option
$module = $input->getOption('module');
if (!$module) {
// @see Drupal\AppConsole\Command\Helper\ModuleTrait::moduleQuestion
$module = $this->moduleQuestion($output, $dialog);
}
$input->setOption('module', $module);

// --entity-class option
$entity_class = $input->getOption('entity-class');
if (!$entity_class) {
$entity_class = $dialog->ask(
$output,
$dialog->getQuestion('Enter the entity class name', 'DefaultEntity'),
'DefaultEntity',
null
);
}
$input->setOption('entity-class', $entity_class);

$machine_name = $utils->camelCaseToMachineName($entity_class);

// --entity-name option
$entity_name = $input->getOption('entity-name');
if (!$entity_name) {
$entity_name = $dialog->ask(
$output,
$dialog->getQuestion('Enter the entity name', $machine_name),
$machine_name,
null
);
}
$input->setOption('entity-name', $entity_name);
}


protected function createGenerator()
{
return new EntityContentGenerator();
parent::configure('EntityContent', 'generate:entity:content');
}
}