本文整理匯總了PHP中Symfony\Component\Console\Helper\DialogHelper::askAndValidate方法的典型用法代碼示例。如果您正苦於以下問題:PHP DialogHelper::askAndValidate方法的具體用法?PHP DialogHelper::askAndValidate怎麽用?PHP DialogHelper::askAndValidate使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類Symfony\Component\Console\Helper\DialogHelper
的用法示例。
在下文中一共展示了DialogHelper::askAndValidate方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。
示例1: ask
public function ask($question, $regex, $errorText = null, $default = null)
{
if ($default) {
$question = "{$question} [{$default}]";
}
return $this->dialog->askAndValidate($this->output, "<question>{$question}</question> ", $this->validateWith($regex, $errorText), false, $default);
}
示例2: configure
/**
* Asks user what the path to javascript source is.
*/
public function configure()
{
if (!$this->settings['enableJsHint']) {
return;
}
$baseDir = $this->settings->getBaseDir();
$default = $this->settings->getDefaultValueFor('javaScriptSrcPath', 'src');
$this->settings['javaScriptSrcPath'] = $this->dialog->askAndValidate($this->output, "What is the path to the JavaScript source code? [{$default}] ", function ($data) use($baseDir) {
if (is_dir($baseDir . '/' . $data)) {
return $data;
}
throw new \Exception("That path doesn't exist");
}, false, $default);
}
示例3: askEnable
/**
* Asks in the commandline if the user want to enable the codesniffer for the given project
*
*/
protected function askEnable()
{
$default = $this->settings->getDefaultValueFor('enablePhpCodeSniffer', true);
$this->settings['enablePhpCodeSniffer'] = $this->dialog->askConfirmation($this->output, "Do you want to enable the PHP Code Sniffer?", $default);
if (!$this->settings['enablePhpCodeSniffer']) {
return;
}
$default = $this->settings->getDefaultValueFor('phpCodeSnifferCodingStyle', 'PSR2');
$this->settings['phpCodeSnifferCodingStyle'] = $this->dialog->askAndValidate($this->output, " - Which coding standard do you want to use? (PEAR, PHPCS, PSR1, PSR2, Squiz, Zend) [{$default}] ", function ($data) {
if (in_array($data, array("PEAR", "PHPCS", "PSR1", "PSR2", "Squiz", "Zend"))) {
return $data;
}
throw new \Exception("That coding style is not supported");
}, false, $default);
}
示例4: execute
/**
* @param InputInterface $input
* @param OutputInterface $output
* @return int
*/
protected final function execute(InputInterface $input, OutputInterface $output)
{
/* @var FormatterHelper $formatterHelper */
$formatterHelper = $this->getHelperSet()->get('formatter');
$that = $this;
// Main exception catching to allow quiting command
try {
while (true) {
// sub exception catching to display error without giving up context changes
try {
// Do not display available commands
$commandToExecute = $this->dialog->askAndValidate($output, 'loop > ', function ($answer) use($that) {
// command accepts arguments, lets explode it and keep the trailing args in a simple string
$allArgs = explode(' ', $answer, 2);
if (empty($allArgs[0])) {
return array();
} elseif (array_key_exists($allArgs[0], $this->commands)) {
$command = array_shift($allArgs);
$arguments = !empty($allArgs) ? $allArgs[0] : null;
// check if args are required, optional, or shouldn't exist
if (empty($arguments) && $this->commands[$command]['mode'] == self::COMMAND_VALUE_REQUIRED) {
throw new \RuntimeException('The command "' . $command . '" needs one or many arguments, nothing given.');
} elseif (!empty($arguments) && $this->commands[$command]['mode'] == self::COMMAND_VALUE_NONE) {
throw new \RuntimeException('The command "' . $command . '" doesn\'t accept arguments, "' . $arguments . '" given.');
}
return array('command' => $command, 'arguments' => $arguments);
} else {
throw new \RuntimeException('Unknown command');
}
}, false, null, array_keys($that->commands));
if (!empty($commandToExecute)) {
call_user_func_array($this->commands[$commandToExecute['command']]['callable'], array($input, $output, $this->dialog, $this->context, $commandToExecute['arguments']));
}
} catch (LoopCommandFinishedException $lcfe) {
throw $lcfe;
} catch (\Exception $e) {
$output->writeln($formatterHelper->formatBlock('Exception ' . get_class($e) . ': ' . $e->getMessage(), 'error', true));
}
}
} catch (LoopCommandFinishedException $lcfe) {
// Nothing to do but return with a message
$output->writeln('<comment>Bye</comment>');
return 0;
}
// we shouldn't be able to get there
return 10;
}
示例5: askBaseDifferentEnvironment
/**
* Ask Base url for an different environment environment
*
* @param OutputInterface $output
* @param string $environment
*/
protected function askBaseDifferentEnvironment(OutputInterface $output, $environment)
{
$default = $this->settings->getDefaultValueFor('baseUrl' . $environment, $this->suggestDomain($this->settings['baseUrl'], strtolower($environment)));
$this->settings['baseUrl' . $environment] = $this->dialog->askAndValidate($output, "What is base url of the " . strtolower($environment) . " environment? [{$default}] ", function ($data) {
if (substr($data, 0, 4) == 'http') {
return $data;
}
throw new \Exception("Url needs to start with http");
}, false, $default);
}
示例6: confirmOperation
/**
* Ask and validate operation by typing MASTER release_code
*/
protected function confirmOperation()
{
$that = $this;
$bundle = $this->dialog->askAndValidate($this->output, "\nConfirm operation by typing MASTER release code: ", function ($answer) use($that) {
if ($that->master_branch !== trim($answer)) {
throw new \RunTimeException("Wrong release code: {$answer}");
}
return $answer;
});
}
示例7: getValues
/**
* @param OutputInterface $output
* @param DialogHelper $dialog
* @return array
*/
public function getValues(OutputInterface $output, DialogHelper $dialog)
{
$defaults = $this->getDefaults();
$value = $dialog->askAndValidate($output, "<info>PHP Namespace</info> ({$defaults[0]}): ", function ($namespace) {
if (preg_match("/^([\\w\\\\]+)*\\w+\$/", $namespace)) {
return $namespace;
}
throw new RuntimeException(sprintf('%s is not a valid namespace', $namespace));
}, false, reset($defaults), $defaults);
return ['php_namespace' => $value, 'php_namespace_escaped' => addslashes($value)];
}
示例8: getValues
/**
* @param OutputInterface $output
* @param DialogHelper $dialog
* @return array
*/
public function getValues(OutputInterface $output, DialogHelper $dialog)
{
$default = $this->getDefault();
$value = $dialog->askAndValidate($output, "<info>Issues url</info> ({$default}): ", function ($email) {
if (false === filter_var($email, FILTER_VALIDATE_URL)) {
throw new RuntimeException('Not a valid url');
}
return $email;
}, false, $default);
return ['bugs' => $value];
}
示例9: configureProjectName
protected function configureProjectName(InputInterface $input, OutputInterface $output)
{
$dirName = basename($this->settings->getBaseDir());
$guessedName = $this->guessName($dirName);
$default = empty($this->settings['projectName']) ? $guessedName : $this->settings['projectName'];
$this->settings['projectName'] = $this->dialog->askAndValidate($output, "What is the name of the project? [{$default}] ", function ($data) {
if (preg_match('/^[\\w\\.\\s]+$/', $data)) {
return $data;
}
throw new \Exception("The project name may only contain 'a-zA-Z0-9_. '");
}, false, $default);
}
示例10: askSlackToken
/**
* @return string
*/
private function askSlackToken()
{
$question = "Please paste your slack credentials \n" . " (see http://docs.travis-ci.com/user/notifications/#Slack-notifications): \n";
// very basic validation
$validator = function ($token) {
// we must have a string, that contains a : not at the starting position
// token format is username:hashedtoken
if (is_string($token) && strpos($token, ':')) {
return $token;
}
throw new \Exception("Please enter a valid token");
};
return $this->dialog->askAndValidate($this->output, $question, $validator, false, $this->settings->getDefaultValueFor('travis.slack.token', null));
}
示例11: testAskAndValidate
public function testAskAndValidate()
{
$dialog = new DialogHelper();
$helperSet = new HelperSet(array(new FormatterHelper()));
$dialog->setHelperSet($helperSet);
$question = 'What color was the white horse of Henry IV?';
$error = 'This is not a color!';
$validator = function ($color) use($error) {
if (!in_array($color, array('white', 'black'))) {
throw new \InvalidArgumentException($error);
}
return $color;
};
$dialog->setInputStream($this->getInputStream("\nblack\n"));
$this->assertEquals('white', $dialog->askAndValidate($this->getOutputStream(), $question, $validator, 2, 'white'));
$this->assertEquals('black', $dialog->askAndValidate($this->getOutputStream(), $question, $validator, 2, 'white'));
$dialog->setInputStream($this->getInputStream("green\nyellow\norange\n"));
try {
$this->assertEquals('white', $dialog->askAndValidate($this->getOutputStream(), $question, $validator, 2, 'white'));
$this->fail();
} catch (\InvalidArgumentException $e) {
$this->assertEquals($error, $e->getMessage());
}
}
示例12: enablePhpUnitAutoLoad
/**
* @param OutputInterface $output
* @param Settings $settings
* @return bool
*/
protected function enablePhpUnitAutoLoad(OutputInterface $output, Settings $settings)
{
$default = $this->settings->getDefaultValueFor('enablePhpUnitAutoload', true);
$settings['enablePhpUnitAutoload'] = $this->dialog->askConfirmation($output, "Do you want to enable an autoload script for PHPUnit?", $default);
if (false === $settings['enablePhpUnitAutoload']) {
return false;
}
if ($settings['enablePhpUnitAutoload']) {
$default = $this->settings->getDefaultValueFor('phpUnitAutoloadPath', 'vendor/autoload.php');
$settings['phpUnitAutoloadPath'] = $this->dialog->askAndValidate($output, "What is the path to the autoload script for PHPUnit? [{$default}] ", function ($data) use($settings) {
if (file_exists($settings->getBaseDir() . '/' . $data)) {
return $data;
}
throw new \Exception("That path doesn't exist");
}, false, $default);
}
return true;
}
示例13: ask
/**
* Ask the user for one or more paths/patterns
*
* @param string $pathQuestion
* @param string $defaultPaths
* @param null $confirmationQuestion Optional question to ask if you want to set the value
* @param bool $defaultConfirmation
* @param callable $callback
*
* @throws \Exception when callable is not callable.
*
* @return string
*/
protected function ask($pathQuestion, $defaultPaths, $confirmationQuestion, $defaultConfirmation, $callback)
{
/**
* Type hinting with callable (@see http://www.php.net/manual/en/language.types.callable.php)
* is only from PHP5.4+ and therefore we check with is_callable()
*/
if (!is_callable($callback)) {
throw new \Exception('Error calling callable');
}
if ($defaultPaths) {
$pathQuestion .= ' [' . (is_array($defaultPaths) ? implode(',', $defaultPaths) : $defaultPaths) . ']';
}
if ($confirmationQuestion) {
if (!$this->dialog->askConfirmation($this->output, $confirmationQuestion, $defaultConfirmation)) {
return array();
}
}
return $this->dialog->askAndValidate($this->output, $pathQuestion . " (comma separated)\n", $callback, false, $defaultPaths);
}
示例14: askQuestion
public function askQuestion(InteractiveQuestion $question, $position = null, InputInterface $input = null)
{
$text = ($position !== null ? $position . ') ' : null) . $question->getFormatedText();
if ($this->dialogHelper instanceof QuestionHelper) {
if (!$input) {
throw new \InvalidArgumentException('With symfony 3, the input stream may not be null');
}
$q = new Question($text, $question->getDefault());
$q->setValidator($question->getValidator());
if ($question->isHiddenAnswer()) {
$q->setHidden(true);
}
return $this->dialogHelper->ask($input, $this, $q);
}
if ($this->dialogHelper instanceof DialogHelper) {
if ($question->isHiddenAnswer()) {
return $this->dialogHelper->askHiddenResponseAndValidate($this, $text, $question->getValidator(), false);
}
return $this->dialogHelper->askAndValidate($this, $text, $question->getValidator(), false, $question->getDefault());
}
throw new \RuntimeException("Invalid dialogHelper");
}
示例15: setParams
protected function setParams()
{
$this->scroll_time = $this->dialog->askAndValidate($this->output, "Select scroll time, ie 10m: ", function ($answer) {
if (!preg_match('/\\d+(s|m|h|d|w|M|y)/', trim($answer))) {
throw new \RunTimeException("Wrong scroll time format, ie '10m': {$answer}");
}
return $answer;
});
$this->scroll_size = $this->dialog->askAndValidate($this->output, "Select scroll size, ie 100: ", function ($answer) {
if (!preg_match('/\\d+/', trim($answer))) {
throw new \RunTimeException("Wrong scroll size format, ie '100': {$answer}");
}
return $answer;
});
$this->from_index = $this->dialog->askAndValidate($this->output, "Index to index from: ", function ($answer) {
if (!trim($answer)) {
throw new \RunTimeException("You have to select an index name.");
}
return $answer;
});
$this->to_index = $this->dialog->askAndValidate($this->output, "Index to index to: ", function ($answer) {
if (!trim($answer)) {
throw new \RunTimeException("You have to select an index name.");
}
return $answer;
});
$this->types_to_index = $this->input->getArgument('types_to_index');
if (!$this->types_to_index) {
$this->out($this->output, '<fg=red;bg=black>No mapping selected, please select a mapping to index.</fg=red;bg=black>');
$this->types_to_index[] = $this->dialog->askAndValidate($this->output, "Mapping to index: ", function ($answer) {
if (!trim($answer)) {
throw new \RunTimeException("You have to select a mapping");
}
return $answer;
});
}
}