Verzeichnisstruktur phpBB-3.3.15
- Veröffentlicht
- 28.08.2024
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. |
|
(Beispiel Datei-Icons)
|
Auf das Icon klicken um den Quellcode anzuzeigen |
ProgressIndicator.php
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 string|null $format Indicator format
038 * @param int $indicatorChangeInterval Change interval in milliseconds
039 * @param array|null $indicatorValues Animated indicator characters
040 */
041 public function __construct(OutputInterface $output, $format = null, $indicatorChangeInterval = 100, $indicatorValues = null)
042 {
043 $this->output = $output;
044
045 if (null === $format) {
046 $format = $this->determineBestFormat();
047 }
048
049 if (null === $indicatorValues) {
050 $indicatorValues = ['-', '\\', '|', '/'];
051 }
052
053 $indicatorValues = array_values($indicatorValues);
054
055 if (2 > \count($indicatorValues)) {
056 throw new InvalidArgumentException('Must have at least 2 indicator value characters.');
057 }
058
059 $this->format = self::getFormatDefinition($format);
060 $this->indicatorChangeInterval = $indicatorChangeInterval;
061 $this->indicatorValues = $indicatorValues;
062 $this->startTime = time();
063 }
064
065 /**
066 * Sets the current indicator message.
067 *
068 * @param string|null $message
069 */
070 public function setMessage($message)
071 {
072 $this->message = $message;
073
074 $this->display();
075 }
076
077 /**
078 * Starts the indicator output.
079 *
080 * @param $message
081 */
082 public function start($message)
083 {
084 if ($this->started) {
085 throw new LogicException('Progress indicator already started.');
086 }
087
088 $this->message = $message;
089 $this->started = true;
090 $this->startTime = time();
091 $this->indicatorUpdateTime = $this->getCurrentTimeInMilliseconds() + $this->indicatorChangeInterval;
092 $this->indicatorCurrent = 0;
093
094 $this->display();
095 }
096
097 /**
098 * Advances the indicator.
099 */
100 public function advance()
101 {
102 if (!$this->started) {
103 throw new LogicException('Progress indicator has not yet been started.');
104 }
105
106 if (!$this->output->isDecorated()) {
107 return;
108 }
109
110 $currentTime = $this->getCurrentTimeInMilliseconds();
111
112 if ($currentTime < $this->indicatorUpdateTime) {
113 return;
114 }
115
116 $this->indicatorUpdateTime = $currentTime + $this->indicatorChangeInterval;
117 ++$this->indicatorCurrent;
118
119 $this->display();
120 }
121
122 /**
123 * Finish the indicator with message.
124 *
125 * @param $message
126 */
127 public function finish($message)
128 {
129 if (!$this->started) {
130 throw new LogicException('Progress indicator has not yet been started.');
131 }
132
133 $this->message = $message;
134 $this->display();
135 $this->output->writeln('');
136 $this->started = false;
137 }
138
139 /**
140 * Gets the format for a given name.
141 *
142 * @param string $name The format name
143 *
144 * @return string|null A format string
145 */
146 public static function getFormatDefinition($name)
147 {
148 if (!self::$formats) {
149 self::$formats = self::initFormats();
150 }
151
152 return isset(self::$formats[$name]) ? self::$formats[$name] : null;
153 }
154
155 /**
156 * Sets a placeholder formatter for a given name.
157 *
158 * This method also allow you to override an existing placeholder.
159 *
160 * @param string $name The placeholder name (including the delimiter char like %)
161 * @param callable $callable A PHP callable
162 */
163 public static function setPlaceholderFormatterDefinition($name, $callable)
164 {
165 if (!self::$formatters) {
166 self::$formatters = self::initPlaceholderFormatters();
167 }
168
169 self::$formatters[$name] = $callable;
170 }
171
172 /**
173 * Gets the placeholder formatter for a given name.
174 *
175 * @param string $name The placeholder name (including the delimiter char like %)
176 *
177 * @return callable|null A PHP callable
178 */
179 public static function getPlaceholderFormatterDefinition($name)
180 {
181 if (!self::$formatters) {
182 self::$formatters = self::initPlaceholderFormatters();
183 }
184
185 return isset(self::$formatters[$name]) ? self::$formatters[$name] : null;
186 }
187
188 private function display()
189 {
190 if (OutputInterface::VERBOSITY_QUIET === $this->output->getVerbosity()) {
191 return;
192 }
193
194 $self = $this;
195
196 $this->overwrite(preg_replace_callback("{%([a-z\-_]+)(?:\:([^%]+))?%}i", function ($matches) use ($self) {
197 if ($formatter = $self::getPlaceholderFormatterDefinition($matches[1])) {
198 return \call_user_func($formatter, $self);
199 }
200
201 return $matches[0];
202 }, $this->format));
203 }
204
205 private function determineBestFormat()
206 {
207 switch ($this->output->getVerbosity()) {
208 // OutputInterface::VERBOSITY_QUIET: display is disabled anyway
209 case OutputInterface::VERBOSITY_VERBOSE:
210 return $this->output->isDecorated() ? 'verbose' : 'verbose_no_ansi';
211 case OutputInterface::VERBOSITY_VERY_VERBOSE:
212 case OutputInterface::VERBOSITY_DEBUG:
213 return $this->output->isDecorated() ? 'very_verbose' : 'very_verbose_no_ansi';
214 default:
215 return $this->output->isDecorated() ? 'normal' : 'normal_no_ansi';
216 }
217 }
218
219 /**
220 * Overwrites a previous message to the output.
221 *
222 * @param string $message The message
223 */
224 private function overwrite($message)
225 {
226 if ($this->output->isDecorated()) {
227 $this->output->write("\x0D\x1B[2K");
228 $this->output->write($message);
229 } else {
230 $this->output->writeln($message);
231 }
232 }
233
234 private function getCurrentTimeInMilliseconds()
235 {
236 return round(microtime(true) * 1000);
237 }
238
239 private static function initPlaceholderFormatters()
240 {
241 return [
242 'indicator' => function (self $indicator) {
243 return $indicator->indicatorValues[$indicator->indicatorCurrent % \count($indicator->indicatorValues)];
244 },
245 'message' => function (self $indicator) {
246 return $indicator->message;
247 },
248 'elapsed' => function (self $indicator) {
249 return Helper::formatTime(time() - $indicator->startTime);
250 },
251 'memory' => function () {
252 return Helper::formatMemory(memory_get_usage(true));
253 },
254 ];
255 }
256
257 private static function initFormats()
258 {
259 return [
260 'normal' => ' %indicator% %message%',
261 'normal_no_ansi' => ' %message%',
262
263 'verbose' => ' %indicator% %message% (%elapsed:6s%)',
264 'verbose_no_ansi' => ' %message% (%elapsed:6s%)',
265
266 'very_verbose' => ' %indicator% %message% (%elapsed:6s%, %memory:6s%)',
267 'very_verbose_no_ansi' => ' %message% (%elapsed:6s%, %memory:6s%)',
268 ];
269 }
270 }
271