-
-
Notifications
You must be signed in to change notification settings - Fork 560
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
omero
wants to merge
5
commits into
hechoendrupal:master
from
omero:duplicate_code_entity_generator_command
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
fdd8d32
Add GeneratorEntityCommand class
omero afa81af
remove duplicate code in GeneratorEntityContentCommand class and adde…
omero 9742518
remove duplicate code in GeneratorEntityConfigCommand class and added…
omero e1ac985
udpate README.md with the new entity generator commands
omero 9597afb
Merge remote-tracking branch 'upstream/master' into duplicate_code_en…
omero File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
{ | ||
$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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Command class in the configure method not accept parameters
https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Console/Command/Command.php#L147