You are here

public function OutputFormatter::format in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/symfony/console/Formatter/OutputFormatter.php \Symfony\Component\Console\Formatter\OutputFormatter::format()

Formats a message according to the given styles.

Parameters

string $message The message to style:

Return value

string The styled message

Overrides OutputFormatterInterface::format

File

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

Class

OutputFormatter
Formatter class for console output.

Namespace

Symfony\Component\Console\Formatter

Code

public function format($message) {
  $message = (string) $message;
  $offset = 0;
  $output = '';
  $tagRegex = '[a-z][a-z0-9_=;-]*';
  preg_match_all("#<(({$tagRegex}) | /({$tagRegex})?)>#ix", $message, $matches, PREG_OFFSET_CAPTURE);
  foreach ($matches[0] as $i => $match) {
    $pos = $match[1];
    $text = $match[0];
    if (0 != $pos && '\\' == $message[$pos - 1]) {
      continue;
    }

    // add the text up to the next tag
    $output .= $this
      ->applyCurrentStyle(substr($message, $offset, $pos - $offset));
    $offset = $pos + strlen($text);

    // opening tag?
    if ($open = '/' != $text[1]) {
      $tag = $matches[1][$i][0];
    }
    else {
      $tag = isset($matches[3][$i][0]) ? $matches[3][$i][0] : '';
    }
    if (!$open && !$tag) {

      // </>
      $this->styleStack
        ->pop();
    }
    elseif (false === ($style = $this
      ->createStyleFromString(strtolower($tag)))) {
      $output .= $this
        ->applyCurrentStyle($text);
    }
    elseif ($open) {
      $this->styleStack
        ->push($style);
    }
    else {
      $this->styleStack
        ->pop($style);
    }
  }
  $output .= $this
    ->applyCurrentStyle(substr($message, $offset));
  return str_replace('\\<', '<', $output);
}