4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Console\Helper;
14 use Symfony\Component\Console\Exception\InvalidArgumentException;
15 use Symfony\Component\Console\Exception\LogicException;
16 use Symfony\Component\Console\Output\OutputInterface;
19 * @author Kevin Bond <kevinbond@gmail.com>
21 class ProgressIndicator
27 private $indicatorValues;
28 private $indicatorCurrent;
29 private $indicatorChangeInterval;
30 private $indicatorUpdateTime;
31 private $started = false;
33 private static $formatters;
34 private static $formats;
37 * @param OutputInterface $output
38 * @param string|null $format Indicator format
39 * @param int $indicatorChangeInterval Change interval in milliseconds
40 * @param array|null $indicatorValues Animated indicator characters
42 public function __construct(OutputInterface $output, $format = null, $indicatorChangeInterval = 100, $indicatorValues = null)
44 $this->output = $output;
46 if (null === $format) {
47 $format = $this->determineBestFormat();
50 if (null === $indicatorValues) {
51 $indicatorValues = array('-', '\\', '|', '/');
54 $indicatorValues = array_values($indicatorValues);
56 if (2 > count($indicatorValues)) {
57 throw new InvalidArgumentException('Must have at least 2 indicator value characters.');
60 $this->format = self::getFormatDefinition($format);
61 $this->indicatorChangeInterval = $indicatorChangeInterval;
62 $this->indicatorValues = $indicatorValues;
63 $this->startTime = time();
67 * Sets the current indicator message.
69 * @param string|null $message
71 public function setMessage($message)
73 $this->message = $message;
79 * Gets the current indicator message.
83 * @internal for PHP 5.3 compatibility
85 public function getMessage()
87 return $this->message;
91 * Gets the progress bar start time.
93 * @return int The progress bar start time
95 * @internal for PHP 5.3 compatibility
97 public function getStartTime()
99 return $this->startTime;
103 * Gets the current animated indicator character.
107 * @internal for PHP 5.3 compatibility
109 public function getCurrentValue()
111 return $this->indicatorValues[$this->indicatorCurrent % count($this->indicatorValues)];
115 * Starts the indicator output.
119 public function start($message)
121 if ($this->started) {
122 throw new LogicException('Progress indicator already started.');
125 $this->message = $message;
126 $this->started = true;
127 $this->startTime = time();
128 $this->indicatorUpdateTime = $this->getCurrentTimeInMilliseconds() + $this->indicatorChangeInterval;
129 $this->indicatorCurrent = 0;
135 * Advances the indicator.
137 public function advance()
139 if (!$this->started) {
140 throw new LogicException('Progress indicator has not yet been started.');
143 if (!$this->output->isDecorated()) {
147 $currentTime = $this->getCurrentTimeInMilliseconds();
149 if ($currentTime < $this->indicatorUpdateTime) {
153 $this->indicatorUpdateTime = $currentTime + $this->indicatorChangeInterval;
154 ++$this->indicatorCurrent;
160 * Finish the indicator with message.
164 public function finish($message)
166 if (!$this->started) {
167 throw new LogicException('Progress indicator has not yet been started.');
170 $this->message = $message;
172 $this->output->writeln('');
173 $this->started = false;
177 * Gets the format for a given name.
179 * @param string $name The format name
181 * @return string|null A format string
183 public static function getFormatDefinition($name)
185 if (!self::$formats) {
186 self::$formats = self::initFormats();
189 return isset(self::$formats[$name]) ? self::$formats[$name] : null;
193 * Sets a placeholder formatter for a given name.
195 * This method also allow you to override an existing placeholder.
197 * @param string $name The placeholder name (including the delimiter char like %)
198 * @param callable $callable A PHP callable
200 public static function setPlaceholderFormatterDefinition($name, $callable)
202 if (!self::$formatters) {
203 self::$formatters = self::initPlaceholderFormatters();
206 self::$formatters[$name] = $callable;
210 * Gets the placeholder formatter for a given name.
212 * @param string $name The placeholder name (including the delimiter char like %)
214 * @return callable|null A PHP callable
216 public static function getPlaceholderFormatterDefinition($name)
218 if (!self::$formatters) {
219 self::$formatters = self::initPlaceholderFormatters();
222 return isset(self::$formatters[$name]) ? self::$formatters[$name] : null;
225 private function display()
227 if (OutputInterface::VERBOSITY_QUIET === $this->output->getVerbosity()) {
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);
242 private function determineBestFormat()
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';
252 return $this->output->isDecorated() ? 'normal' : 'normal_no_ansi';
257 * Overwrites a previous message to the output.
259 * @param string $message The message
261 private function overwrite($message)
263 if ($this->output->isDecorated()) {
264 $this->output->write("\x0D\x1B[2K");
265 $this->output->write($message);
267 $this->output->writeln($message);
271 private function getCurrentTimeInMilliseconds()
273 return round(microtime(true) * 1000);
276 private static function initPlaceholderFormatters()
279 'indicator' => function (ProgressIndicator $indicator) {
280 return $indicator->getCurrentValue();
282 'message' => function (ProgressIndicator $indicator) {
283 return $indicator->getMessage();
285 'elapsed' => function (ProgressIndicator $indicator) {
286 return Helper::formatTime(time() - $indicator->getStartTime());
288 'memory' => function () {
289 return Helper::formatMemory(memory_get_usage(true));
294 private static function initFormats()
297 'normal' => ' %indicator% %message%',
298 'normal_no_ansi' => ' %message%',
300 'verbose' => ' %indicator% %message% (%elapsed:6s%)',
301 'verbose_no_ansi' => ' %message% (%elapsed:6s%)',
303 'very_verbose' => ' %indicator% %message% (%elapsed:6s%, %memory:6s%)',
304 'very_verbose_no_ansi' => ' %message% (%elapsed:6s%, %memory:6s%)',