Symfony\Component\Console\Formatter\OutputFormatter::format PHP Method

format() public method

Formats a message according to the given styles.
public format ( string $message ) : string
$message string The message to style
return string The styled message
    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));
        if (false !== strpos($output, '<<')) {
            return strtr($output, array('\\<' => '<', '<<' => '\\'));
        }
        return str_replace('\\<', '<', $output);
    }

Usage Example

コード例 #1
0
ファイル: AnsiFormatter.php プロジェクト: webmozart/console
 /**
  * {@inheritdoc}
  */
 public function removeFormat($string)
 {
     $this->innerFormatter->setDecorated(false);
     $formatted = $this->innerFormatter->format($string);
     $this->innerFormatter->setDecorated(true);
     return $formatted;
 }
All Usage Examples Of Symfony\Component\Console\Formatter\OutputFormatter::format