本文整理汇总了PHP中Symfony\Component\Console\Question\ChoiceQuestion::setValidator方法的典型用法代码示例。如果您正苦于以下问题:PHP ChoiceQuestion::setValidator方法的具体用法?PHP ChoiceQuestion::setValidator怎么用?PHP ChoiceQuestion::setValidator使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Symfony\Component\Console\Question\ChoiceQuestion
的用法示例。
在下文中一共展示了ChoiceQuestion::setValidator方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: interact
protected function interact(InputInterface $input, OutputInterface $output)
{
$helper = $this->getHelper('question');
$this->selectedSchema = $input->getArgument('name');
if (!array_key_exists($this->selectedSchema, $this->schemaList)) {
$question = new ChoiceQuestion('Please select of of the following templates:', array_keys($this->schemaList));
$validator = function ($answer) use($output) {
if (!array_key_exists($answer, $this->schemaList)) {
throw new \RuntimeException('The name of the template not found');
}
return $answer;
};
$question->setValidator($validator);
$question->setMaxAttempts(2);
$this->selectedSchema = $helper->ask($input, $output, $question);
}
$schema = $this->schemaList[$this->selectedSchema];
foreach ($schema->getUndefinedVariables() as $var) {
$question = new Question(sprintf('Please enter value for variable %s:', $var));
$answer = $helper->ask($input, $output, $question);
$schema->setVariable($var, $answer);
}
$dir = $input->getOption('dir');
if (!$dir) {
$dir = str_replace('\\', '/', $schema->getVariable('NAMESPACE'));
}
$this->selectedDir = trim($dir, '/\\');
}
示例2: interact
protected function interact(InputInterface $input, OutputInterface $output)
{
$this->metadata = [];
$dialog = $this->getDialogHelper();
$moduleHandler = $this->getModuleHandler();
$drupal = $this->getDrupalHelper();
$questionHelper = $this->getQuestionHelper();
// --module option
$module = $input->getOption('module');
if (!$module) {
// @see Drupal\Console\Command\ModuleTrait::moduleQuestion
$module = $this->moduleQuestion($output, $dialog);
}
$input->setOption('module', $module);
// --class-name option
$form_id = $input->getOption('form-id');
if (!$form_id) {
$forms = [];
// Get form ids from webprofiler
if ($moduleHandler->moduleExists('webprofiler')) {
$output->writeln('[-] <info>' . $this->trans('commands.generate.form.alter.messages.loading-forms') . '</info>');
$forms = $this->getWebprofilerForms();
}
if (!empty($forms)) {
$form_id = $dialog->askAndValidate($output, $dialog->getQuestion($this->trans('commands.generate.form.alter.options.form-id'), current(array_keys($forms))), function ($form) {
return $form;
}, false, '', array_combine(array_keys($forms), array_keys($forms)));
}
}
if ($moduleHandler->moduleExists('webprofiler') && isset($forms[$form_id])) {
$this->metadata['class'] = $forms[$form_id]['class']['class'];
$this->metadata['method'] = $forms[$form_id]['class']['method'];
$this->metadata['file'] = str_replace($drupal->getRoot(), '', $forms[$form_id]['class']['file']);
$formItems = array_keys($forms[$form_id]['form']);
$question = new ChoiceQuestion($this->trans('commands.generate.form.alter.messages.hide-form-elements'), array_combine($formItems, $formItems), '0');
$question->setMultiselect(true);
$question->setValidator(function ($answer) {
return $answer;
});
$formItemsToHide = $questionHelper->ask($input, $output, $question);
$this->metadata['unset'] = array_filter(array_map('trim', explode(',', $formItemsToHide)));
}
$input->setOption('form-id', $form_id);
$output->writeln($this->trans('commands.generate.form.alter.messages.inputs'));
// @see Drupal\Console\Command\FormTrait::formQuestion
$form = $this->formQuestion($output, $dialog);
$input->setOption('inputs', $form);
}
示例3: interact
/**
* Interacts with the user.
*
* @param InputInterface $input The input
* @param OutputInterface $output The output
*
* @throws \InvalidArgumentException
*
* @return void
*/
protected function interact(InputInterface $input, OutputInterface $output)
{
if (!$input->getArgument('username')) {
$question = new Question('Please choose a username:');
$question->setValidator(function ($username) {
if (null === $username) {
throw new \InvalidArgumentException('Username can not be empty');
}
return $username;
});
$username = $this->getHelper('question')->ask($input, $output, $question);
$input->setArgument('username', $username);
}
if (!$input->getArgument('email')) {
$question = new Question('Please choose an email:');
$question->setValidator(function ($email) {
//TODO: we might want to check if its an actual email address ..?
if (null === $email) {
throw new \InvalidArgumentException('Email can not be empty');
}
return $email;
});
$email = $this->getHelper('question')->ask($input, $output, $question);
$input->setArgument('email', $email);
}
if (!$input->getArgument('password')) {
$question = new Question('Please choose a password:');
$question->setHidden(true);
$question->setHiddenFallback(false);
$question->setValidator(function ($password) {
if (null === $password) {
throw new \InvalidArgumentException('Password can not be empty');
}
return $password;
});
$password = $this->getHelper('question')->ask($input, $output, $question);
$input->setArgument('password', $password);
}
if (!$input->getArgument('locale')) {
$locale = $this->getHelper('question')->ask($input, $output, new Question('Please enter the locale (or leave empty for default admin locale):'));
$input->setArgument('locale', $locale);
}
$groups = $this->getContainer()->get('fos_user.group_manager')->findGroups();
if (!$input->getOption('group')) {
$question = new ChoiceQuestion('Please enter the group(s) the user should be a member of (multiple possible, separated by comma):', $groups, '');
$question->setMultiselect(true);
$question->setValidator(function ($groups) {
if ($groups === '') {
throw new \RuntimeException('Group(s) must be of type integer and can not be empty');
}
return $groups;
});
// Group has to be imploded because $input->setOption expects a string
$groups = $this->getHelper('question')->ask($input, $output, $question);
$input->setOption('group', $groups);
}
}
示例4: execute
protected function execute(InputInterface $input, OutputInterface $output)
{
// -> We check if there is already an app instance, else we
// can not configure git-deployer yet
$hasStorage = false;
try {
// -> We need to throw an error if we're loggin in to another
// service than the currently logged in!
$instance = \GitDeployer\AppInstance::getInstance();
$storageService = $instance->storage();
if (is_object($storageService) && $storageService instanceof \GitDeployer\Storage\BaseStorage) {
$hasStorage = str_replace(array('GitDeployer\\Storage\\', 'Storage'), array('', ''), get_class($storageService));
}
} catch (\Exception $e) {
throw new \RuntimeException('Please log-in to a service first!');
}
$output->writeln(<<<INTRO
Welcome to the configuration wizard for Git-Deployer!
Let's get started!
INTRO
);
// -> Storage service
// Get available storage services to display to the user
$services = \GitDeployer\Storage\BaseStorage::getStorageServicesForHelp();
$output->writeln(<<<INTRO
First, you will need to configure a <info>storage service</info>:
A <info>storage service</info> is responsible of storing Git-Deployer's state,
i such as what repositories are deployed, which version/tag was deployed, where
they are deployed, etc...
The following <info>storage services</info> exist in this build:
<comment>{$services}</comment>
INTRO
);
$services = \GitDeployer\Storage\BaseStorage::getStorageServicesForIterating();
$default = $hasStorage ? array_search($hasStorage, $services) : 0;
$helper = $this->getHelper('question');
// -> Get storage service to use
$question = new ChoiceQuestion('Which storage service would you like to use?', $services, $default);
$question->setValidator(function ($answer) use($services) {
if (!isset($services[$answer])) {
throw new \RuntimeException('Please select a correct value!');
}
return $answer;
});
$number = $helper->ask($input, $output, $question);
// If we selected the already existing storage instance, load
// it up so we can see the already defined values
if ($hasStorage && $number == $default) {
$storageService->setInstances($input, $output, $this->getHelperSet());
} else {
$storageService = \GitDeployer\Storage\BaseStorage::createServiceInstance($services[$number], $input, $output, $this->getHelperSet());
}
// -> Now that we have a storage service, add this to
// the AppInstance, to be opened later again
if ($storageService->configure()) {
$instance->storage($storageService)->save();
}
$output->writeln(<<<OUTRO
This finishes the configuration of Git-Deployer!
Thank you!
OUTRO
);
}
示例5: chooseIncludeExamples
private function chooseIncludeExamples()
{
$default = 'i';
$choices = ['i' => 'Include examples', 'e' => 'Export to a separate file', 's' => 'Skip examples'];
$question = new ChoiceQuestion("Do you want to include code examples (default: {$default})?", $choices, $default);
$question->setMultiselect(false);
$question->setAutocompleterValues(null);
$question->setValidator(function ($val) {
return $val;
});
return $this->helper->ask($this->input, $this->output, $question);
}
示例6: execute
protected function execute(InputInterface $input, OutputInterface $output)
{
$repository = $input->getArgument('repository');
$revision = $input->getArgument('revision');
$configuration = $input->getOption('configuration');
$bag = $input->getOption('bag');
$force = $input->getOption('force-redeploy');
// -> Check that the revision matches what we expect
preg_match('#^(tag|branch|rev):(.*)#iu', $revision, $matches);
if (count($matches) < 3) {
throw new \Exception('You must correctly specify the tag/branch/revision you want to deploy!' . "\n" . 'Check "help deploy" for an explanation.');
} else {
$type = $matches[1];
$version = $matches[2];
}
// -> Get logged-in service
$instance = \GitDeployer\AppInstance::getInstance();
$appService = $instance->service();
$appService->setInstances($input, $output, $this->getHelperSet());
// .. and storage
$storage = $instance->storage();
$hasBeenFound = false;
$this->showMessage('INIT', 'Getting repository...', $output);
foreach ($appService->getProjects() as $key => $project) {
if ($project->name() == $repository) {
$hasBeenFound = true;
$status = $storage->getDeploymentStatus($project);
// -> Check if we have already been added to Git-Deployer yet
if (!$status->added()) {
throw new \Exception('This repository is not yet added to Git-Deployer!' . "\n" . 'Please add it first via the add command.');
}
// -> Check we are not deploying the same version again. Ask
// if we do, so the user can decide
if ($status->isDeployd()) {
if ($status->getDeployedVersion() == $revision && !$force) {
$helper = $this->getHelper('question');
$question = new ConfirmationQuestion('Version ' . $status->getDeployedVersion() . ' was already deployed on ' . $status->deployedWhen() . '. Continue anyway? (y/[n]) ', false);
if (!$helper->ask($input, $output, $question)) {
break;
}
}
}
// -> Now clone it to a temp directory, if it doesn't exist already
$tmpDir = sys_get_temp_dir() . '/git-deploy-' . strtolower($project->name());
if (\Gitonomy\Git\Admin::isValidRepository($tmpDir)) {
$this->showMessage('GIT', 'Pulling latest changes from repository...', $output);
$repository = \Gitonomy\Git\Admin::init($tmpDir, false);
$repository->run('pull');
} else {
$this->showMessage('GIT', 'Cloning repository...', $output);
$repository = \Gitonomy\Git\Admin::cloneTo($tmpDir, $project->url(), false);
}
// -> Check out the correct branch/revision/tag
$this->showMessage('GIT', 'Checking out ' . $revision . '...', $output);
$wc = $repository->getWorkingCopy();
$wc->checkout($version);
// -> Open .deployerfile and parse it
$this->showMessage('DEPLOY', 'Checking .deployerfile...', $output);
if (file_exists($tmpDir . '/.deployerfile')) {
$deployerfile = json_decode(file_get_contents($tmpDir . '/.deployerfile'));
} else {
throw new \Exception('This repository has no .deployerfile!' . "\n" . 'Please add one first!');
}
if ($deployerfile == null) {
throw new \Exception('Could not parse .deployerfile: ' . json_last_error_msg() . "\n" . 'Please check that your JSON is valid!');
}
// -> Load the correct deployer from the .deployerfile, and fire it up with
// the correct configuration options
if (!isset($deployerfile->type)) {
throw new \Exception('Could not parse .deployerfile: ' . "\n" . 'Please specify a deployer via the "type" option!');
}
if (!isset($deployerfile->configurations) || isset($deployerfile->configurations) && !is_object($deployerfile->configurations)) {
throw new \Exception('Could not parse .deployerfile: ' . "\n" . 'Please specify at least one deployment configuration in the "configurations" option!');
}
// -> Get available configurations, and if not set, ask the user
// which configuration to use
$configs = get_object_vars($deployerfile->configurations);
if ($configuration == null && count(get_object_vars($deployerfile->configurations)) > 1) {
// If no configuration has been specified via command line,
// ask the user which one to use
$confignames = array();
foreach ($configs as $name => $values) {
$confignames[] = $name;
}
// -> Get storage service to use
$question = new ChoiceQuestion('Which deployment configuration would you like to use?', $confignames);
$question->setValidator(function ($answer) use($configs, $confignames) {
if (!isset($configs[$confignames[$answer]])) {
throw new \RuntimeException('Please select a correct value!');
}
return $confignames[$answer];
});
$configuration = $helper->ask($input, $output, $question);
} elseif ($configuration != null) {
// Try to use the current specified configuration
if (!isset($configs[$configuration])) {
throw new \Exception('The configuration "' . $configuration . '" was not found in this .deployefile!');
}
} else {
foreach ($configs as $name => $values) {
//.........这里部分代码省略.........