Sensio\Bundle\GeneratorBundle\Command\GenerateBundleCommand::interact PHP Method

interact() protected method

protected interact ( Symfony\Component\Console\Input\InputInterface $input, Symfony\Component\Console\Output\OutputInterface $output )
$input Symfony\Component\Console\Input\InputInterface
$output Symfony\Component\Console\Output\OutputInterface
    protected function interact(InputInterface $input, OutputInterface $output)
    {
        $dialog = $this->getDialogHelper();
        $dialog->writeSection($output, 'Welcome to the Symfony2 bundle generator');

        // namespace
        $output->writeln(array(
            '',
            'Your application code must be written in <comment>bundles</comment>. This command helps',
            'you generate them easily.',
            '',
            'Each bundle is hosted under a namespace (like <comment>Acme/Bundle/BlogBundle</comment>).',
            'The namespace should begin with a "vendor" name like your company name, your',
            'project name, or your client name, followed by one or more optional category',
            'sub-namespaces, and it should end with the bundle name itself',
            '(which must have <comment>Bundle</comment> as a suffix).',
            '',
            'Use <comment>/</comment> instead of <comment>\\</comment> for the namespace delimiter to avoid any problem.',
            '',
        ));

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

        // bundle name
        $bundle = $input->getOption('bundle-name') ?: strtr($namespace, array('\\Bundle\\' => '', '\\' => ''));
        $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);
        $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);
        $input->setOption('dir', $dir);

        // format
        $output->writeln(array(
            '',
            'Determine the format to use for the generated configuration.',
            '',
        ));
        $format = $dialog->askAndValidate($output, $dialog->getQuestion('Configuration format (yml, xml, php, or annotation)', $input->getOption('format')), array('Sensio\Bundle\GeneratorBundle\Command\Validators', 'validateFormat'), false, $input->getOption('format'));
        $input->setOption('format', $format);

        // optional files to generate
        $output->writeln(array(
            '',
            'To help you getting started faster, the command can generate some',
            'code snippets for you.',
            '',
        ));

        $structure = $input->getOption('structure');
        if (!$structure && $dialog->askConfirmation($output, $dialog->getQuestion('Do you want to generate the whole directory structure', 'no', '?'), false)) {
            $structure = true;
        }
        $input->setOption('structure', $structure);

        // summary
        $output->writeln(array(
            '',
            $this->getHelper('formatter')->formatBlock('Summary before generation', 'bg=blue;fg=white', true),
            '',
            sprintf("You are going to generate a \"<info>%s\\%s</info>\" bundle\nin \"<info>%s</info>\" using the \"<info>%s</info>\" format.", $namespace, $bundle, $dir, $format),
            '',
        ));
    }

Usage Example

 protected function interact(InputInterface $input, OutputInterface $output)
 {
     $questionHelper = $this->getQuestionHelper();
     $questionHelper->writeSection($output, 'Welcome to the Symfony2Admingenerator');
     $output->writeln('<comment>Create an admingenerator bundle with generate:bundle</comment>');
     $generator = $input->getOption('generator');
     $question = new ChoiceQuestion('Generator to use (doctrine, doctrine_odm, propel)', array('doctrine', 'doctrine_odm', 'propel'), 0);
     $question->setErrorMessage('Generator to use have to be doctrine, doctrine_odm or propel.');
     $generator = $questionHelper->ask($input, $output, $question);
     $input->setOption('generator', $generator);
     // Model name
     $modelName = $input->getOption('model-name');
     $question = new Question($questionHelper->getQuestion('Model name', $modelName), $modelName);
     $question->setValidator(function ($answer) {
         if (empty($answer) || preg_match('#[^a-zA-Z0-9]#', $answer)) {
             throw new \RuntimeException('Model name should not contain any special characters nor spaces.');
         }
         return $answer;
     });
     $modelName = $questionHelper->ask($input, $output, $question);
     $input->setOption('model-name', $modelName);
     // prefix
     $prefix = $input->getOption('prefix');
     $question = new Question($questionHelper->getQuestion('Prefix of yaml', $prefix), $prefix);
     $question->setValidator(function ($prefix) {
         if (!preg_match('/([a-z]+)/i', $prefix)) {
             throw new \RuntimeException('Prefix have to be a simple word');
         }
         return $prefix;
     });
     $prefix = $questionHelper->ask($input, $output, $question);
     $input->setOption('prefix', $prefix);
     parent::interact($input, $output);
 }
All Usage Examples Of Sensio\Bundle\GeneratorBundle\Command\GenerateBundleCommand::interact