Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\Console\Formatter; Chris@0: Chris@0: use Symfony\Component\Console\Exception\InvalidArgumentException; Chris@0: Chris@0: /** Chris@0: * Formatter style class for defining styles. Chris@0: * Chris@0: * @author Konstantin Kudryashov Chris@0: */ Chris@0: class OutputFormatterStyle implements OutputFormatterStyleInterface Chris@0: { Chris@17: private static $availableForegroundColors = [ Chris@17: 'black' => ['set' => 30, 'unset' => 39], Chris@17: 'red' => ['set' => 31, 'unset' => 39], Chris@17: 'green' => ['set' => 32, 'unset' => 39], Chris@17: 'yellow' => ['set' => 33, 'unset' => 39], Chris@17: 'blue' => ['set' => 34, 'unset' => 39], Chris@17: 'magenta' => ['set' => 35, 'unset' => 39], Chris@17: 'cyan' => ['set' => 36, 'unset' => 39], Chris@17: 'white' => ['set' => 37, 'unset' => 39], Chris@17: 'default' => ['set' => 39, 'unset' => 39], Chris@17: ]; Chris@17: private static $availableBackgroundColors = [ Chris@17: 'black' => ['set' => 40, 'unset' => 49], Chris@17: 'red' => ['set' => 41, 'unset' => 49], Chris@17: 'green' => ['set' => 42, 'unset' => 49], Chris@17: 'yellow' => ['set' => 43, 'unset' => 49], Chris@17: 'blue' => ['set' => 44, 'unset' => 49], Chris@17: 'magenta' => ['set' => 45, 'unset' => 49], Chris@17: 'cyan' => ['set' => 46, 'unset' => 49], Chris@17: 'white' => ['set' => 47, 'unset' => 49], Chris@17: 'default' => ['set' => 49, 'unset' => 49], Chris@17: ]; Chris@17: private static $availableOptions = [ Chris@17: 'bold' => ['set' => 1, 'unset' => 22], Chris@17: 'underscore' => ['set' => 4, 'unset' => 24], Chris@17: 'blink' => ['set' => 5, 'unset' => 25], Chris@17: 'reverse' => ['set' => 7, 'unset' => 27], Chris@17: 'conceal' => ['set' => 8, 'unset' => 28], Chris@17: ]; Chris@0: Chris@0: private $foreground; Chris@0: private $background; Chris@17: private $options = []; Chris@0: Chris@0: /** Chris@0: * Initializes output formatter style. Chris@0: * Chris@0: * @param string|null $foreground The style foreground color name Chris@0: * @param string|null $background The style background color name Chris@0: * @param array $options The style options Chris@0: */ Chris@17: public function __construct($foreground = null, $background = null, array $options = []) Chris@0: { Chris@0: if (null !== $foreground) { Chris@0: $this->setForeground($foreground); Chris@0: } Chris@0: if (null !== $background) { Chris@0: $this->setBackground($background); Chris@0: } Chris@17: if (\count($options)) { Chris@0: $this->setOptions($options); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets style foreground color. Chris@0: * Chris@0: * @param string|null $color The color name Chris@0: * Chris@0: * @throws InvalidArgumentException When the color name isn't defined Chris@0: */ Chris@0: public function setForeground($color = null) Chris@0: { Chris@0: if (null === $color) { Chris@0: $this->foreground = null; Chris@0: Chris@0: return; Chris@0: } Chris@0: Chris@0: if (!isset(static::$availableForegroundColors[$color])) { Chris@17: throw new InvalidArgumentException(sprintf('Invalid foreground color specified: "%s". Expected one of (%s)', $color, implode(', ', array_keys(static::$availableForegroundColors)))); Chris@0: } Chris@0: Chris@0: $this->foreground = static::$availableForegroundColors[$color]; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets style background color. Chris@0: * Chris@0: * @param string|null $color The color name Chris@0: * Chris@0: * @throws InvalidArgumentException When the color name isn't defined Chris@0: */ Chris@0: public function setBackground($color = null) Chris@0: { Chris@0: if (null === $color) { Chris@0: $this->background = null; Chris@0: Chris@0: return; Chris@0: } Chris@0: Chris@0: if (!isset(static::$availableBackgroundColors[$color])) { Chris@17: throw new InvalidArgumentException(sprintf('Invalid background color specified: "%s". Expected one of (%s)', $color, implode(', ', array_keys(static::$availableBackgroundColors)))); Chris@0: } Chris@0: Chris@0: $this->background = static::$availableBackgroundColors[$color]; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets some specific style option. Chris@0: * Chris@0: * @param string $option The option name Chris@0: * Chris@0: * @throws InvalidArgumentException When the option name isn't defined Chris@0: */ Chris@0: public function setOption($option) Chris@0: { Chris@0: if (!isset(static::$availableOptions[$option])) { Chris@17: throw new InvalidArgumentException(sprintf('Invalid option specified: "%s". Expected one of (%s)', $option, implode(', ', array_keys(static::$availableOptions)))); Chris@0: } Chris@0: Chris@17: if (!\in_array(static::$availableOptions[$option], $this->options)) { Chris@0: $this->options[] = static::$availableOptions[$option]; Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Unsets some specific style option. Chris@0: * Chris@0: * @param string $option The option name Chris@0: * Chris@0: * @throws InvalidArgumentException When the option name isn't defined Chris@0: */ Chris@0: public function unsetOption($option) Chris@0: { Chris@0: if (!isset(static::$availableOptions[$option])) { Chris@17: throw new InvalidArgumentException(sprintf('Invalid option specified: "%s". Expected one of (%s)', $option, implode(', ', array_keys(static::$availableOptions)))); Chris@0: } Chris@0: Chris@0: $pos = array_search(static::$availableOptions[$option], $this->options); Chris@0: if (false !== $pos) { Chris@0: unset($this->options[$pos]); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@14: * {@inheritdoc} Chris@0: */ Chris@0: public function setOptions(array $options) Chris@0: { Chris@17: $this->options = []; Chris@0: Chris@0: foreach ($options as $option) { Chris@0: $this->setOption($option); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Applies the style to a given text. Chris@0: * Chris@0: * @param string $text The text to style Chris@0: * Chris@0: * @return string Chris@0: */ Chris@0: public function apply($text) Chris@0: { Chris@17: $setCodes = []; Chris@17: $unsetCodes = []; Chris@0: Chris@0: if (null !== $this->foreground) { Chris@0: $setCodes[] = $this->foreground['set']; Chris@0: $unsetCodes[] = $this->foreground['unset']; Chris@0: } Chris@0: if (null !== $this->background) { Chris@0: $setCodes[] = $this->background['set']; Chris@0: $unsetCodes[] = $this->background['unset']; Chris@0: } Chris@17: if (\count($this->options)) { Chris@0: foreach ($this->options as $option) { Chris@0: $setCodes[] = $option['set']; Chris@0: $unsetCodes[] = $option['unset']; Chris@0: } Chris@0: } Chris@0: Chris@17: if (0 === \count($setCodes)) { Chris@0: return $text; Chris@0: } Chris@0: Chris@0: return sprintf("\033[%sm%s\033[%sm", implode(';', $setCodes), $text, implode(';', $unsetCodes)); Chris@0: } Chris@0: }