think\console\output\Formatter::format PHP Méthode

format() public méthode

使用所给的样式格式化文字
public format ( string $message ) : string
$message string 文字
Résultat string
    public function format($message)
    {
        $offset = 0;
        $output = '';
        $tagRegex = '[a-z][a-z0-9_=;-]*';
        preg_match_all("#<(({$tagRegex}) | /({$tagRegex})?)>#isx", $message, $matches, PREG_OFFSET_CAPTURE);
        foreach ($matches[0] as $i => $match) {
            $pos = $match[1];
            $text = $match[0];
            if (0 != $pos && '\\' == $message[$pos - 1]) {
                continue;
            }
            $output .= $this->applyCurrentStyle(substr($message, $offset, $pos - $offset));
            $offset = $pos + strlen($text);
            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);
    }

Usage Example

Exemple #1
0
 public function write($messages, $newline = false, $type = Output::OUTPUT_NORMAL, $stream = null)
 {
     if (Output::VERBOSITY_QUIET === $this->output->getVerbosity()) {
         return;
     }
     $messages = (array) $messages;
     foreach ($messages as $message) {
         switch ($type) {
             case Output::OUTPUT_NORMAL:
                 $message = $this->formatter->format($message);
                 break;
             case Output::OUTPUT_RAW:
                 break;
             case Output::OUTPUT_PLAIN:
                 $message = strip_tags($this->formatter->format($message));
                 break;
             default:
                 throw new \InvalidArgumentException(sprintf('Unknown output type given (%s)', $type));
         }
         $this->doWrite($message, $newline, $stream);
     }
 }
All Usage Examples Of think\console\output\Formatter::format