本文整理汇总了PHP中Symfony\Component\Console\Formatter\OutputFormatter::escape方法的典型用法代码示例。如果您正苦于以下问题:PHP OutputFormatter::escape方法的具体用法?PHP OutputFormatter::escape怎么用?PHP OutputFormatter::escape使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Symfony\Component\Console\Formatter\OutputFormatter
的用法示例。
在下文中一共展示了OutputFormatter::escape方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: 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();
}
示例2: 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(' > ');
}
示例3: 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;
}
示例4: 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'])));
}
}
示例5: 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();
}
示例6: 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()));
}
示例7: testLGCharEscaping
public function testLGCharEscaping()
{
$formatter = new OutputFormatter(true);
$this->assertEquals("foo<bar", $formatter->format('foo\\<bar'));
$this->assertEquals("<info>some info</info>", $formatter->format('\\<info>some info\\</info>'));
$this->assertEquals("\\<info>some info\\</info>", OutputFormatter::escape('<info>some info</info>'));
$this->assertEquals("[33mSymfony\\Component\\Console does work very well![0m", $formatter->format('<comment>Symfony\\Component\\Console does work very well!</comment>'));
}
示例8: 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);
}
示例9: format
/**
* @param string $diff
* @param string $lineTemplate
*
* @return string
*/
public function format($diff, $lineTemplate = '%s')
{
$isDecorated = $this->isDecoratedOutput;
$template = $isDecorated ? $this->template : preg_replace('/<[^<>]+>/', '', $this->template);
return sprintf($template, implode(PHP_EOL, array_map(function ($string) use($isDecorated, $lineTemplate) {
if ($isDecorated) {
$string = preg_replace(array('/^(\\+.*)/', '/^(\\-.*)/', '/^(@.*)/'), array('<fg=green>\\1</fg=green>', '<fg=red>\\1</fg=red>', '<fg=cyan>\\1</fg=cyan>'), $string);
}
$templated = sprintf($lineTemplate, $string);
if (' ' === $string) {
$templated = rtrim($templated);
}
return $templated;
}, preg_split("#\n\r|\n#", $isDecorated ? OutputFormatter::escape(rtrim($diff)) : $diff))));
}
示例10: generate
/**
* {@inheritdoc}
*/
public function generate(ReportSummary $reportSummary)
{
$dom = new \DOMDocument('1.0', 'UTF-8');
$testsuites = $dom->appendChild($dom->createElement('testsuites'));
/** @var \DomElement $testsuite */
$testsuite = $testsuites->appendChild($dom->createElement('testsuite'));
$testsuite->setAttribute('name', 'PHP CS Fixer');
if (count($reportSummary->getChanged())) {
$this->createFailedTestCases($dom, $testsuite, $reportSummary);
} else {
$this->createSuccessTestCase($dom, $testsuite);
}
if ($reportSummary->getTime()) {
$testsuite->setAttribute('time', sprintf('%.3f', $reportSummary->getTime() / 1000));
}
$dom->formatOutput = true;
return $reportSummary->isDecoratedOutput() ? OutputFormatter::escape($dom->saveXML()) : $dom->saveXML();
}
示例11: writeTwoPhases
/**
* Writes a file in two phase to the filesystem.
*
* First write the data to a temporary file (in the same directory) and than renames the temporary file. If the file
* already exists and its content is equal to the data that must be written no action is taken. This has the
* following advantages:
* * In case of some write error (e.g. disk full) the original file is kept in tact and no file with partially data
* is written.
* * Renaming a file is atomic. So, running processes will never read a partially written data.
*
* @param string $filename The name of the file were the data must be stored.
* @param string $data The data that must be written.
* @param StratumStyle $io The output decorator.
*/
static function writeTwoPhases($filename, $data, $io)
{
$write_flag = true;
if (file_exists($filename)) {
$old_data = file_get_contents($filename);
if ($data == $old_data) {
$write_flag = false;
}
}
if ($write_flag) {
$tmp_filename = $filename . '.tmp';
file_put_contents($tmp_filename, $data);
rename($tmp_filename, $filename);
$io->text(sprintf('Wrote <fso>%s</fso>', OutputFormatter::escape($filename)));
} else {
$io->text(sprintf('File <fso>%s</fso> is up to date', OutputFormatter::escape($filename)));
}
}
示例12: format
private function format($value)
{
// Handle floats.
if (is_float($value)) {
// Some are unencodable...
if (is_nan($value)) {
return 'NAN';
} elseif (is_infinite($value)) {
return $value === INF ? 'INF' : '-INF';
}
// ... others just encode as ints when there's no decimal
$float = Json::encode($value);
if (strpos($float, '.') === false) {
$float .= '.0';
}
return $float;
}
return OutputFormatter::escape(Json::encode($value));
}
示例13: 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);
$indentLength = 0;
$lines = array();
if (null !== $type) {
$typePrefix = sprintf('[%s] ', $type);
$indentLength = strlen($typePrefix);
$lineIndentation = str_repeat(' ', $indentLength);
}
// 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) - $indentLength, PHP_EOL, true)));
// prefix each line with a number of spaces equivalent to the type length
if (null !== $type) {
foreach ($lines as &$line) {
$line = $lineIndentation === substr($line, 0, $indentLength) ? $line : $lineIndentation . $line;
}
}
if (count($messages) > 1 && $key < count($messages) - 1) {
$lines[] = '';
}
}
if (null !== $type) {
$lines[0] = substr_replace($lines[0], $typePrefix, 0, $indentLength);
}
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();
}
示例14: getDiff
/**
* @param bool $isDecoratedOutput
* @param array $fixResult
*
* @return string
*/
private function getDiff($isDecoratedOutput, array $fixResult)
{
if (empty($fixResult['diff'])) {
return '';
}
if ($isDecoratedOutput) {
$template = '<comment> ---------- begin diff ----------</comment>%s<comment> ----------- end diff -----------</comment>';
$diff = implode(PHP_EOL, array_map(function ($string) {
$string = preg_replace('/^(\\+){3}/', '<info>+++</info>', $string);
$string = preg_replace('/^(\\+){1}/', '<info>+</info>', $string);
$string = preg_replace('/^(\\-){3}/', '<error>---</error>', $string);
$string = preg_replace('/^(\\-){1}/', '<error>-</error>', $string);
return $string;
}, explode(PHP_EOL, OutputFormatter::escape($fixResult['diff']))));
} else {
$template = ' ---------- begin diff ----------%s ----------- end diff -----------';
$diff = $fixResult['diff'];
}
return PHP_EOL . sprintf($template, PHP_EOL . $diff . PHP_EOL) . PHP_EOL;
}
示例15: renderCodeCoverage
private function renderCodeCoverage(array $reportLines, $widestLine)
{
$output = '';
$widestLineNum = mb_strlen(count($reportLines));
foreach ($reportLines as $i => $line) {
$source = $this->formatter->escape(str_pad($line['line'], $widestLine));
$lineNum = str_pad((string) ($i + 1), $widestLineNum, ' ', STR_PAD_LEFT);
switch ($line['code']) {
case self::COVERAGE_COVERED:
$output .= $this->formatter->format(sprintf('<bg=white;fg=green>[%s] %s</>', $lineNum, $source));
break;
case self::COVERAGE_NOT_COVERED:
$output .= $this->formatter->format(sprintf('<bg=white;fg=red>[%s] %s</>', $lineNum, $source));
break;
case self::COVERAGE_META:
$output .= $this->formatter->format(sprintf('<bg=white;fg=black>[%s] %s</>', $lineNum, $source));
break;
}
$output .= PHP_EOL;
}
return $output;
}