2011-03-17 15:51:49 +00:00
|
|
|
<?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
|
2011-03-17 15:51:49 +00:00
|
|
|
*/
|
|
|
|
class OutputFormatter implements OutputFormatterInterface
|
|
|
|
{
|
|
|
|
private $decorated;
|
|
|
|
private $styles = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes console output formatter.
|
|
|
|
*
|
2011-04-27 06:25:26 +01:00
|
|
|
* @param Boolean $decorated Whether this formatter should actually decorate strings
|
2011-03-17 15:51:49 +00:00
|
|
|
* @param array $styles Array of "name => FormatterStyle" instance
|
2011-03-24 08:39:53 +00:00
|
|
|
*
|
|
|
|
* @api
|
2011-03-17 15:51:49 +00:00
|
|
|
*/
|
|
|
|
public function __construct($decorated = null, array $styles = array())
|
|
|
|
{
|
|
|
|
$this->decorated = (Boolean) $decorated;
|
|
|
|
|
|
|
|
$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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the decorated flag.
|
|
|
|
*
|
|
|
|
* @param Boolean $decorated Whether to decorated the messages or not
|
2011-03-24 08:39:53 +00:00
|
|
|
*
|
|
|
|
* @api
|
2011-03-17 15:51:49 +00:00
|
|
|
*/
|
|
|
|
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
|
2011-03-17 15:51:49 +00:00
|
|
|
*/
|
|
|
|
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
|
2011-03-17 15:51:49 +00:00
|
|
|
*/
|
|
|
|
public function setStyle($name, OutputFormatterStyleInterface $style)
|
|
|
|
{
|
|
|
|
$this->styles[strtolower($name)] = $style;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if output formatter has style with specified name.
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
*
|
2011-04-27 06:25:26 +01:00
|
|
|
* @return Boolean
|
2011-03-24 08:39:53 +00:00
|
|
|
*
|
|
|
|
* @api
|
2011-03-17 15:51:49 +00:00
|
|
|
*/
|
|
|
|
public function hasStyle($name)
|
|
|
|
{
|
|
|
|
return isset($this->styles[strtolower($name)]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets style options from style with specified name.
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
*
|
|
|
|
* @return OutputFormatterStyleInterface
|
2011-03-24 08:39:53 +00:00
|
|
|
*
|
|
|
|
* @api
|
2011-03-17 15:51:49 +00:00
|
|
|
*/
|
|
|
|
public function getStyle($name)
|
|
|
|
{
|
|
|
|
if (!$this->hasStyle($name)) {
|
|
|
|
throw new \InvalidArgumentException('Undefined style: ' . $name);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->styles[strtolower($name)];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Formats a message according to the given styles.
|
|
|
|
*
|
|
|
|
* @param string $message The message to style
|
|
|
|
*
|
|
|
|
* @return string The styled message
|
2011-03-24 08:39:53 +00:00
|
|
|
*
|
|
|
|
* @api
|
2011-03-17 15:51:49 +00:00
|
|
|
*/
|
|
|
|
public function format($message)
|
|
|
|
{
|
2011-06-18 00:24:46 +01:00
|
|
|
return preg_replace_callback('#<([a-z][a-z0-9_=;-]+)>(.*?)</\\1?>#is', array($this, 'replaceStyle'), $message);
|
2011-03-17 15:51:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-05-23 15:41:34 +01:00
|
|
|
* Replaces style of the output.
|
2011-03-17 15:51:49 +00:00
|
|
|
*
|
|
|
|
* @param array $match
|
|
|
|
*
|
|
|
|
* @return string The replaced style
|
|
|
|
*/
|
2011-05-23 15:41:34 +01:00
|
|
|
private function replaceStyle($match)
|
2011-03-17 15:51:49 +00:00
|
|
|
{
|
|
|
|
if (!$this->isDecorated()) {
|
2011-05-23 15:41:34 +01:00
|
|
|
return $match[2];
|
2011-03-17 15:51:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($this->styles[strtolower($match[1])])) {
|
|
|
|
$style = $this->styles[strtolower($match[1])];
|
|
|
|
} else {
|
|
|
|
$style = $this->createStyleFromString($match[1]);
|
|
|
|
|
|
|
|
if (false === $style) {
|
|
|
|
return $match[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-03 21:48:04 +01:00
|
|
|
return $style->apply($this->format($match[2]));
|
2011-03-17 15:51:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-05-29 23:24:32 +01:00
|
|
|
* Tries to create new style instance from string.
|
2011-03-17 15:51:49 +00:00
|
|
|
*
|
|
|
|
* @param string $string
|
|
|
|
*
|
2011-04-27 06:25:26 +01:00
|
|
|
* @return Symfony\Component\Console\Format\FormatterStyle|Boolean false if string is not format string
|
2011-03-17 15:51:49 +00:00
|
|
|
*/
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|