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

Breadcrumb

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

function OutputFormatter::createStyleFromString

Tries to create new style instance from string.

1 call to OutputFormatter::createStyleFromString()
OutputFormatter::formatAndWrap in vendor/symfony/console/Formatter/OutputFormatter.php
Formats a message according to the given styles, wrapping at `$width` (0 means no wrapping).

File

vendor/symfony/console/Formatter/OutputFormatter.php, line 175

Class

OutputFormatter
Formatter class for console output.

Namespace

Symfony\Component\Console\Formatter

Code

private function createStyleFromString(string $string) : ?OutputFormatterStyleInterface {
    if (isset($this->styles[$string])) {
        return $this->styles[$string];
    }
    if (!preg_match_all('/([^=]+)=([^;]+)(;|$)/', $string, $matches, \PREG_SET_ORDER)) {
        return null;
    }
    $style = new OutputFormatterStyle();
    foreach ($matches as $match) {
        array_shift($match);
        $match[0] = strtolower($match[0]);
        if ('fg' == $match[0]) {
            $style->setForeground(strtolower($match[1]));
        }
        elseif ('bg' == $match[0]) {
            $style->setBackground(strtolower($match[1]));
        }
        elseif ('href' === $match[0]) {
            $url = preg_replace('{\\\\([<>])}', '$1', $match[1]);
            $style->setHref($url);
        }
        elseif ('options' === $match[0]) {
            preg_match_all('([^,;]+)', strtolower($match[1]), $options);
            $options = array_shift($options);
            foreach ($options as $option) {
                $style->setOption($option);
            }
        }
        else {
            return null;
        }
    }
    return $style;
}

API Navigation

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