Вход Регистрация
Файл: concrete5.7.5.6/concrete/vendor/symfony/console/Helper/SymfonyQuestionHelper.php
Строк: 150
<?php

/*
 * This file is part of the Symfony package.
 *
 * (c) Fabien Potencier <fabien@symfony.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace SymfonyComponentConsoleHelper;

use 
SymfonyComponentConsoleInputInputInterface;
use 
SymfonyComponentConsoleOutputOutputInterface;
use 
SymfonyComponentConsoleQuestionChoiceQuestion;
use 
SymfonyComponentConsoleQuestionConfirmationQuestion;
use 
SymfonyComponentConsoleQuestionQuestion;
use 
SymfonyComponentConsoleStyleSymfonyStyle;

/**
 * Symfony Style Guide compliant question helper.
 *
 * @author Kevin Bond <kevinbond@gmail.com>
 */
class SymfonyQuestionHelper extends QuestionHelper
{
    
/**
     * {@inheritdoc}
     */
    
public function ask(InputInterface $inputOutputInterface $outputQuestion $question)
    {
        
$validator $question->getValidator();
        
$question->setValidator(function ($value) use ($validator) {
            if (
null !== $validator && is_callable($validator)) {
                
$value $validator($value);
            }

            
// make required
            
if (!is_array($value) && !is_bool($value) && === strlen($value)) {
                throw new 
Exception('A value is required.');
            }

            return 
$value;
        });

        return 
parent::ask($input$output$question);
    }

    
/**
     * {@inheritdoc}
     */
    
protected function writePrompt(OutputInterface $outputQuestion $question)
    {
        
$text $question->getQuestion();
        
$default $question->getDefault();

        switch (
true) {
            case 
null === $default:
                
$text sprintf(' <info>%s</info>:'$text);

                break;

            case 
$question instanceof ConfirmationQuestion:
                
$text sprintf(' <info>%s (yes/no)</info> [<comment>%s</comment>]:'$text$default 'yes' 'no');

                break;

            case 
$question instanceof ChoiceQuestion:
                
$choices $question->getChoices();
                
$text sprintf(' <info>%s</info> [<comment>%s</comment>]:'$text$choices[$default]);

                break;

            default:
                
$text sprintf(' <info>%s</info> [<comment>%s</comment>]:'$text$default);
        }

        
$output->writeln($text);

        if (
$question instanceof ChoiceQuestion) {
            
$width max(array_map('strlen'array_keys($question->getChoices())));

            foreach (
$question->getChoices() as $key => $value) {
                
$output->writeln(sprintf("  [<comment>%-${width}s</comment>] %s"$key$value));
            }
        }

        
$output->write(' > ');
    }

    
/**
     * {@inheritdoc}
     */
    
protected function writeError(OutputInterface $outputException $error)
    {
        if (
$output instanceof SymfonyStyle) {
            
$output->newLine();
            
$output->error($error->getMessage());

            return;
        }

        
parent::writeError($output$error);
    }
}
Онлайн: 0
Реклама