2015-08-18 00:00:26 +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 ;
2016-04-20 16:56:34 +00:00
use Symfony\Component\Console\Exception\InvalidArgumentException ;
2015-08-18 00:00:26 +00:00
/**
* Formatter style class for defining styles .
*
* @ author Konstantin Kudryashov < ever . zet @ gmail . com >
*/
class OutputFormatterStyle implements OutputFormatterStyleInterface
{
private static $availableForegroundColors = array (
'black' => array ( 'set' => 30 , 'unset' => 39 ),
'red' => array ( 'set' => 31 , 'unset' => 39 ),
'green' => array ( 'set' => 32 , 'unset' => 39 ),
'yellow' => array ( 'set' => 33 , 'unset' => 39 ),
'blue' => array ( 'set' => 34 , 'unset' => 39 ),
'magenta' => array ( 'set' => 35 , 'unset' => 39 ),
'cyan' => array ( 'set' => 36 , 'unset' => 39 ),
'white' => array ( 'set' => 37 , 'unset' => 39 ),
2015-08-27 19:03:05 +00:00
'default' => array ( 'set' => 39 , 'unset' => 39 ),
2015-08-18 00:00:26 +00:00
);
private static $availableBackgroundColors = array (
'black' => array ( 'set' => 40 , 'unset' => 49 ),
'red' => array ( 'set' => 41 , 'unset' => 49 ),
'green' => array ( 'set' => 42 , 'unset' => 49 ),
'yellow' => array ( 'set' => 43 , 'unset' => 49 ),
'blue' => array ( 'set' => 44 , 'unset' => 49 ),
'magenta' => array ( 'set' => 45 , 'unset' => 49 ),
'cyan' => array ( 'set' => 46 , 'unset' => 49 ),
'white' => array ( 'set' => 47 , 'unset' => 49 ),
2015-08-27 19:03:05 +00:00
'default' => array ( 'set' => 49 , 'unset' => 49 ),
2015-08-18 00:00:26 +00:00
);
private static $availableOptions = array (
'bold' => array ( 'set' => 1 , 'unset' => 22 ),
'underscore' => array ( 'set' => 4 , 'unset' => 24 ),
'blink' => array ( 'set' => 5 , 'unset' => 25 ),
'reverse' => array ( 'set' => 7 , 'unset' => 27 ),
'conceal' => array ( 'set' => 8 , 'unset' => 28 ),
);
private $foreground ;
private $background ;
private $options = array ();
/**
* Initializes output formatter style .
*
* @ param string | null $foreground The style foreground color name
* @ param string | null $background The style background color name
* @ param array $options The style options
*/
public function __construct ( $foreground = null , $background = null , array $options = array ())
{
if ( null !== $foreground ) {
$this -> setForeground ( $foreground );
}
if ( null !== $background ) {
$this -> setBackground ( $background );
}
2018-11-23 12:29:20 +00:00
if ( \count ( $options )) {
2015-08-18 00:00:26 +00:00
$this -> setOptions ( $options );
}
}
/**
* Sets style foreground color .
*
* @ param string | null $color The color name
*
2016-04-20 16:56:34 +00:00
* @ throws InvalidArgumentException When the color name isn ' t defined
2015-08-18 00:00:26 +00:00
*/
public function setForeground ( $color = null )
{
if ( null === $color ) {
$this -> foreground = null ;
return ;
}
if ( ! isset ( static :: $availableForegroundColors [ $color ])) {
2018-11-23 12:29:20 +00:00
throw new InvalidArgumentException ( sprintf ( 'Invalid foreground color specified: "%s". Expected one of (%s)' , $color , implode ( ', ' , array_keys ( static :: $availableForegroundColors ))));
2015-08-18 00:00:26 +00:00
}
$this -> foreground = static :: $availableForegroundColors [ $color ];
}
/**
* Sets style background color .
*
* @ param string | null $color The color name
*
2016-04-20 16:56:34 +00:00
* @ throws InvalidArgumentException When the color name isn ' t defined
2015-08-18 00:00:26 +00:00
*/
public function setBackground ( $color = null )
{
if ( null === $color ) {
$this -> background = null ;
return ;
}
if ( ! isset ( static :: $availableBackgroundColors [ $color ])) {
2018-11-23 12:29:20 +00:00
throw new InvalidArgumentException ( sprintf ( 'Invalid background color specified: "%s". Expected one of (%s)' , $color , implode ( ', ' , array_keys ( static :: $availableBackgroundColors ))));
2015-08-18 00:00:26 +00:00
}
$this -> background = static :: $availableBackgroundColors [ $color ];
}
/**
* Sets some specific style option .
*
* @ param string $option The option name
*
2016-04-20 16:56:34 +00:00
* @ throws InvalidArgumentException When the option name isn ' t defined
2015-08-18 00:00:26 +00:00
*/
public function setOption ( $option )
{
if ( ! isset ( static :: $availableOptions [ $option ])) {
2018-11-23 12:29:20 +00:00
throw new InvalidArgumentException ( sprintf ( 'Invalid option specified: "%s". Expected one of (%s)' , $option , implode ( ', ' , array_keys ( static :: $availableOptions ))));
2015-08-18 00:00:26 +00:00
}
2018-11-23 12:29:20 +00:00
if ( ! \in_array ( static :: $availableOptions [ $option ], $this -> options )) {
2015-08-18 00:00:26 +00:00
$this -> options [] = static :: $availableOptions [ $option ];
}
}
/**
* Unsets some specific style option .
*
* @ param string $option The option name
*
2016-04-20 16:56:34 +00:00
* @ throws InvalidArgumentException When the option name isn ' t defined
2015-08-18 00:00:26 +00:00
*/
public function unsetOption ( $option )
{
if ( ! isset ( static :: $availableOptions [ $option ])) {
2018-11-23 12:29:20 +00:00
throw new InvalidArgumentException ( sprintf ( 'Invalid option specified: "%s". Expected one of (%s)' , $option , implode ( ', ' , array_keys ( static :: $availableOptions ))));
2015-08-18 00:00:26 +00:00
}
$pos = array_search ( static :: $availableOptions [ $option ], $this -> options );
if ( false !== $pos ) {
unset ( $this -> options [ $pos ]);
}
}
/**
2018-11-23 12:29:20 +00:00
* { @ inheritdoc }
2015-08-18 00:00:26 +00:00
*/
public function setOptions ( array $options )
{
$this -> options = array ();
foreach ( $options as $option ) {
$this -> setOption ( $option );
}
}
/**
* Applies the style to a given text .
*
* @ param string $text The text to style
*
* @ return string
*/
public function apply ( $text )
{
$setCodes = array ();
$unsetCodes = array ();
if ( null !== $this -> foreground ) {
$setCodes [] = $this -> foreground [ 'set' ];
$unsetCodes [] = $this -> foreground [ 'unset' ];
}
if ( null !== $this -> background ) {
$setCodes [] = $this -> background [ 'set' ];
$unsetCodes [] = $this -> background [ 'unset' ];
}
2018-11-23 12:29:20 +00:00
if ( \count ( $this -> options )) {
2015-08-18 00:00:26 +00:00
foreach ( $this -> options as $option ) {
$setCodes [] = $option [ 'set' ];
$unsetCodes [] = $option [ 'unset' ];
}
}
2018-11-23 12:29:20 +00:00
if ( 0 === \count ( $setCodes )) {
2015-08-18 00:00:26 +00:00
return $text ;
}
return sprintf ( " \033 [%sm%s \033 [%sm " , implode ( ';' , $setCodes ), $text , implode ( ';' , $unsetCodes ));
}
}