Verzeichnisstruktur phpBB-3.1.0


Veröffentlicht
27.10.2014

So funktioniert es


Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück

Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis.
Auf den Verzeichnisnamen klicken, dies zeigt nur das Verzeichnis mit Inhalt an

(Beispiel Datei-Icons)

Auf das Icon klicken um den Quellcode anzuzeigen

OutputFormatter.php

Zuletzt modifiziert: 09.10.2024, 12:58 - Dateigröße: 6.32 KiB


001  <?php
002   
003  /*
004   * This file is part of the Symfony package.
005   *
006   * (c) Fabien Potencier <fabien@symfony.com>
007   *
008   * For the full copyright and license information, please view the LICENSE
009   * file that was distributed with this source code.
010   */
011   
012  namespace Symfony\Component\Console\Formatter;
013   
014  /**
015   * Formatter class for console output.
016   *
017   * @author Konstantin Kudryashov <ever.zet@gmail.com>
018   *
019   * @api
020   */
021  class OutputFormatter implements OutputFormatterInterface
022  {
023      private $decorated;
024      private $styles = array();
025      private $styleStack;
026   
027      /**
028       * Escapes "<" special char in given text.
029       *
030       * @param string $text Text to escape
031       *
032       * @return string Escaped text
033       */
034      public static function escape($text)
035      {
036          return preg_replace('/([^\\\\]?)</is', '$1\\<', $text);
037      }
038   
039      /**
040       * Initializes console output formatter.
041       *
042       * @param bool             $decorated Whether this formatter should actually decorate strings
043       * @param OutputFormatterStyleInterface[] $styles    Array of "name => FormatterStyle" instances
044       *
045       * @api
046       */
047      public function __construct($decorated = false, array $styles = array())
048      {
049          $this->decorated = (bool) $decorated;
050   
051          $this->setStyle('error', new OutputFormatterStyle('white', 'red'));
052          $this->setStyle('info', new OutputFormatterStyle('green'));
053          $this->setStyle('comment', new OutputFormatterStyle('yellow'));
054          $this->setStyle('question', new OutputFormatterStyle('black', 'cyan'));
055   
056          foreach ($styles as $name => $style) {
057              $this->setStyle($name, $style);
058          }
059   
060          $this->styleStack = new OutputFormatterStyleStack();
061      }
062   
063      /**
064       * Sets the decorated flag.
065       *
066       * @param bool    $decorated Whether to decorate the messages or not
067       *
068       * @api
069       */
070      public function setDecorated($decorated)
071      {
072          $this->decorated = (bool) $decorated;
073      }
074   
075      /**
076       * Gets the decorated flag.
077       *
078       * @return bool    true if the output will decorate messages, false otherwise
079       *
080       * @api
081       */
082      public function isDecorated()
083      {
084          return $this->decorated;
085      }
086   
087      /**
088       * Sets a new style.
089       *
090       * @param string                        $name  The style name
091       * @param OutputFormatterStyleInterface $style The style instance
092       *
093       * @api
094       */
095      public function setStyle($name, OutputFormatterStyleInterface $style)
096      {
097          $this->styles[strtolower($name)] = $style;
098      }
099   
100      /**
101       * Checks if output formatter has style with specified name.
102       *
103       * @param string $name
104       *
105       * @return bool
106       *
107       * @api
108       */
109      public function hasStyle($name)
110      {
111          return isset($this->styles[strtolower($name)]);
112      }
113   
114      /**
115       * Gets style options from style with specified name.
116       *
117       * @param string $name
118       *
119       * @return OutputFormatterStyleInterface
120       *
121       * @throws \InvalidArgumentException When style isn't defined
122       *
123       * @api
124       */
125      public function getStyle($name)
126      {
127          if (!$this->hasStyle($name)) {
128              throw new \InvalidArgumentException(sprintf('Undefined style: %s', $name));
129          }
130   
131          return $this->styles[strtolower($name)];
132      }
133   
134      /**
135       * Formats a message according to the given styles.
136       *
137       * @param string $message The message to style
138       *
139       * @return string The styled message
140       *
141       * @api
142       */
143      public function format($message)
144      {
145          $offset = 0;
146          $output = '';
147          $tagRegex = '[a-z][a-z0-9_=;-]*';
148          preg_match_all("#<(($tagRegex) | /($tagRegex)?)>#isx", $message, $matches, PREG_OFFSET_CAPTURE);
149          foreach ($matches[0] as $i => $match) {
150              $pos = $match[1];
151              $text = $match[0];
152   
153              // add the text up to the next tag
154              $output .= $this->applyCurrentStyle(substr($message, $offset, $pos - $offset));
155              $offset = $pos + strlen($text);
156   
157              // opening tag?
158              if ($open = '/' != $text[1]) {
159                  $tag = $matches[1][$i][0];
160              } else {
161                  $tag = isset($matches[3][$i][0]) ? $matches[3][$i][0] : '';
162              }
163   
164              if (!$open && !$tag) {
165                  // </>
166                  $this->styleStack->pop();
167              } elseif ($pos && '\\' == $message[$pos - 1]) {
168                  // escaped tag
169                  $output .= $this->applyCurrentStyle($text);
170              } elseif (false === $style = $this->createStyleFromString(strtolower($tag))) {
171                  $output .= $this->applyCurrentStyle($text);
172              } elseif ($open) {
173                  $this->styleStack->push($style);
174              } else {
175                  $this->styleStack->pop($style);
176              }
177          }
178   
179          $output .= $this->applyCurrentStyle(substr($message, $offset));
180   
181          return str_replace('\\<', '<', $output);
182      }
183   
184      /**
185       * @return OutputFormatterStyleStack
186       */
187      public function getStyleStack()
188      {
189          return $this->styleStack;
190      }
191   
192      /**
193       * Tries to create new style instance from string.
194       *
195       * @param string $string
196       *
197       * @return OutputFormatterStyle|bool    false if string is not format string
198       */
199      private function createStyleFromString($string)
200      {
201          if (isset($this->styles[$string])) {
202              return $this->styles[$string];
203          }
204   
205          if (!preg_match_all('/([^=]+)=([^;]+)(;|$)/', strtolower($string), $matches, PREG_SET_ORDER)) {
206              return false;
207          }
208   
209          $style = new OutputFormatterStyle();
210          foreach ($matches as $match) {
211              array_shift($match);
212   
213              if ('fg' == $match[0]) {
214                  $style->setForeground($match[1]);
215              } elseif ('bg' == $match[0]) {
216                  $style->setBackground($match[1]);
217              } else {
218                  try {
219                      $style->setOption($match[1]);
220                  } catch (\InvalidArgumentException $e) {
221                      return false;
222                  }
223              }
224          }
225   
226          return $style;
227      }
228   
229      /**
230       * Applies current style from stack to text, if must be applied.
231       *
232       * @param string $text Input text
233       *
234       * @return string Styled text
235       */
236      private function applyCurrentStyle($text)
237      {
238          return $this->isDecorated() && strlen($text) > 0 ? $this->styleStack->getCurrent()->apply($text) : $text;
239      }
240  }
241