Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. OutputFormatterStyleStack.php

class OutputFormatterStyleStack

@author Jean-François Simon <contact@jfsimon.fr>

Hierarchy

  • class \Symfony\Component\Console\Formatter\OutputFormatterStyleStack implements \Symfony\Contracts\Service\ResetInterface

Expanded class hierarchy of OutputFormatterStyleStack

File

vendor/symfony/console/Formatter/OutputFormatterStyleStack.php, line 20

Namespace

Symfony\Component\Console\Formatter
View source
class OutputFormatterStyleStack implements ResetInterface {
    
    /**
     * @var OutputFormatterStyleInterface[]
     */
    private array $styles = [];
    private OutputFormatterStyleInterface $emptyStyle;
    public function __construct(?OutputFormatterStyleInterface $emptyStyle = null) {
        $this->emptyStyle = $emptyStyle ?? new OutputFormatterStyle();
        $this->reset();
    }
    
    /**
     * Resets stack (ie. empty internal arrays).
     */
    public function reset() : void {
        $this->styles = [];
    }
    
    /**
     * Pushes a style in the stack.
     */
    public function push(OutputFormatterStyleInterface $style) : void {
        $this->styles[] = $style;
    }
    
    /**
     * Pops a style from the stack.
     *
     * @throws InvalidArgumentException When style tags incorrectly nested
     */
    public function pop(?OutputFormatterStyleInterface $style = null) : OutputFormatterStyleInterface {
        if (!$this->styles) {
            return $this->emptyStyle;
        }
        if (null === $style) {
            return array_pop($this->styles);
        }
        foreach (array_reverse($this->styles, true) as $index => $stackedStyle) {
            if ($style->apply('') === $stackedStyle->apply('')) {
                $this->styles = \array_slice($this->styles, 0, $index);
                return $stackedStyle;
            }
        }
        throw new InvalidArgumentException('Incorrectly nested style tag found.');
    }
    
    /**
     * Computes current style with stacks top codes.
     */
    public function getCurrent() : OutputFormatterStyleInterface {
        if (!$this->styles) {
            return $this->emptyStyle;
        }
        return $this->styles[\count($this->styles) - 1];
    }
    
    /**
     * @return $this
     */
    public function setEmptyStyle(OutputFormatterStyleInterface $emptyStyle) : static {
        $this->emptyStyle = $emptyStyle;
        return $this;
    }
    public function getEmptyStyle() : OutputFormatterStyleInterface {
        return $this->emptyStyle;
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
OutputFormatterStyleStack::$emptyStyle private property
OutputFormatterStyleStack::$styles private property
OutputFormatterStyleStack::getCurrent public function Computes current style with stacks top codes.
OutputFormatterStyleStack::getEmptyStyle public function
OutputFormatterStyleStack::pop public function Pops a style from the stack.
OutputFormatterStyleStack::push public function Pushes a style in the stack.
OutputFormatterStyleStack::reset public function Resets stack (ie. empty internal arrays). Overrides ResetInterface::reset
OutputFormatterStyleStack::setEmptyStyle public function
OutputFormatterStyleStack::__construct public function

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal