This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/Console/Formatter/OutputFormatter.php

251 lines
6.3 KiB
PHP
Raw Normal View History

<?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 Symfony\Component\Console\Formatter;
/**
* Formatter class for console output.
*
* @author Konstantin Kudryashov <ever.zet@gmail.com>
2011-03-24 08:39:53 +00:00
*
* @api
*/
class OutputFormatter implements OutputFormatterInterface
{
/**
* The pattern to phrase the format.
*/
const FORMAT_PATTERN = '#(\\\\?)<(/?)([a-z][a-z0-9_=;-]*)?>((?: [^<\\\\]+ | (?!<(?:/?[a-z]|/>)). | .(?<=\\\\<) )*)#isx';
private $decorated;
private $styles = array();
private $styleStack;
2012-07-10 10:13:16 +01:00
/**
* Escapes "<" special char in given text.
*
* @param string $text Text to escape
*
* @return string Escaped text
*/
public static function escape($text)
{
return preg_replace('/([^\\\\]?)</is', '$1\\<', $text);
}
/**
* Initializes console output formatter.
*
* @param Boolean $decorated Whether this formatter should actually decorate strings
2013-09-18 13:41:53 +01:00
* @param OutputFormatterStyleInterface[] $styles Array of "name => FormatterStyle" instances
2011-03-24 08:39:53 +00:00
*
* @api
*/
public function __construct($decorated = false, array $styles = array())
{
$this->decorated = (Boolean) $decorated;
2012-05-15 16:04:12 +01:00
$this->setStyle('error', new OutputFormatterStyle('white', 'red'));
$this->setStyle('info', new OutputFormatterStyle('green'));
$this->setStyle('comment', new OutputFormatterStyle('yellow'));
$this->setStyle('question', new OutputFormatterStyle('black', 'cyan'));
foreach ($styles as $name => $style) {
$this->setStyle($name, $style);
}
$this->styleStack = new OutputFormatterStyleStack();
}
/**
* Sets the decorated flag.
*
* @param Boolean $decorated Whether to decorate the messages or not
2011-03-24 08:39:53 +00:00
*
* @api
*/
public function setDecorated($decorated)
{
$this->decorated = (Boolean) $decorated;
}
/**
* Gets the decorated flag.
*
* @return Boolean true if the output will decorate messages, false otherwise
2011-03-24 08:39:53 +00:00
*
* @api
*/
public function isDecorated()
{
return $this->decorated;
}
/**
* Sets a new style.
*
2011-04-23 16:05:44 +01:00
* @param string $name The style name
* @param OutputFormatterStyleInterface $style The style instance
2011-03-24 08:39:53 +00:00
*
* @api
*/
public function setStyle($name, OutputFormatterStyleInterface $style)
{
$this->styles[strtolower($name)] = $style;
}
/**
* Checks if output formatter has style with specified name.
*
2012-05-15 16:04:12 +01:00
* @param string $name
*
2012-05-15 16:04:12 +01:00
* @return Boolean
2011-03-24 08:39:53 +00:00
*
* @api
*/
public function hasStyle($name)
{
return isset($this->styles[strtolower($name)]);
}
/**
* Gets style options from style with specified name.
*
2012-05-15 16:04:12 +01:00
* @param string $name
*
2012-05-15 16:04:12 +01:00
* @return OutputFormatterStyleInterface
2011-03-24 08:39:53 +00:00
*
2012-05-15 16:04:12 +01:00
* @throws \InvalidArgumentException When style isn't defined
*
2011-03-24 08:39:53 +00:00
* @api
*/
public function getStyle($name)
{
if (!$this->hasStyle($name)) {
2013-04-10 11:24:37 +01:00
throw new \InvalidArgumentException(sprintf('Undefined style: %s', $name));
}
return $this->styles[strtolower($name)];
}
/**
* Formats a message according to the given styles.
*
2012-05-15 21:19:31 +01:00
* @param string $message The message to style
*
* @return string The styled message
2011-03-24 08:39:53 +00:00
*
* @api
*/
public function format($message)
{
2012-07-10 10:13:16 +01:00
$message = preg_replace_callback(self::FORMAT_PATTERN, array($this, 'replaceStyle'), $message);
2012-07-10 13:40:39 +01:00
return str_replace('\\<', '<', $message);
}
/**
* @return OutputFormatterStyleStack
*/
public function getStyleStack()
{
return $this->styleStack;
}
/**
* Replaces style of the output.
*
* All escaped tags and tags that reference unknown styles are kept as is.
*
* @param array $match
*
* @return string The replaced style
*/
private function replaceStyle($match)
{
2012-07-10 10:13:16 +01:00
// we got "\<" escaped char
2012-07-10 13:40:39 +01:00
if ('\\' === $match[1]) {
return $this->applyCurrentStyle($match[0]);
2012-07-10 10:13:16 +01:00
}
if ('' === $match[3]) {
if ('/' === $match[2]) {
// we got "</>" tag
$this->styleStack->pop();
return $this->applyCurrentStyle($match[4]);
}
// we got "<>" tag
return '<>'.$this->applyCurrentStyle($match[4]);
}
2012-07-10 10:13:16 +01:00
if (isset($this->styles[strtolower($match[3])])) {
$style = $this->styles[strtolower($match[3])];
} else {
2012-07-10 10:13:16 +01:00
$style = $this->createStyleFromString($match[3]);
if (false === $style) {
return $this->applyCurrentStyle($match[0]);
}
}
2012-07-10 10:13:16 +01:00
if ('/' === $match[2]) {
$this->styleStack->pop($style);
} else {
$this->styleStack->push($style);
}
return $this->applyCurrentStyle($match[4]);
}
/**
2011-05-29 23:24:32 +01:00
* Tries to create new style instance from string.
*
2012-05-15 16:04:12 +01:00
* @param string $string
*
2012-05-18 18:02:36 +01:00
* @return OutputFormatterStyle|Boolean false if string is not format string
*/
private function createStyleFromString($string)
{
if (!preg_match_all('/([^=]+)=([^;]+)(;|$)/', strtolower($string), $matches, PREG_SET_ORDER)) {
return false;
}
$style = new OutputFormatterStyle();
foreach ($matches as $match) {
array_shift($match);
if ('fg' == $match[0]) {
$style->setForeground($match[1]);
} elseif ('bg' == $match[0]) {
$style->setBackground($match[1]);
} else {
$style->setOption($match[1]);
}
}
return $style;
}
/**
* Applies current style from stack to text, if must be applied.
*
* @param string $text Input text
*
* @return string Styled text
*/
private function applyCurrentStyle($text)
{
return $this->isDecorated() && strlen($text) > 0 ? $this->styleStack->getCurrent()->apply($text) : $text;
}
}