Skip to content

Commit

Permalink
Replace DialogHelper with QuestionHelper
Browse files Browse the repository at this point in the history
  • Loading branch information
KUNSTMAAN committed Apr 10, 2015
1 parent 620d8e2 commit 254d7ee
Show file tree
Hide file tree
Showing 14 changed files with 258 additions and 196 deletions.
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -45,7 +45,7 @@
"symfony-cmf/routing-bundle": "~1.1.1",
"ruflin/elastica": "~1.0",
"fpn/doctrine-extensions-taggable": "0.9.0",
"sensio/generator-bundle": ">=2.3.0, <2.5.0"
"sensio/generator-bundle": ">=2.5.0"
},
"require-dev": {
"behat/behat": "~2.5.0",
Expand Down
42 changes: 24 additions & 18 deletions src/Kunstmaan/GeneratorBundle/Command/GenerateAdminListCommand.php
Expand Up @@ -8,7 +8,9 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Sensio\Bundle\GeneratorBundle\Command\Validators;
use Sensio\Bundle\GeneratorBundle\Command\Helper\DialogHelper;
use Sensio\Bundle\GeneratorBundle\Command\Helper\QuestionHelper;
use Symfony\Component\Console\Question\ConfirmationQuestion;
use Symfony\Component\Console\Question\Question;
use Kunstmaan\GeneratorBundle\Generator\AdminListGenerator;
use Kunstmaan\GeneratorBundle\Helper\GeneratorUtils;

Expand Down Expand Up @@ -47,7 +49,7 @@ protected function configure()
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$dialog = $this->getDialogHelper();
$questionHelper = $this->getQuestionHelper();

GeneratorUtils::ensureOptionsProvided($input, array('entity'));

Expand All @@ -58,16 +60,16 @@ protected function execute(InputInterface $input, OutputInterface $output)
$metadata = $this->getEntityMetadata($entityClass);
$bundle = $this->getContainer()->get('kernel')->getBundle($bundle);

$dialog->writeSection($output, 'AdminList Generation');
$questionHelper->writeSection($output, 'AdminList Generation');

$generator = $this->getGenerator($this->getApplication()->getKernel()->getBundle("KunstmaanGeneratorBundle"));
$generator->setDialog($dialog);
$generator->setQuestion($questionHelper);
$generator->generate($bundle, $entityClass, $metadata[0], $output);

$parts = explode('\\', $entity);
$entityClass = array_pop($parts);

$this->updateRouting($dialog, $input, $output, $bundle, $entityClass);
$this->updateRouting($questionHelper, $input, $output, $bundle, $entityClass);
}

/**
Expand All @@ -78,15 +80,15 @@ protected function execute(InputInterface $input, OutputInterface $output)
*/
protected function interact(InputInterface $input, OutputInterface $output)
{
$dialog = $this->getDialogHelper();
$dialog->writeSection($output, 'Welcome to the Kunstmaan admin list generator');
$questionHelper = $this->getQuestionHelper();
$questionHelper->writeSection($output, 'Welcome to the Kunstmaan admin list generator');

// entity
$entity = null;
try {
$entity = $input->getOption('entity') ? Validators::validateEntityName($input->getOption('entity')) : null;
} catch (\Exception $error) {
$output->writeln($dialog->getHelperSet()->get('formatter')->formatBlock($error->getMessage(), 'error'));
$output->writeln($questionHelper->getHelperSet()->get('formatter')->formatBlock($error->getMessage(), 'error'));
}

if (is_null($entity)) {
Expand All @@ -98,27 +100,31 @@ protected function interact(InputInterface $input, OutputInterface $output)
'',
));

$entity = $dialog->askAndValidate($output, $dialog->getQuestion('The entity shortcut name', $entity), array('Sensio\Bundle\GeneratorBundle\Command\Validators', 'validateEntityName'), false, $entity);
$question = new Question($questionHelper->getQuestion('The entity shortcut name', $entity), $entity);
$question->setValidator(array('Sensio\Bundle\GeneratorBundle\Command\Validators', 'validateEntityName'));
$entity = $questionHelper->ask($input, $output, $question);
$input->setOption('entity', $entity);
}
}

/**
* @param DialogHelper $dialog The dialog helper
* @param InputInterface $input The command input
* @param OutputInterface $output The command output
* @param Bundle $bundle The bundle
* @param string $entityClass The classname of the entity
* @param QuestionHelper $questionHelper The question helper
* @param InputInterface $input The command input
* @param OutputInterface $output The command output
* @param Bundle $bundle The bundle
* @param string $entityClass The classname of the entity
*
* @return void
*/
protected function updateRouting(DialogHelper $dialog, InputInterface $input, OutputInterface $output, Bundle $bundle, $entityClass)
protected function updateRouting(QuestionHelper $questionHelper, InputInterface $input, OutputInterface $output, Bundle $bundle, $entityClass)
{
$auto = true;
$multilang = false;
if ($input->isInteractive()) {
$multilang = $dialog->askConfirmation($output, $dialog->getQuestion('Is it a multilanguage site', 'yes', '?'), true);
$auto = $dialog->askConfirmation($output, $dialog->getQuestion('Do you want to update the routing automatically', 'yes', '?'), true);
$confirmationQuestion = new ConfirmationQuestion($questionHelper->getQuestion('Is it a multilanguage site', 'yes', '?'), true);
$multilang = $questionHelper->ask($input, $output, $confirmationQuestion);
$confirmationQuestion = new ConfirmationQuestion($questionHelper->getQuestion('Do you want to update the routing automatically', 'yes', '?'), true);
$auto = $questionHelper->ask($input, $output, $confirmationQuestion);
}

$prefix = $multilang ? '/{_locale}' : '';
Expand Down Expand Up @@ -146,7 +152,7 @@ protected function updateRouting(DialogHelper $dialog, InputInterface $input, Ou
$content .= $code;

if (false === file_put_contents($file, $content)) {
$output->writeln($dialog->getHelperSet()->get('formatter')->formatBlock("Failed adding the content automatically", 'error'));
$output->writeln($questionHelper->getHelperSet()->get('formatter')->formatBlock("Failed adding the content automatically", 'error'));
} else {
return;
}
Expand Down
Expand Up @@ -43,8 +43,8 @@ protected function configure()
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$dialog = $this->getDialogHelper();
$dialog->writeSection($output, 'Admin Tests Generation');
$questionHelper = $this->getQuestionHelper();
$questionHelper->writeSection($output, 'Admin Tests Generation');

GeneratorUtils::ensureOptionsProvided($input, array('namespace'));

Expand All @@ -65,10 +65,10 @@ protected function execute(InputInterface $input, OutputInterface $output)
*/
protected function interact(InputInterface $input, OutputInterface $output)
{
$dialog = $this->getDialogHelper();
$dialog->writeSection($output, 'Welcome to the Kunstmaan default site generator');
$questionHelper = $this->getQuestionHelper();
$questionHelper->writeSection($output, 'Welcome to the Kunstmaan default site generator');

$inputAssistant = GeneratorUtils::getInputAssistant($input, $output, $dialog, $this->getApplication()->getKernel(), $this->getContainer());
$inputAssistant = GeneratorUtils::getInputAssistant($input, $output, $questionHelper, $this->getApplication()->getKernel(), $this->getContainer());

$inputAssistant->askForNamespace(array(
'',
Expand Down
15 changes: 9 additions & 6 deletions src/Kunstmaan/GeneratorBundle/Command/GenerateArticleCommand.php
Expand Up @@ -6,6 +6,7 @@
use Kunstmaan\GeneratorBundle\Helper\GeneratorUtils;
use Symfony\Component\Console\Input\InputOption;
use Sensio\Bundle\GeneratorBundle\Command\Validators;
use Symfony\Component\Console\Question\Question;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Sensio\Bundle\GeneratorBundle\Command\GenerateDoctrineCommand;
Expand Down Expand Up @@ -55,8 +56,8 @@ protected function configure()
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$dialog = $this->getDialogHelper();
$dialog->writeSection($output, 'Article Generation');
$questionHelper = $this->getQuestionHelper();
$questionHelper->writeSection($output, 'Article Generation');

GeneratorUtils::ensureOptionsProvided($input, array('namespace', 'entity'));

Expand Down Expand Up @@ -89,10 +90,10 @@ protected function execute(InputInterface $input, OutputInterface $output)

protected function interact(InputInterface $input, OutputInterface $output)
{
$dialog = $this->getDialogHelper();
$dialog->writeSection($output, 'Welcome to the Kunstmaan Article generator');
$questionHelper = $this->getQuestionHelper();
$questionHelper->writeSection($output, 'Welcome to the Kunstmaan Article generator');

$inputAssistant = GeneratorUtils::getInputAssistant($input, $output, $dialog, $this->getApplication()->getKernel(), $this->getContainer());
$inputAssistant = GeneratorUtils::getInputAssistant($input, $output, $questionHelper, $this->getApplication()->getKernel(), $this->getContainer());

$inputAssistant->askForNamespace(array(
'',
Expand Down Expand Up @@ -123,7 +124,9 @@ protected function interact(InputInterface $input, OutputInterface $output)
}
};

$entity = $dialog->askAndValidate($output, $dialog->getQuestion('Name', $entity), $entityValidation, false, $entity);
$question = new Question($questionHelper->getQuestion('Name', $entity), $entity);
$question->setValidator($entityValidation);
$entity = $questionHelper->ask($input, $output, $question);
$input->setOption('entity', $entity);
}

Expand Down
75 changes: 40 additions & 35 deletions src/Kunstmaan/GeneratorBundle/Command/GenerateBundleCommand.php
Expand Up @@ -11,7 +11,9 @@
use Sensio\Bundle\GeneratorBundle\Command\Validators;
use Sensio\Bundle\GeneratorBundle\Manipulator\KernelManipulator;
use Sensio\Bundle\GeneratorBundle\Manipulator\RoutingManipulator;
use Sensio\Bundle\GeneratorBundle\Command\Helper\DialogHelper;
use Sensio\Bundle\GeneratorBundle\Command\Helper\QuestionHelper;
use Symfony\Component\Console\Question\Question;
use Symfony\Component\Console\Question\ConfirmationQuestion;

/**
* Generates bundles.
Expand Down Expand Up @@ -65,10 +67,11 @@ protected function configure()
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$dialog = $this->getDialogHelper();
$questionHelper = $this->getQuestionHelper();

if ($input->isInteractive()) {
if (!$dialog->askConfirmation($output, $dialog->getQuestion('Do you confirm generation', 'yes', '?'), true)) {
$confirmationQuestion = new ConfirmationQuestion($questionHelper->getQuestion('Do you confirm generation', 'yes', '?'), true);
if (!$questionHelper->ask($input, $output, $confirmationQuestion)) {
$output->writeln('<error>Command aborted</error>');

return 1;
Expand All @@ -85,7 +88,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$dir = Validators::validateTargetDir($input->getOption('dir'), $bundle, $namespace);
$format = 'yml';

$dialog->writeSection($output, 'Bundle generation');
$questionHelper->writeSection($output, 'Bundle generation');

if (!$this
->getContainer()
Expand All @@ -101,18 +104,18 @@ protected function execute(InputInterface $input, OutputInterface $output)
$output->writeln('Generating the bundle code: <info>OK</info>');

$errors = array();
$runner = $dialog->getRunner($output, $errors);
$runner = $questionHelper->getRunner($output, $errors);

// check that the namespace is already autoloaded
$runner($this->checkAutoloader($output, $namespace, $bundle));

// register the bundle in the Kernel class
$runner($this->updateKernel($dialog, $input, $output, $this->getContainer()->get('kernel'), $namespace, $bundle));
$runner($this->updateKernel($questionHelper, $input, $output, $this->getContainer()->get('kernel'), $namespace, $bundle));

// routing
$runner($this->updateRouting($dialog, $input, $output, $bundle, $format));
$runner($this->updateRouting($questionHelper, $input, $output, $bundle, $format));

$dialog->writeGeneratorSummary($output, $errors);
$questionHelper->writeGeneratorSummary($output, $errors);
}

/**
Expand All @@ -125,8 +128,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
*/
protected function interact(InputInterface $input, OutputInterface $output)
{
$dialog = $this->getDialogHelper();
$dialog->writeSection($output, 'Welcome to the Kunstmaan bundle generator');
$questionHelper = $this->getQuestionHelper();
$questionHelper->writeSection($output, 'Welcome to the Kunstmaan bundle generator');

// namespace
$output
Expand All @@ -138,9 +141,9 @@ protected function interact(InputInterface $input, OutputInterface $output)
'See http://symfony.com/doc/current/cookbook/bundles/best_practices.html#index-1 for more', 'details on bundle naming conventions.', '',
'Use <comment>/</comment> instead of <comment>\\ </comment>for the namespace delimiter to avoid any problems.', '',));

$namespace = $dialog
->askAndValidate($output, $dialog->getQuestion('Bundle namespace', $input->getOption('namespace')),
array('Sensio\Bundle\GeneratorBundle\Command\Validators', 'validateBundleNamespace'), false, $input->getOption('namespace'));
$question = new Question($questionHelper->getQuestion('Bundle namespace', $input->getOption('namespace')), $input->getOption('namespace'));
$question->setValidator( array('Sensio\Bundle\GeneratorBundle\Command\Validators', 'validateBundleNamespace'));
$namespace = $questionHelper->ask($input, $output, $question);
$input->setOption('namespace', $namespace);

// bundle name
Expand All @@ -149,19 +152,19 @@ protected function interact(InputInterface $input, OutputInterface $output)
->writeln(
array('', 'In your code, a bundle is often referenced by its name. It can be the', 'concatenation of all namespace parts but it\'s really up to you to come',
'up with a unique name (a good practice is to start with the vendor name).', 'Based on the namespace, we suggest <comment>' . $bundle . '</comment>.', '',));
$bundle = $dialog->askAndValidate($output, $dialog->getQuestion('Bundle name', $bundle), array('Sensio\Bundle\GeneratorBundle\Command\Validators', 'validateBundleName'), false, $bundle);
$question = new Question($questionHelper->getQuestion('Bundle name', $bundle), $bundle);
$question->setValidator(array('Sensio\Bundle\GeneratorBundle\Command\Validators', 'validateBundleName'));
$bundle = $questionHelper->ask($input, $output, $question);
$input->setOption('bundle-name', $bundle);

// target dir
$dir = $input->getOption('dir') ? : dirname($this
->getContainer()
->getParameter('kernel.root_dir')) . '/src';
$output->writeln(array('', 'The bundle can be generated anywhere. The suggested default directory uses', 'the standard conventions.', '',));
$dir = $dialog
->askAndValidate($output, $dialog->getQuestion('Target directory', $dir),
function ($dir) use ($bundle, $namespace) {
return Validators::validateTargetDir($dir, $bundle, $namespace);
}, false, $dir);
$question = new Question($questionHelper->getQuestion('Target directory', $dir), $dir);
$question->setValidator(function ($dir) use ($bundle, $namespace) { return Validators::validateTargetDir($dir, $bundle, $namespace); });
$dir = $questionHelper->ask($input, $output, $question);
$input->setOption('dir', $dir);

// format
Expand Down Expand Up @@ -195,20 +198,21 @@ protected function checkAutoloader(OutputInterface $output, $namespace, $bundle)
}

/**
* @param DialogHelper $dialog The dialog helper
* @param InputInterface $input The command input
* @param OutputInterface $output The command output
* @param KernelInterface $kernel The kernel
* @param string $namespace The namespace
* @param string $bundle The bundle
* @param QuestionHelper $questionHelper The question helper
* @param InputInterface $input The command input
* @param OutputInterface $output The command output
* @param KernelInterface $kernel The kernel
* @param string $namespace The namespace
* @param string $bundle The bundle
*
* @return array
*/
protected function updateKernel(DialogHelper $dialog, InputInterface $input, OutputInterface $output, KernelInterface $kernel, $namespace, $bundle)
protected function updateKernel(QuestionHelper $questionHelper, InputInterface $input, OutputInterface $output, KernelInterface $kernel, $namespace, $bundle)
{
$auto = true;
if ($input->isInteractive()) {
$auto = $dialog->askConfirmation($output, $dialog->getQuestion('Confirm automatic update of your Kernel', 'yes', '?'), true);
$confirmationQuestion = new ConfirmationQuestion($questionHelper->getQuestion('Confirm automatic update of your Kernel', 'yes', '?'), true);
$auto = $questionHelper->ask($input, $output, $confirmationQuestion);
}

$output->write('Enabling the bundle inside the Kernel: ');
Expand All @@ -228,19 +232,20 @@ protected function updateKernel(DialogHelper $dialog, InputInterface $input, Out
}

/**
* @param DialogHelper $dialog The dialog helper
* @param InputInterface $input The command input
* @param OutputInterface $output The command output
* @param string $bundle The bundle name
* @param string $format the format
* @param QuestionHelper $questionHelper The question helper
* @param InputInterface $input The command input
* @param OutputInterface $output The command output
* @param string $bundle The bundle name
* @param string $format The format
*
* @return array
*/
protected function updateRouting(DialogHelper $dialog, InputInterface $input, OutputInterface $output, $bundle, $format)
protected function updateRouting(QuestionHelper $questionHelper, InputInterface $input, OutputInterface $output, $bundle, $format)
{
$auto = true;
if ($input->isInteractive()) {
$auto = $dialog->askConfirmation($output, $dialog->getQuestion('Confirm automatic update of the Routing', 'yes', '?'), true);
$confirmationQuestion = new ConfirmationQuestion($questionHelper->getQuestion('Confirm automatic update of the Routing', 'yes', '?'), true);
$auto = $questionHelper->ask($input, $output, $confirmationQuestion);
}

$output->write('Importing the bundle routing resource: ');
Expand All @@ -264,4 +269,4 @@ protected function createGenerator()
{
return new BundleGenerator($this->getContainer()->get('filesystem'));
}
}
}

0 comments on commit 254d7ee

Please sign in to comment.