本文整理汇总了PHP中Sensio\Bundle\GeneratorBundle\Command\Validators::validateTargetDir方法的典型用法代码示例。如果您正苦于以下问题:PHP Validators::validateTargetDir方法的具体用法?PHP Validators::validateTargetDir怎么用?PHP Validators::validateTargetDir使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Sensio\Bundle\GeneratorBundle\Command\Validators
的用法示例。
在下文中一共展示了Validators::validateTargetDir方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: interact
protected function interact(InputInterface $input, OutputInterface $output)
{
$dialog = $this->getDialogHelper();
$dialog->writeSection($output, 'Welcome to the Symfony2 admin generator');
$output->writeln('<comment>Create controllers for a generator module</comment>');
$generator = $dialog->askAndValidate($output, $dialog->getQuestion('Generator to use (doctrine, doctrine_odm, propel)', $input->getOption('generator')), function ($generator) {
if (!in_array($generator, array('doctrine', 'doctrine_odm', 'propel'))) {
throw new \RuntimeException('Generator to use have to be doctrine, doctrine_odm or propel');
}
return $generator;
}, false, $input->getOption('generator'));
$input->setOption('generator', $generator);
$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);
// prefix
$prefix = $dialog->askAndValidate($output, $dialog->getQuestion('Prefix of yaml', $input->getOption('prefix')), function ($prefix) {
if (!preg_match('/([a-z]+)/i', $prefix)) {
throw new \RuntimeException('Prefix have to be a simple word');
}
return $prefix;
}, false, $input->getOption('prefix'));
$input->setOption('prefix', $prefix);
}
示例2: execute
/**
* @see Command
*
* @throws \InvalidArgumentException When namespace doesn't end with Bundle
* @throws \RuntimeException When bundle can't be executed
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$dialog = $this->getDialogHelper();
// @codeCoverageIgnoreStart
if ($input->isInteractive()) {
if (!$dialog->askConfirmation($output, $dialog->getQuestion('Do you confirm generation', 'yes', '?'), true)) {
$output->writeln('<error>Command aborted</error>');
return 1;
}
}
// @codeCoverageIgnoreEnd
foreach (array('namespace', 'dir') as $option) {
if (null === $input->getOption($option)) {
// @codeCoverageIgnoreStart
throw new \RuntimeException(sprintf('The "%s" option must be provided.', $option));
// @codeCoverageIgnoreEnd
}
}
$namespace = Validators::validateBundleNamespace($input->getOption('namespace'));
if (!($bundle = $input->getOption('bundle-name'))) {
$bundle = strtr($namespace, array('\\' => ''));
}
if ($input->getOption('no-strict') == false) {
$this->checkStrictNamespace($namespace);
}
$bundle = Validators::validateBundleName($bundle);
$dir = Validators::validateTargetDir($input->getOption('dir'), $bundle, $namespace);
$format = Validators::validateFormat($input->getOption('format'));
$structure = $input->getOption('structure');
$dialog->writeSection($output, 'Bundle generation');
// @codeCoverageIgnoreStart
if (!$this->getContainer()->get('filesystem')->isAbsolutePath($dir)) {
$dir = getcwd() . '/' . $dir;
}
// @codeCoverageIgnoreEnd
$generator = $this->getGenerator();
$generator->generateExt($namespace, $bundle, $dir, $format, $structure, $this->getGeneratorExtraOptions($input));
$output->writeln('Generating the bundle code: <info>OK</info>');
$errors = array();
$runner = $dialog->getRunner($output, $errors);
// check that the namespace is already autoloaded
$runner($this->checkAutoloader($output, $namespace, $bundle, $dir));
// register the bundle in the Kernel class
if ($this->updateKernel) {
$runner($this->updateKernel($dialog, $input, $output, $this->getContainer()->get('kernel'), $namespace, $bundle));
}
$dialog->writeGeneratorSummary($output, $errors);
}
示例3: execute
/**
* @see Command
*
* @throws \InvalidArgumentException When namespace doesn't end with Bundle
* @throws \RuntimeException When bundle can't be executed
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$helper = $this->getQuestionHelper();
$question = new ConfirmationQuestion('Do you confirm generation?', true);
if ($input->isInteractive()) {
if (!$helper->ask($input, $output, $question)) {
$output->writeln('<error>Command aborted</error>');
return 1;
}
}
foreach (array('namespace', 'short-name') as $option) {
if (null === $input->getOption($option)) {
throw new \RuntimeException(sprintf('The "%s" option must be provided.', $option));
}
}
$namespace = Validators::validateBundleNamespace($input->getOption('namespace'));
$bundle = strtr($namespace, array('\\' => ''));
$bundle = Validators::validateBundleName($bundle);
$dir = realpath(__DIR__ . '/../../../../../../');
if (null !== $input->getOption('dir')) {
$dir = $input->getOption('dir');
}
$dir = Validators::validateTargetDir($dir, $bundle, $namespace);
if (!$this->getContainer()->get('filesystem')->isAbsolutePath($dir)) {
$dir = getcwd() . '/' . $dir;
}
$format = 'yml';
$format = Validators::validateFormat($format);
$helper->writeSection($output, 'Bundle generation');
/** @var ComponentGenerator $generator */
$generator = $this->getGenerator();
$generator->generate($namespace, $bundle, $dir, $format);
$output->writeln('Generating the bundle code: <info>OK</info>');
$errors = array();
$runner = $helper->getRunner($output, $errors);
//update parameters.yml file in vendor
$shortName = $input->getOption('short-name');
$runner($this->updateParameters($output, $shortName, $dir, $namespace, $bundle));
$helper->writeGeneratorSummary($output, $errors);
}
示例4: interact
/**
* Collect options and arguments.
*
* @param InputInterface $input
* @param OutputInterface $output
*
* @return void
*/
protected function interact(InputInterface $input, OutputInterface $output)
{
$questionHelper = $this->getQuestionHelper();
$questionHelper->writeSection($output, 'Welcome to the Victoire widget bundle generator');
///////////////////////
// //
// Create Bundle //
// //
///////////////////////
// namespace
$namespace = null;
try {
$namespace = $input->getOption('namespace') ? Validators::validateBundleNamespace($input->getOption('namespace')) : null;
} catch (\Exception $error) {
$output->writeln($questionHelper->getHelperSet()->get('formatter')->formatBlock($error->getMessage(), 'error'));
}
if (null === $namespace) {
$output->writeln(['', 'Your application code must be written in <comment>widget bundles</comment>. This command helps', 'you generate them easily.', '', 'Each widget is hosted under a namespace (like <comment>Victoire/Widget/YourAwesomeWidgetNameBundle</comment>).', '', 'If you want for example a BlogWidget, the Widget Name should be Blog']);
$question = new Question($questionHelper->getQuestion('Widget name', $input->getOption('bundle-name')));
$question->setValidator(function ($answer) {
return self::validateWidgetName($answer, false);
});
$name = $questionHelper->ask($input, $output, $question);
$bundle = 'VictoireWidget' . $name . 'Bundle';
$input->setOption('bundle-name', $bundle);
$namespace = 'Victoire\\Widget\\' . $name . 'Bundle';
$input->setOption('namespace', $namespace);
}
$orgname = $input->getOption('orgname');
if (null === $orgname) {
$output->writeln(['', 'A composer.json file will be generated, we need to know under which organisation you will publish the widget', '', 'The default organisation will be friendsofvictoire']);
$question = new Question($questionHelper->getQuestion('Under which organisation do you want to publish your widget ?', 'friendsofvictoire'), 'friendsofvictoire');
$orgname = $questionHelper->ask($input, $output, $question);
}
$input->setOption('orgname', $orgname);
$parent = $input->getOption('parent');
$question = new ConfirmationQuestion($questionHelper->getQuestion('Does your widget extends another widget ?', 'no', '?'), false);
if (null === $parent && $questionHelper->ask($input, $output, $question)) {
$output->writeln(['', 'A widget can extends another to reproduce it\'s behavior', '', 'If you wabt to do so, please give the name of the widget to extend', '', 'If you want to extends the TestWidget, the widget name should be Test']);
$question = new Question($questionHelper->getQuestion('Parent widget name', false));
$question->setValidator(function ($answer) {
return self::validateWidgetName($answer, false);
});
$parent = $questionHelper->ask($input, $output, $question);
$input->setOption('parent', $parent);
$packagistParentName = 'friendsofvictoire/' . strtolower($parent) . '-widget';
$question = new Question($questionHelper->getQuestion('Parent widget packagist name', $packagistParentName));
$parent = $questionHelper->ask($input, $output, $question);
$input->setOption('packagist-parent-name', $packagistParentName);
}
$dir = dirname($this->getContainer()->getParameter('kernel.root_dir')) . '/src';
$output->writeln(['', 'The bundle can be generated anywhere. The suggested default directory uses', 'the standard conventions.', '']);
$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
$format = null;
try {
$format = $input->getOption('format') ? Validators::validateFormat($input->getOption('format')) : null;
} catch (\Exception $error) {
$output->writeln($questionHelper->getHelperSet()->get('formatter')->formatBlock($error->getMessage(), 'error'));
}
if (null === $format) {
$output->writeln(['', 'Determine the format to use for the generated configuration.', '']);
$question = new Question($questionHelper->getQuestion('Configuration format (yml, xml, php, or annotation)', 'annotation'), 'annotation');
$question->setValidator(['Sensio\\Bundle\\GeneratorBundle\\Command\\Validators', 'validateFormat']);
$format = $questionHelper->ask($input, $output, $question);
$input->setOption('format', $format);
}
$input->setOption('structure', false);
$contentResolver = $input->getOption('content-resolver');
$question = new ConfirmationQuestion($questionHelper->getQuestion('Do you want to customize widget rendering logic ?', 'no', '?'), false);
if (!$contentResolver && $questionHelper->ask($input, $output, $question)) {
$contentResolver = true;
}
$input->setOption('content-resolver', $contentResolver);
///////////////////////
// //
// Create Entity //
// //
///////////////////////
$input->setOption('fields', $this->addFields($input, $output, $questionHelper));
$entity = 'Widget' . $name;
$input->setOption('entity', $bundle . ':' . $entity);
// summary
$output->writeln(['', $this->getHelper('formatter')->formatBlock('Summary before generation', 'bg=blue;fg=white', true), '', sprintf("You are going to generate a \"<info>%s\\%s</info>\" widget bundle\nin \"<info>%s</info>\" using the \"<info>%s</info>\" format.", $namespace, $bundle, $dir, $format), '']);
}
示例5: interact
protected function interact(InputInterface $input, OutputInterface $output)
{
$questionHelper = $this->getQuestionHelper();
$questionHelper->writeSection($output, 'Welcome to the Symfony2 bundle generator');
// namespace
$namespace = null;
try {
// validate the namespace option (if any) but don't require the vendor namespace
$namespace = $input->getOption('namespace') ? Validators::validateBundleNamespace($input->getOption('namespace'), false) : null;
} catch (\Exception $error) {
$output->writeln($questionHelper->getHelperSet()->get('formatter')->formatBlock($error->getMessage(), 'error'));
}
if (null === $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).', '', '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 problem.', ''));
$acceptedNamespace = false;
while (!$acceptedNamespace) {
$question = new Question($questionHelper->getQuestion('Bundle namespace', $input->getOption('namespace')), $input->getOption('namespace'));
$question->setValidator(function ($answer) {
return Validators::validateBundleNamespace($answer, false);
});
$namespace = $questionHelper->ask($input, $output, $question);
// mark as accepted, unless they want to try again below
$acceptedNamespace = true;
// see if there is a vendor namespace. If not, this could be accidental
if (false === strpos($namespace, '\\')) {
// language is (almost) duplicated in Validators
$msg = array();
$msg[] = '';
$msg[] = sprintf('The namespace sometimes contain a vendor namespace (e.g. <info>VendorName/BlogBundle</info> instead of simply <info>%s</info>).', $namespace, $namespace);
$msg[] = 'If you\'ve *did* type a vendor namespace, try using a forward slash <info>/</info> (<info>Acme/BlogBundle</info>)?';
$msg[] = '';
$output->writeln($msg);
$question = new ConfirmationQuestion($questionHelper->getQuestion(sprintf('Keep <comment>%s</comment> as the bundle namespace (choose no to try again)?', $namespace), 'yes'), true);
$acceptedNamespace = $questionHelper->ask($input, $output, $question);
}
}
$input->setOption('namespace', $namespace);
}
// bundle name
$bundle = null;
try {
$bundle = $input->getOption('bundle-name') ? Validators::validateBundleName($input->getOption('bundle-name')) : null;
} catch (\Exception $error) {
$output->writeln($questionHelper->getHelperSet()->get('formatter')->formatBlock($error->getMessage(), 'error'));
}
if (null === $bundle) {
$bundle = 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>.', ''));
$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 = null;
try {
$dir = $input->getOption('dir') ? Validators::validateTargetDir($input->getOption('dir'), $bundle, $namespace) : null;
} catch (\Exception $error) {
$output->writeln($questionHelper->getHelperSet()->get('formatter')->formatBlock($error->getMessage(), 'error'));
}
if (null === $dir) {
$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.', ''));
$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
$format = null;
try {
$format = $input->getOption('format') ? Validators::validateFormat($input->getOption('format')) : null;
} catch (\Exception $error) {
$output->writeln($questionHelper->getHelperSet()->get('formatter')->formatBlock($error->getMessage(), 'error'));
}
if (null === $format) {
$output->writeln(array('', 'Determine the format to use for the generated configuration.', ''));
$question = new Question($questionHelper->getQuestion('Configuration format (yml, xml, php, or annotation)', $input->getOption('format')), $input->getOption('format'));
$question->setValidator(array('Sensio\\Bundle\\GeneratorBundle\\Command\\Validators', 'validateFormat'));
$format = $questionHelper->ask($input, $output, $question);
$input->setOption('format', $format);
}
// optional files to generate
$output->writeln(array('', 'To help you get started faster, the command can generate some', 'code snippets for you.', ''));
$structure = $input->getOption('structure');
$question = new ConfirmationQuestion($questionHelper->getQuestion('Do you want to generate the whole directory structure', 'no', '?'), false);
if (!$structure && $questionHelper->ask($input, $output, $question)) {
$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), ''));
}
示例6: interact
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).', '', '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 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), ''));
}
示例7: execute
/**
* @see Command
*
* @throws \InvalidArgumentException When namespace doesn't end with Bundle
* @throws \RuntimeException When bundle can't be executed
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$questionHelper = $this->getQuestionHelper();
if ($input->isInteractive()) {
$question = new ConfirmationQuestion('Do you confirm generation?', true);
if (!$questionHelper->ask($input, $output, $question)) {
$output->writeln('<error>Command aborted</error>');
return 1;
}
}
foreach (array('namespace', 'dir') as $option) {
if (null === $input->getOption($option)) {
throw new \RuntimeException(sprintf('The "%s" option must be provided.', $option));
}
}
$namespace = Validators::validateBundleNamespace($input->getOption('namespace'));
if (!($bundle = $input->getOption('bundle-name'))) {
$bundle = strtr($namespace, array('\\' => ''));
}
$bundle = Validators::validateBundleName($bundle);
$dir = Validators::validateTargetDir($input->getOption('dir'), $bundle, $namespace);
$format = Validators::validateFormat($input->getOption('format'));
$structure = $input->getOption('structure');
$questionHelper->writeSection($output, 'Bundle generation');
if (!$this->getContainer()->get('filesystem')->isAbsolutePath($dir)) {
$dir = getcwd() . '/' . $dir;
}
$generatorName = $input->getOption('generator');
$modelName = $input->getOption('model-name');
$generator = $this->createGenerator();
$generator->setGenerator($generatorName);
$generator->setPrefix($input->getOption('prefix'));
$generator->generate($namespace, $bundle, $dir, $format, $structure, $generatorName, $modelName);
$output->writeln('Generating the bundle code: <info>OK</info>');
$errors = array();
$runner = $questionHelper->getRunner($output, $errors);
// check that the namespace is already autoloaded
$runner($this->checkAutoloader($output, $namespace, $bundle, $dir));
// register the bundle in the Kernel class
$runner($this->updateKernel($questionHelper, $input, $output, $this->getContainer()->get('kernel'), $namespace, $bundle));
// routing
$runner($this->updateRouting($questionHelper, $input, $output, $bundle, $format));
$questionHelper->writeGeneratorSummary($output, $errors);
}
示例8: interact
protected function interact(InputInterface $input, OutputInterface $output)
{
$questionHelper = $this->getQuestionHelper();
$questionHelper->writeSection($output, 'Welcome to the Symfony2Admingenerator');
$output->writeln('<comment>Create controllers for a generator module</comment>');
$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);
$namespace = null;
try {
// validate the namespace option (if any) but don't require the vendor namespace
$namespace = $input->getOption('namespace') ? Validators::validateBundleNamespace($input->getOption('namespace'), false) : null;
} catch (\Exception $error) {
$output->writeln($questionHelper->getHelperSet()->get('formatter')->formatBlock($error->getMessage(), 'error'));
}
if (null === $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).', '', '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 problem.', ''));
$acceptedNamespace = false;
while (!$acceptedNamespace) {
$question = new Question($questionHelper->getQuestion('Bundle namespace', $input->getOption('namespace')), $input->getOption('namespace'));
$question->setValidator(function ($answer) {
return Validators::validateBundleNamespace($answer, false);
});
$namespace = $questionHelper->ask($input, $output, $question);
// mark as accepted, unless they want to try again below
$acceptedNamespace = true;
// see if there is a vendor namespace. If not, this could be accidental
if (false === strpos($namespace, '\\')) {
// language is (almost) duplicated in Validators
$msg = array();
$msg[] = '';
$msg[] = sprintf('The namespace sometimes contain a vendor namespace (e.g. <info>VendorName/BlogBundle</info> instead of simply <info>%s</info>).', $namespace, $namespace);
$msg[] = 'If you\'ve *did* type a vendor namespace, try using a forward slash <info>/</info> (<info>Acme/BlogBundle</info>)?';
$msg[] = '';
$output->writeln($msg);
$question = new ConfirmationQuestion($questionHelper->getQuestion(sprintf('Keep <comment>%s</comment> as the bundle namespace (choose no to try again)?', $namespace), 'yes'), true);
$acceptedNamespace = $questionHelper->ask($input, $output, $question);
}
}
$input->setOption('namespace', $namespace);
}
// 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);
// bundle name
$bundle = null;
try {
$bundle = $input->getOption('bundle-name') ? Validators::validateBundleName($input->getOption('bundle-name')) : null;
} catch (\Exception $error) {
$output->writeln($questionHelper->getHelperSet()->get('formatter')->formatBlock($error->getMessage(), 'error'));
}
if (null === $bundle) {
$bundle = 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>.', ''));
$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 = null;
try {
$dir = $input->getOption('dir') ? Validators::validateTargetDir($input->getOption('dir'), $bundle, $namespace) : null;
} catch (\Exception $error) {
$output->writeln($questionHelper->getHelperSet()->get('formatter')->formatBlock($error->getMessage(), 'error'));
}
if (null === $dir) {
$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.', ''));
$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
$format = null;
try {
$format = $input->getOption('format') ? Validators::validateFormat($input->getOption('format')) : null;
} catch (\Exception $error) {
$output->writeln($questionHelper->getHelperSet()->get('formatter')->formatBlock($error->getMessage(), 'error'));
}
if (null === $format) {
$output->writeln(array('', 'Determine the format to use for the generated configuration.', ''));
$question = new Question($questionHelper->getQuestion('Configuration format (yml, xml, php, or annotation)', $input->getOption('format')), $input->getOption('format'));
$question->setValidator(array('Sensio\\Bundle\\GeneratorBundle\\Command\\Validators', 'validateFormat'));
$format = $questionHelper->ask($input, $output, $question);
$input->setOption('format', $format);
}
// prefix
//.........这里部分代码省略.........