| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPConsole\Core\Console\Psy\Output; |
| 4 |
|
| 5 |
use Symfony\Component\Console\Exception\InvalidArgumentException; |
| 6 |
use Symfony\Component\Console\Formatter\OutputFormatterStyle as SymfonyOutputFormatterStyle; |
| 7 |
|
| 8 |
/** |
| 9 |
* Override version of Symphony OutputFormatterStyle |
| 10 |
* Everything is except: |
| 11 |
* 1) The available static properties |
| 12 |
* 2) The return statement of apply method |
| 13 |
* 3) The coding style |
| 14 |
*/ |
| 15 |
class OutputFormatterStyle extends SymfonyOutputFormatterStyle { |
| 16 |
private static $availableForegroundColors = [ |
| 17 |
'black' => [ 'set' => '<span class="wp-console-color-black">', 'unset' => '</span>' ], |
| 18 |
'red' => [ 'set' => '<span class="wp-console-color-red">', 'unset' => '</span>' ], |
| 19 |
'green' => [ 'set' => '<span class="wp-console-color-green">', 'unset' => '</span>' ], |
| 20 |
'yellow' => [ 'set' => '<span class="wp-console-color-yellow">', 'unset' => '</span>' ], |
| 21 |
'blue' => [ 'set' => '<span class="wp-console-color-blue">', 'unset' => '</span>' ], |
| 22 |
'magenta' => [ 'set' => '<span class="wp-console-color-magenta">', 'unset' => '</span>' ], |
| 23 |
'cyan' => [ 'set' => '<span class="wp-console-color-cyan">', 'unset' => '</span>' ], |
| 24 |
'white' => [ 'set' => '<span class="wp-console-color-white">', 'unset' => '</span>' ], |
| 25 |
'default' => [ 'set' => '<span class="wp-console-color-default">', 'unset' => '</span>' ], |
| 26 |
]; |
| 27 |
|
| 28 |
private static $availableBackgroundColors = [ |
| 29 |
'black' => [ 'set' => '<span class="wp-console-color-black-bg">', 'unset' => '</span>' ], |
| 30 |
'red' => [ 'set' => '<span class="wp-console-color-red-bg">', 'unset' => '</span>' ], |
| 31 |
'green' => [ 'set' => '<span class="wp-console-color-green-bg">', 'unset' => '</span>' ], |
| 32 |
'yellow' => [ 'set' => '<span class="wp-console-color-yellow-bg">', 'unset' => '</span>' ], |
| 33 |
'blue' => [ 'set' => '<span class="wp-console-color-blue-bg">', 'unset' => '</span>' ], |
| 34 |
'magenta' => [ 'set' => '<span class="wp-console-color-magenta-bg">', 'unset' => '</span>' ], |
| 35 |
'cyan' => [ 'set' => '<span class="wp-console-color-cyan-bg">', 'unset' => '</span>' ], |
| 36 |
'white' => [ 'set' => '<span class="wp-console-color-white-bg">', 'unset' => '</span>' ], |
| 37 |
'default' => [ 'set' => '<span class="wp-console-color-default-bg">', 'unset' => '</span>' ], |
| 38 |
]; |
| 39 |
|
| 40 |
private static $availableOptions = [ |
| 41 |
'bold' => [ 'set' => '<span class="wp-console-color-bold">', 'unset' => '</span>' ], |
| 42 |
'underscore' => [ 'set' => '<span class="wp-console-color-underscore">', 'unset' => '</span>' ], |
| 43 |
'blink' => [ 'set' => '<span class="wp-console-color-blink">', 'unset' => '</span>' ], |
| 44 |
'reverse' => [ 'set' => '<span class="wp-console-color-reverse">', 'unset' => '</span>' ], |
| 45 |
'conceal' => [ 'set' => '<span class="wp-console-color-conceal">', 'unset' => '</span>' ], |
| 46 |
]; |
| 47 |
|
| 48 |
private $foreground; |
| 49 |
private $background; |
| 50 |
private $options = []; |
| 51 |
|
| 52 |
/** |
| 53 |
* Initializes output formatter style. |
| 54 |
* |
| 55 |
* @param string|null $foreground The style foreground color name |
| 56 |
* @param string|null $background The style background color name |
| 57 |
* @param array $options The style options |
| 58 |
*/ |
| 59 |
public function __construct( $foreground = null, $background = null, $options = [] ) { |
| 60 |
if ( null !== $foreground ) { |
| 61 |
$this->setForeground( $foreground ); |
| 62 |
} |
| 63 |
|
| 64 |
if ( null !== $background ) { |
| 65 |
$this->setBackground( $background ); |
| 66 |
} |
| 67 |
|
| 68 |
if ( \count( $options ) ) { |
| 69 |
$this->setOptions( $options ); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Sets style foreground color. |
| 75 |
* |
| 76 |
* @since 1.0.0 |
| 77 |
* |
| 78 |
* @param string|null $color The color name |
| 79 |
* |
| 80 |
* @throws InvalidArgumentException When the color name isn't defined |
| 81 |
* |
| 82 |
* @return void |
| 83 |
*/ |
| 84 |
public function setForeground( $color = null ) { |
| 85 |
if (null === $color) { |
| 86 |
$this->foreground = null; |
| 87 |
|
| 88 |
return; |
| 89 |
} |
| 90 |
|
| 91 |
if (!isset(static::$availableForegroundColors[$color])) { |
| 92 |
throw new InvalidArgumentException(sprintf('Invalid foreground color specified: "%s". Expected one of (%s)', $color, implode(', ', array_keys(static::$availableForegroundColors)))); |
| 93 |
} |
| 94 |
|
| 95 |
$this->foreground = static::$availableForegroundColors[$color]; |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Sets style background color. |
| 100 |
* |
| 101 |
* @param string|null $color The color name |
| 102 |
* |
| 103 |
* @throws InvalidArgumentException When the color name isn't defined |
| 104 |
*/ |
| 105 |
public function setBackground( $color = null ) { |
| 106 |
if ( null === $color ) { |
| 107 |
$this->background = null; |
| 108 |
|
| 109 |
return; |
| 110 |
} |
| 111 |
|
| 112 |
if ( ! isset( static::$availableBackgroundColors[ $color ] ) ) { |
| 113 |
throw new InvalidArgumentException( sprintf( 'Invalid background color specified: "%s". Expected one of (%s)', $color, implode( ', ', array_keys( static::$availableBackgroundColors ) ) ) ); |
| 114 |
} |
| 115 |
|
| 116 |
$this->background = static::$availableBackgroundColors[ $color ]; |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Sets some specific style option. |
| 121 |
* |
| 122 |
* @since 1.0.0 |
| 123 |
* |
| 124 |
* @param string $option The option name |
| 125 |
* |
| 126 |
* @throws InvalidArgumentException When the option name isn't defined |
| 127 |
* |
| 128 |
* @return void |
| 129 |
*/ |
| 130 |
public function setOption( $option ) { |
| 131 |
if ( ! isset( static::$availableOptions[ $option ] ) ) { |
| 132 |
throw new InvalidArgumentException( sprintf( 'Invalid option specified: "%s". Expected one of (%s)', $option, implode( ', ', array_keys( static::$availableOptions ) ) ) ); |
| 133 |
} |
| 134 |
|
| 135 |
if ( ! \in_array( static::$availableOptions[ $option ], $this->options ) ) { |
| 136 |
$this->options[] = static::$availableOptions[ $option ]; |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Unsets some specific style option. |
| 142 |
* |
| 143 |
* @since 1.0.0 |
| 144 |
* |
| 145 |
* @param string $option The option name |
| 146 |
* |
| 147 |
* @throws InvalidArgumentException When the option name isn't defined |
| 148 |
* |
| 149 |
* @return void |
| 150 |
*/ |
| 151 |
public function unsetOption( $option ) { |
| 152 |
if ( ! isset( static::$availableOptions[ $option ] ) ) { |
| 153 |
throw new InvalidArgumentException( sprintf( 'Invalid option specified: "%s". Expected one of (%s)', $option, implode( ', ', array_keys( static::$availableOptions ) ) ) ); |
| 154 |
} |
| 155 |
|
| 156 |
$pos = array_search( static::$availableOptions[ $option ], $this->options ); |
| 157 |
if ( false !== $pos ) { |
| 158 |
unset( $this->options[ $pos ] ); |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
/** |
| 163 |
* Sets multiple style options at once. |
| 164 |
* |
| 165 |
* @since 1.0.0 |
| 166 |
* |
| 167 |
* @return void |
| 168 |
*/ |
| 169 |
public function setOptions( array $options ) { |
| 170 |
$this->options = []; |
| 171 |
|
| 172 |
foreach ( $options as $option ) { |
| 173 |
$this->setOption( $option ); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* Applies the style to a given text. |
| 179 |
* |
| 180 |
* @since 1.0.0 |
| 181 |
* |
| 182 |
* @param string $text The text to style |
| 183 |
* |
| 184 |
* @return string |
| 185 |
*/ |
| 186 |
public function apply( $text ) { |
| 187 |
$setCodes = []; |
| 188 |
$unsetCodes = []; |
| 189 |
|
| 190 |
if ( null !== $this->foreground ) { |
| 191 |
$setCodes[] = $this->foreground['set']; |
| 192 |
$unsetCodes[] = $this->foreground['unset']; |
| 193 |
} |
| 194 |
|
| 195 |
if ( null !== $this->background ) { |
| 196 |
$setCodes[] = $this->background['set']; |
| 197 |
$unsetCodes[] = $this->background['unset']; |
| 198 |
} |
| 199 |
|
| 200 |
if ( \count( $this->options ) ) { |
| 201 |
foreach ( $this->options as $option ) { |
| 202 |
$setCodes[] = $option['set']; |
| 203 |
$unsetCodes[] = $option['unset']; |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
if ( 0 === \count( $setCodes ) ) { |
| 208 |
return $text; |
| 209 |
} |
| 210 |
|
| 211 |
return sprintf( '%s%s%s', implode( '', $setCodes ), $text, implode( '', $unsetCodes ) ); |
| 212 |
} |
| 213 |
} |
| 214 |
|