本文整理汇总了PHP中Symfony\Component\Console\Formatter\OutputFormatter类的典型用法代码示例。如果您正苦于以下问题:PHP OutputFormatter类的具体用法?PHP OutputFormatter怎么用?PHP OutputFormatter使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了OutputFormatter类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: run
/**
* Override run method to internationalize error message.
*
* @param \Symfony\Component\Console\Input\InputInterface $input
* @param \Symfony\Component\Console\Output\OutputInterface $output
* @return int Return code
*/
public function run(InputInterface $input, OutputInterface $output)
{
// Set extra colors
// The most problem is $output->getFormatter() don't work.
// So create new formatter to add extra color.
$formatter = new OutputFormatter($output->isDecorated());
$formatter->setStyle('red', new OutputFormatterStyle('red', 'black'));
$formatter->setStyle('green', new OutputFormatterStyle('green', 'black'));
$formatter->setStyle('yellow', new OutputFormatterStyle('yellow', 'black'));
$formatter->setStyle('blue', new OutputFormatterStyle('blue', 'black'));
$formatter->setStyle('magenta', new OutputFormatterStyle('magenta', 'black'));
$formatter->setStyle('yellow-blue', new OutputFormatterStyle('yellow', 'blue'));
$output->setFormatter($formatter);
\App::setLocale(\Config::get('syncle::MessageLang'));
try {
$result = parent::run($input, $output);
} catch (\RuntimeException $e) {
// All error messages were hard coded in
// Symfony/Component/Console/Input/Input.php
if ($e->getMessage() == 'Not enough arguments.') {
$this->error(\Lang::get('syncle::BaseCommand.ArgumentNotEnough'));
} elseif ($e->getMessage() == 'Too many arguments.') {
$this->error(\Lang::get('syncle::BaseCommand.TooManyArgument'));
} elseif (preg_match('/The "(.+)" option does not exist./', $e->getMessage(), $matches)) {
$this->error(\Lang::get('syncle::BaseCommand.OptionNotExist', array('option' => $matches[1])));
} else {
$this->error($e->getMessage());
}
$result = 1;
}
return $result;
}
示例2: testFormattedEscapedOutput
public function testFormattedEscapedOutput()
{
$output = new StreamOutput($this->stream, StreamOutput::VERBOSITY_NORMAL, true, null);
$output->writeln('<info>' . OutputFormatter::escape('<error>some error</error>') . '</info>');
rewind($output->getStream());
$this->assertSame("[32m<error>some error</error>[39m" . PHP_EOL, stream_get_contents($output->getStream()));
}
示例3: writePrompt
/**
* {@inheritdoc}
*/
protected function writePrompt(OutputInterface $output, Question $question)
{
$text = OutputFormatter::escape($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 && $question->isMultiselect():
$choices = $question->getChoices();
$default = explode(',', $default);
foreach ($default as $key => $value) {
$default[$key] = $choices[trim($value)];
}
$text = sprintf(' <info>%s</info> [<comment>%s</comment>]:', $text, OutputFormatter::escape(implode(', ', $default)));
break;
case $question instanceof ChoiceQuestion:
$choices = $question->getChoices();
$text = sprintf(' <info>%s</info> [<comment>%s</comment>]:', $text, OutputFormatter::escape($choices[$default]));
break;
default:
$text = sprintf(' <info>%s</info> [<comment>%s</comment>]:', $text, OutputFormatter::escape($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(' > ');
}
示例4: generate
/**
* {@inheritdoc}
*/
public function generate(ReportSummary $reportSummary)
{
$dom = new \DOMDocument('1.0', 'UTF-8');
// new nodes should be added to this or existing children
$root = $dom->createElement('report');
$dom->appendChild($root);
$filesXML = $dom->createElement('files');
$root->appendChild($filesXML);
$i = 1;
foreach ($reportSummary->getChanged() as $file => $fixResult) {
$fileXML = $dom->createElement('file');
$fileXML->setAttribute('id', $i++);
$fileXML->setAttribute('name', $file);
$filesXML->appendChild($fileXML);
if ($reportSummary->shouldAddAppliedFixers()) {
$fileXML->appendChild($this->createAppliedFixersElement($dom, $fixResult));
}
if (!empty($fixResult['diff'])) {
$fileXML->appendChild($this->createDiffElement($dom, $fixResult));
}
}
if (null !== $reportSummary->getTime()) {
$root->appendChild($this->createTimeElement($reportSummary->getTime(), $dom));
}
if (null !== $reportSummary->getTime()) {
$root->appendChild($this->createMemoryElement($reportSummary->getMemory(), $dom));
}
$dom->formatOutput = true;
return $reportSummary->isDecoratedOutput() ? OutputFormatter::escape($dom->saveXML()) : $dom->saveXML();
}
示例5: complete
/**
* Print escaped values of all parameters
* Strings like 'Namespace\\' has to be escaped
*
* @link https://github.com/symfony/symfony/issues/17908
*
* @return void
*/
public function complete()
{
$this->io->write('<info>Summary :</info>');
foreach ($this->getConfigKey('Placeholders') as $placeholder) {
$this->io->write(sprintf(_('%1$s: <info>%2$s</info>'), Formatter\OutputFormatter::escape($placeholder['name']), Formatter\OutputFormatter::escape($placeholder['value'])));
}
}
示例6: __construct
/**
* @param boolean $decorated
*
* @param array $styles
*/
public function __construct($decorated = false, array $styles = array())
{
parent::__construct($decorated, $styles);
$this->setStyle('pending', new OutputFormatterStyle('yellow'));
$this->setStyle('pending-bg', new OutputFormatterStyle('black', 'yellow', array('bold')));
$this->setStyle('skipped', new OutputFormatterStyle('cyan'));
$this->setStyle('skipped-bg', new OutputFormatterStyle('white', 'cyan', array('bold')));
$this->setStyle('failed', new OutputFormatterStyle('red'));
$this->setStyle('failed-bg', new OutputFormatterStyle('white', 'red', array('bold')));
$this->setStyle('broken', new OutputFormatterStyle('magenta'));
$this->setStyle('broken-bg', new OutputFormatterStyle('white', 'magenta', array('bold')));
$this->setStyle('passed', new OutputFormatterStyle('green'));
$this->setStyle('passed-bg', new OutputFormatterStyle('black', 'green', array('bold')));
$this->setStyle('value', new OutputFormatterStyle('yellow'));
$this->setStyle('lineno', new OutputFormatterStyle(null, 'black'));
$this->setStyle('code', new OutputFormatterStyle('white'));
$this->setStyle('hl', new OutputFormatterStyle('black', 'yellow', array('bold')));
$this->setStyle('question', new OutputFormatterStyle('black', 'yellow', array('bold')));
$this->setStyle('trace', new OutputFormatterStyle());
$this->setStyle('trace-class', new OutputFormatterStyle('cyan'));
$this->setStyle('trace-func', new OutputFormatterStyle('cyan'));
$this->setStyle('trace-type', new OutputFormatterStyle());
$this->setStyle('trace-args', new OutputFormatterStyle());
$this->setStyle('diff-add', new OutputFormatterStyle('green'));
$this->setStyle('diff-del', new OutputFormatterStyle('red'));
}
示例7: block
/**
* Formats a message as a block of text.
*
* @param string|array $messages The message to write in the block
* @param string|null $type The block type (added in [] on first line)
* @param string|null $style The style to apply to the whole block
* @param string $prefix The prefix for the block
* @param bool $padding Whether to add vertical padding
*/
public function block($messages, $type = null, $style = null, $prefix = ' ', $padding = false)
{
$this->autoPrependBlock();
$messages = is_array($messages) ? array_values($messages) : array($messages);
$lines = array();
// add type
if (null !== $type) {
$messages[0] = sprintf('[%s] %s', $type, $messages[0]);
}
// wrap and add newlines for each element
foreach ($messages as $key => $message) {
$message = OutputFormatter::escape($message);
$lines = array_merge($lines, explode(PHP_EOL, wordwrap($message, $this->lineLength - Helper::strlen($prefix), PHP_EOL, true)));
if (count($messages) > 1 && $key < count($messages) - 1) {
$lines[] = '';
}
}
if ($padding && $this->isDecorated()) {
array_unshift($lines, '');
$lines[] = '';
}
foreach ($lines as &$line) {
$line = sprintf('%s%s', $prefix, $line);
$line .= str_repeat(' ', $this->lineLength - Helper::strlenWithoutDecoration($this->getFormatter(), $line));
if ($style) {
$line = sprintf('<%s>%s</>', $style, $line);
}
}
$this->writeln($lines);
$this->newLine();
}
示例8: getBacktrace
/**
* Get a backtrace for an exception.
*
* Optionally limit the number of rows to include with $count, and exclude
* Psy from the trace.
*
* @param \Exception $e The exception with a backtrace.
* @param int $count (default: PHP_INT_MAX)
* @param bool $includePsy (default: true)
*
* @return array Formatted stacktrace lines.
*/
protected function getBacktrace(\Exception $e, $count = null, $includePsy = true)
{
if ($cwd = getcwd()) {
$cwd = rtrim($cwd, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
}
if ($count === null) {
$count = PHP_INT_MAX;
}
$lines = array();
$trace = $e->getTrace();
array_unshift($trace, array('function' => '', 'file' => $e->getFile() !== null ? $e->getFile() : 'n/a', 'line' => $e->getLine() !== null ? $e->getLine() : 'n/a', 'args' => array()));
if (!$includePsy) {
for ($i = count($trace) - 1; $i >= 0; $i--) {
$thing = isset($trace[$i]['class']) ? $trace[$i]['class'] : $trace[$i]['function'];
if (preg_match('/\\\\?Psy\\\\/', $thing)) {
$trace = array_slice($trace, $i + 1);
break;
}
}
}
for ($i = 0, $count = min($count, count($trace)); $i < $count; $i++) {
$class = isset($trace[$i]['class']) ? $trace[$i]['class'] : '';
$type = isset($trace[$i]['type']) ? $trace[$i]['type'] : '';
$function = $trace[$i]['function'];
$file = isset($trace[$i]['file']) ? $this->replaceCwd($cwd, $trace[$i]['file']) : 'n/a';
$line = isset($trace[$i]['line']) ? $trace[$i]['line'] : 'n/a';
$lines[] = sprintf(' <class>%s</class>%s%s() at <info>%s:%s</info>', OutputFormatter::escape($class), OutputFormatter::escape($type), OutputFormatter::escape($function), OutputFormatter::escape($file), OutputFormatter::escape($line));
}
return $lines;
}
示例9: testContentWithLineBreaks
public function testContentWithLineBreaks()
{
$formatter = new OutputFormatter(true);
$this->assertEquals(<<<EOF
[32m
some text[0m
EOF
, $formatter->format(<<<EOF
<info>
some text</info>
EOF
));
$this->assertEquals(<<<EOF
[32msome text
[0m
EOF
, $formatter->format(<<<EOF
<info>some text
</info>
EOF
));
$this->assertEquals(<<<EOF
[32m
some text
[0m
EOF
, $formatter->format(<<<EOF
<info>
some text
</info>
EOF
));
$this->assertEquals(<<<EOF
[32m
some text
more text
[0m
EOF
, $formatter->format(<<<EOF
<info>
some text
more text
</info>
EOF
));
}
示例10: __construct
/**
* OutputFormatter constructor.
*
* @param bool $decorated
* @param array $styles
*/
public function __construct($decorated = false, array $styles = array())
{
parent::__construct($decorated, $styles);
$this->setStyle('passed', new OutputFormatterStyle('green', null, array()));
$this->setStyle('passed-bg', new OutputFormatterStyle('black', 'green', array('bold')));
$this->setStyle('broken', new OutputFormatterStyle('red', null, array()));
$this->setStyle('broken-bg', new OutputFormatterStyle('white', 'red', array('bold')));
$this->setStyle('blink', new OutputFormatterStyle(null, null, array('blink')));
}
示例11: __construct
public function __construct($decorated = false, array $styles = array())
{
parent::__construct(true, $styles);
$this->setStyle('logo', new OutputFormatterStyle('magenta'));
$this->setStyle('error', new OutputFormatterStyle('white', 'red'));
$this->setStyle('comment', new OutputFormatterStyle('magenta', null, array('underscore')));
$this->setStyle('info', new OutputFormatterStyle('cyan'));
$this->setStyle('question', new OutputFormatterStyle('black', 'cyan'));
}
示例12: isset
function __construct($config)
{
$this->config = array_merge($this->config, $config);
// enable interactive output mode for CLI
$this->isInteractive = $this->config['interactive'] && isset($_SERVER['TERM']) && php_sapi_name() == 'cli' && $_SERVER['TERM'] != 'linux';
$formatter = new OutputFormatter($this->config['colors']);
$formatter->setStyle('bold', new OutputFormatterStyle(null, null, ['bold']));
$formatter->setStyle('focus', new OutputFormatterStyle('magenta', null, ['bold']));
$formatter->setStyle('ok', new OutputFormatterStyle('white', 'magenta'));
$formatter->setStyle('error', new OutputFormatterStyle('white', 'red'));
$formatter->setStyle('debug', new OutputFormatterStyle('cyan'));
$formatter->setStyle('comment', new OutputFormatterStyle('yellow'));
$formatter->setStyle('info', new OutputFormatterStyle('green'));
$this->formatHelper = new FormatterHelper();
parent::__construct($this->config['verbosity'], $this->config['colors'], $formatter);
}
示例13: getOutputFormatter
/**
* Initialize and get console output formatter
*
* @return OutputFormatter
*/
protected function getOutputFormatter()
{
if (null === $this->outputFormatter) {
$formatter = new OutputFormatter(true);
$formatter->setStyle(LogLevel::EMERGENCY, new OutputFormatterStyle('white', 'red'));
$formatter->setStyle(LogLevel::ALERT, new OutputFormatterStyle('white', 'red'));
$formatter->setStyle(LogLevel::CRITICAL, new OutputFormatterStyle('red'));
$formatter->setStyle(LogLevel::ERROR, new OutputFormatterStyle('red'));
$formatter->setStyle(LogLevel::WARNING, new OutputFormatterStyle('yellow'));
$formatter->setStyle(LogLevel::NOTICE, new OutputFormatterStyle());
$formatter->setStyle(LogLevel::INFO, new OutputFormatterStyle());
$formatter->setStyle(LogLevel::DEBUG, new OutputFormatterStyle('cyan'));
$this->outputFormatter = $formatter;
}
return $this->outputFormatter;
}
示例14: OutputFormatter
function __construct($config)
{
$this->config = array_merge($this->config, $config);
$formatter = new OutputFormatter($this->config['colors']);
$formatter->setStyle('bold', new OutputFormatterStyle(null, null, array('bold')));
$formatter->setStyle('focus', new OutputFormatterStyle('magenta', null, array('bold')));
$formatter->setStyle('ok', new OutputFormatterStyle('white', 'magenta'));
$formatter->setStyle('error', new OutputFormatterStyle('white', 'red'));
$formatter->setStyle('debug', new OutputFormatterStyle('cyan'));
$formatter->setStyle('info', new OutputFormatterStyle('yellow'));
$this->formatHelper = new FormatterHelper();
parent::__construct($this->config['verbosity'], $this->config['colors'], $formatter);
}
示例15: presentRef
/**
* Present a reference to the value.
*
* @param mixed $value
*
* @return string
*/
public function presentRef($value, $color = false)
{
$type = get_resource_type($value);
if ($type === 'stream') {
$meta = stream_get_meta_data($value);
$type = sprintf('%s stream', $meta['stream_type']);
}
$id = str_replace('Resource id #', '', (string) $value);
$format = $color ? self::COLOR_FMT : self::FMT;
return sprintf($format, OutputFormatter::escape('<'), $type, $id);
}