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

ProgressIndicator.php

Zuletzt modifiziert: 09.10.2024, 12:56 - Dateigröße: 8.45 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\Helper;
013   
014  use Symfony\Component\Console\Exception\InvalidArgumentException;
015  use Symfony\Component\Console\Exception\LogicException;
016  use Symfony\Component\Console\Output\OutputInterface;
017   
018  /**
019   * @author Kevin Bond <kevinbond@gmail.com>
020   */
021  class ProgressIndicator
022  {
023      private $output;
024      private $startTime;
025      private $format;
026      private $message;
027      private $indicatorValues;
028      private $indicatorCurrent;
029      private $indicatorChangeInterval;
030      private $indicatorUpdateTime;
031      private $started = false;
032   
033      private static $formatters;
034      private static $formats;
035   
036      /**
037       * @param OutputInterface $output
038       * @param string|null     $format                  Indicator format
039       * @param int             $indicatorChangeInterval Change interval in milliseconds
040       * @param array|null      $indicatorValues         Animated indicator characters
041       */
042      public function __construct(OutputInterface $output, $format = null, $indicatorChangeInterval = 100, $indicatorValues = null)
043      {
044          $this->output = $output;
045   
046          if (null === $format) {
047              $format = $this->determineBestFormat();
048          }
049   
050          if (null === $indicatorValues) {
051              $indicatorValues = array('-', '\\', '|', '/');
052          }
053   
054          $indicatorValues = array_values($indicatorValues);
055   
056          if (2 > count($indicatorValues)) {
057              throw new InvalidArgumentException('Must have at least 2 indicator value characters.');
058          }
059   
060          $this->format = self::getFormatDefinition($format);
061          $this->indicatorChangeInterval = $indicatorChangeInterval;
062          $this->indicatorValues = $indicatorValues;
063          $this->startTime = time();
064      }
065   
066      /**
067       * Sets the current indicator message.
068       *
069       * @param string|null $message
070       */
071      public function setMessage($message)
072      {
073          $this->message = $message;
074   
075          $this->display();
076      }
077   
078      /**
079       * Gets the current indicator message.
080       *
081       * @return string|null
082       *
083       * @internal for PHP 5.3 compatibility
084       */
085      public function getMessage()
086      {
087          return $this->message;
088      }
089   
090      /**
091       * Gets the progress bar start time.
092       *
093       * @return int The progress bar start time
094       *
095       * @internal for PHP 5.3 compatibility
096       */
097      public function getStartTime()
098      {
099          return $this->startTime;
100      }
101   
102      /**
103       * Gets the current animated indicator character.
104       *
105       * @return string
106       *
107       * @internal for PHP 5.3 compatibility
108       */
109      public function getCurrentValue()
110      {
111          return $this->indicatorValues[$this->indicatorCurrent % count($this->indicatorValues)];
112      }
113   
114      /**
115       * Starts the indicator output.
116       *
117       * @param $message
118       */
119      public function start($message)
120      {
121          if ($this->started) {
122              throw new LogicException('Progress indicator already started.');
123          }
124   
125          $this->message = $message;
126          $this->started = true;
127          $this->startTime = time();
128          $this->indicatorUpdateTime = $this->getCurrentTimeInMilliseconds() + $this->indicatorChangeInterval;
129          $this->indicatorCurrent = 0;
130   
131          $this->display();
132      }
133   
134      /**
135       * Advances the indicator.
136       */
137      public function advance()
138      {
139          if (!$this->started) {
140              throw new LogicException('Progress indicator has not yet been started.');
141          }
142   
143          if (!$this->output->isDecorated()) {
144              return;
145          }
146   
147          $currentTime = $this->getCurrentTimeInMilliseconds();
148   
149          if ($currentTime < $this->indicatorUpdateTime) {
150              return;
151          }
152   
153          $this->indicatorUpdateTime = $currentTime + $this->indicatorChangeInterval;
154          ++$this->indicatorCurrent;
155   
156          $this->display();
157      }
158   
159      /**
160       * Finish the indicator with message.
161       *
162       * @param $message
163       */
164      public function finish($message)
165      {
166          if (!$this->started) {
167              throw new LogicException('Progress indicator has not yet been started.');
168          }
169   
170          $this->message = $message;
171          $this->display();
172          $this->output->writeln('');
173          $this->started = false;
174      }
175   
176      /**
177       * Gets the format for a given name.
178       *
179       * @param string $name The format name
180       *
181       * @return string|null A format string
182       */
183      public static function getFormatDefinition($name)
184      {
185          if (!self::$formats) {
186              self::$formats = self::initFormats();
187          }
188   
189          return isset(self::$formats[$name]) ? self::$formats[$name] : null;
190      }
191   
192      /**
193       * Sets a placeholder formatter for a given name.
194       *
195       * This method also allow you to override an existing placeholder.
196       *
197       * @param string   $name     The placeholder name (including the delimiter char like %)
198       * @param callable $callable A PHP callable
199       */
200      public static function setPlaceholderFormatterDefinition($name, $callable)
201      {
202          if (!self::$formatters) {
203              self::$formatters = self::initPlaceholderFormatters();
204          }
205   
206          self::$formatters[$name] = $callable;
207      }
208   
209      /**
210       * Gets the placeholder formatter for a given name.
211       *
212       * @param string $name The placeholder name (including the delimiter char like %)
213       *
214       * @return callable|null A PHP callable
215       */
216      public static function getPlaceholderFormatterDefinition($name)
217      {
218          if (!self::$formatters) {
219              self::$formatters = self::initPlaceholderFormatters();
220          }
221   
222          return isset(self::$formatters[$name]) ? self::$formatters[$name] : null;
223      }
224   
225      private function display()
226      {
227          if (OutputInterface::VERBOSITY_QUIET === $this->output->getVerbosity()) {
228              return;
229          }
230   
231          $self = $this;
232   
233          $this->overwrite(preg_replace_callback("{%([a-z\-_]+)(?:\:([^%]+))?%}i", function ($matches) use ($self) {
234              if ($formatter = $self::getPlaceholderFormatterDefinition($matches[1])) {
235                  return call_user_func($formatter, $self);
236              }
237   
238              return $matches[0];
239          }, $this->format));
240      }
241   
242      private function determineBestFormat()
243      {
244          switch ($this->output->getVerbosity()) {
245              // OutputInterface::VERBOSITY_QUIET: display is disabled anyway
246              case OutputInterface::VERBOSITY_VERBOSE:
247                  return $this->output->isDecorated() ? 'verbose' : 'verbose_no_ansi';
248              case OutputInterface::VERBOSITY_VERY_VERBOSE:
249              case OutputInterface::VERBOSITY_DEBUG:
250                  return $this->output->isDecorated() ? 'very_verbose' : 'very_verbose_no_ansi';
251              default:
252                  return $this->output->isDecorated() ? 'normal' : 'normal_no_ansi';
253          }
254      }
255   
256      /**
257       * Overwrites a previous message to the output.
258       *
259       * @param string $message The message
260       */
261      private function overwrite($message)
262      {
263          if ($this->output->isDecorated()) {
264              $this->output->write("\x0D\x1B[2K");
265              $this->output->write($message);
266          } else {
267              $this->output->writeln($message);
268          }
269      }
270   
271      private function getCurrentTimeInMilliseconds()
272      {
273          return round(microtime(true) * 1000);
274      }
275   
276      private static function initPlaceholderFormatters()
277      {
278          return array(
279              'indicator' => function (ProgressIndicator $indicator) {
280                  return $indicator->getCurrentValue();
281              },
282              'message' => function (ProgressIndicator $indicator) {
283                  return $indicator->getMessage();
284              },
285              'elapsed' => function (ProgressIndicator $indicator) {
286                  return Helper::formatTime(time() - $indicator->getStartTime());
287              },
288              'memory' => function () {
289                  return Helper::formatMemory(memory_get_usage(true));
290              },
291          );
292      }
293   
294      private static function initFormats()
295      {
296          return array(
297              'normal' => ' %indicator% %message%',
298              'normal_no_ansi' => ' %message%',
299   
300              'verbose' => ' %indicator% %message% (%elapsed:6s%)',
301              'verbose_no_ansi' => ' %message% (%elapsed:6s%)',
302   
303              'very_verbose' => ' %indicator% %message% (%elapsed:6s%, %memory:6s%)',
304              'very_verbose_no_ansi' => ' %message% (%elapsed:6s%, %memory:6s%)',
305          );
306      }
307  }
308