Verzeichnisstruktur phpBB-3.2.0


Veröffentlicht
06.01.2017

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:56 - Dateigröße: 6.56 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  use Symfony\Component\Console\Exception\InvalidArgumentException;
015   
016  /**
017   * Formatter class for console output.
018   *
019   * @author Konstantin Kudryashov <ever.zet@gmail.com>
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          $text = preg_replace('/([^\\\\]?)</', '$1\\<', $text);
037   
038          if ('\\' === substr($text, -1)) {
039              $len = strlen($text);
040              $text = rtrim($text, '\\');
041              $text .= str_repeat('<<', $len - strlen($text));
042          }
043   
044          return $text;
045      }
046   
047      /**
048       * Initializes console output formatter.
049       *
050       * @param bool                            $decorated Whether this formatter should actually decorate strings
051       * @param OutputFormatterStyleInterface[] $styles    Array of "name => FormatterStyle" instances
052       */
053      public function __construct($decorated = false, array $styles = array())
054      {
055          $this->decorated = (bool) $decorated;
056   
057          $this->setStyle('error', new OutputFormatterStyle('white', 'red'));
058          $this->setStyle('info', new OutputFormatterStyle('green'));
059          $this->setStyle('comment', new OutputFormatterStyle('yellow'));
060          $this->setStyle('question', new OutputFormatterStyle('black', 'cyan'));
061   
062          foreach ($styles as $name => $style) {
063              $this->setStyle($name, $style);
064          }
065   
066          $this->styleStack = new OutputFormatterStyleStack();
067      }
068   
069      /**
070       * Sets the decorated flag.
071       *
072       * @param bool $decorated Whether to decorate the messages or not
073       */
074      public function setDecorated($decorated)
075      {
076          $this->decorated = (bool) $decorated;
077      }
078   
079      /**
080       * Gets the decorated flag.
081       *
082       * @return bool true if the output will decorate messages, false otherwise
083       */
084      public function isDecorated()
085      {
086          return $this->decorated;
087      }
088   
089      /**
090       * Sets a new style.
091       *
092       * @param string                        $name  The style name
093       * @param OutputFormatterStyleInterface $style The style instance
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      public function hasStyle($name)
108      {
109          return isset($this->styles[strtolower($name)]);
110      }
111   
112      /**
113       * Gets style options from style with specified name.
114       *
115       * @param string $name
116       *
117       * @return OutputFormatterStyleInterface
118       *
119       * @throws InvalidArgumentException When style isn't defined
120       */
121      public function getStyle($name)
122      {
123          if (!$this->hasStyle($name)) {
124              throw new InvalidArgumentException(sprintf('Undefined style: %s', $name));
125          }
126   
127          return $this->styles[strtolower($name)];
128      }
129   
130      /**
131       * Formats a message according to the given styles.
132       *
133       * @param string $message The message to style
134       *
135       * @return string The styled message
136       */
137      public function format($message)
138      {
139          $message = (string) $message;
140          $offset = 0;
141          $output = '';
142          $tagRegex = '[a-z][a-z0-9_=;-]*+';
143          preg_match_all("#<(($tagRegex) | /($tagRegex)?)>#ix", $message, $matches, PREG_OFFSET_CAPTURE);
144          foreach ($matches[0] as $i => $match) {
145              $pos = $match[1];
146              $text = $match[0];
147   
148              if (0 != $pos && '\\' == $message[$pos - 1]) {
149                  continue;
150              }
151   
152              // add the text up to the next tag
153              $output .= $this->applyCurrentStyle(substr($message, $offset, $pos - $offset));
154              $offset = $pos + strlen($text);
155   
156              // opening tag?
157              if ($open = '/' != $text[1]) {
158                  $tag = $matches[1][$i][0];
159              } else {
160                  $tag = isset($matches[3][$i][0]) ? $matches[3][$i][0] : '';
161              }
162   
163              if (!$open && !$tag) {
164                  // </>
165                  $this->styleStack->pop();
166              } elseif (false === $style = $this->createStyleFromString(strtolower($tag))) {
167                  $output .= $this->applyCurrentStyle($text);
168              } elseif ($open) {
169                  $this->styleStack->push($style);
170              } else {
171                  $this->styleStack->pop($style);
172              }
173          }
174   
175          $output .= $this->applyCurrentStyle(substr($message, $offset));
176   
177          if (false !== strpos($output, '<<')) {
178              return strtr($output, array('\\<' => '<', '<<' => '\\'));
179          }
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