Keine Beschreibung

ProgressIndicator.php 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Console\Helper;
  11. use Symfony\Component\Console\Exception\InvalidArgumentException;
  12. use Symfony\Component\Console\Exception\LogicException;
  13. use Symfony\Component\Console\Output\OutputInterface;
  14. /**
  15. * @author Kevin Bond <kevinbond@gmail.com>
  16. */
  17. class ProgressIndicator
  18. {
  19. private $output;
  20. private $startTime;
  21. private $format;
  22. private $message;
  23. private $indicatorValues;
  24. private $indicatorCurrent;
  25. private $indicatorChangeInterval;
  26. private $indicatorUpdateTime;
  27. private $started = false;
  28. private static $formatters;
  29. private static $formats;
  30. /**
  31. * @param string|null $format Indicator format
  32. * @param int $indicatorChangeInterval Change interval in milliseconds
  33. * @param array|null $indicatorValues Animated indicator characters
  34. */
  35. public function __construct(OutputInterface $output, string $format = null, int $indicatorChangeInterval = 100, array $indicatorValues = null)
  36. {
  37. $this->output = $output;
  38. if (null === $format) {
  39. $format = $this->determineBestFormat();
  40. }
  41. if (null === $indicatorValues) {
  42. $indicatorValues = ['-', '\\', '|', '/'];
  43. }
  44. $indicatorValues = array_values($indicatorValues);
  45. if (2 > \count($indicatorValues)) {
  46. throw new InvalidArgumentException('Must have at least 2 indicator value characters.');
  47. }
  48. $this->format = self::getFormatDefinition($format);
  49. $this->indicatorChangeInterval = $indicatorChangeInterval;
  50. $this->indicatorValues = $indicatorValues;
  51. $this->startTime = time();
  52. }
  53. /**
  54. * Sets the current indicator message.
  55. *
  56. * @param string|null $message
  57. */
  58. public function setMessage($message)
  59. {
  60. $this->message = $message;
  61. $this->display();
  62. }
  63. /**
  64. * Starts the indicator output.
  65. *
  66. * @param $message
  67. */
  68. public function start($message)
  69. {
  70. if ($this->started) {
  71. throw new LogicException('Progress indicator already started.');
  72. }
  73. $this->message = $message;
  74. $this->started = true;
  75. $this->startTime = time();
  76. $this->indicatorUpdateTime = $this->getCurrentTimeInMilliseconds() + $this->indicatorChangeInterval;
  77. $this->indicatorCurrent = 0;
  78. $this->display();
  79. }
  80. /**
  81. * Advances the indicator.
  82. */
  83. public function advance()
  84. {
  85. if (!$this->started) {
  86. throw new LogicException('Progress indicator has not yet been started.');
  87. }
  88. if (!$this->output->isDecorated()) {
  89. return;
  90. }
  91. $currentTime = $this->getCurrentTimeInMilliseconds();
  92. if ($currentTime < $this->indicatorUpdateTime) {
  93. return;
  94. }
  95. $this->indicatorUpdateTime = $currentTime + $this->indicatorChangeInterval;
  96. ++$this->indicatorCurrent;
  97. $this->display();
  98. }
  99. /**
  100. * Finish the indicator with message.
  101. *
  102. * @param $message
  103. */
  104. public function finish($message)
  105. {
  106. if (!$this->started) {
  107. throw new LogicException('Progress indicator has not yet been started.');
  108. }
  109. $this->message = $message;
  110. $this->display();
  111. $this->output->writeln('');
  112. $this->started = false;
  113. }
  114. /**
  115. * Gets the format for a given name.
  116. *
  117. * @param string $name The format name
  118. *
  119. * @return string|null A format string
  120. */
  121. public static function getFormatDefinition($name)
  122. {
  123. if (!self::$formats) {
  124. self::$formats = self::initFormats();
  125. }
  126. return self::$formats[$name] ?? null;
  127. }
  128. /**
  129. * Sets a placeholder formatter for a given name.
  130. *
  131. * This method also allow you to override an existing placeholder.
  132. *
  133. * @param string $name The placeholder name (including the delimiter char like %)
  134. * @param callable $callable A PHP callable
  135. */
  136. public static function setPlaceholderFormatterDefinition($name, $callable)
  137. {
  138. if (!self::$formatters) {
  139. self::$formatters = self::initPlaceholderFormatters();
  140. }
  141. self::$formatters[$name] = $callable;
  142. }
  143. /**
  144. * Gets the placeholder formatter for a given name.
  145. *
  146. * @param string $name The placeholder name (including the delimiter char like %)
  147. *
  148. * @return callable|null A PHP callable
  149. */
  150. public static function getPlaceholderFormatterDefinition($name)
  151. {
  152. if (!self::$formatters) {
  153. self::$formatters = self::initPlaceholderFormatters();
  154. }
  155. return self::$formatters[$name] ?? null;
  156. }
  157. private function display()
  158. {
  159. if (OutputInterface::VERBOSITY_QUIET === $this->output->getVerbosity()) {
  160. return;
  161. }
  162. $this->overwrite(preg_replace_callback("{%([a-z\-_]+)(?:\:([^%]+))?%}i", function ($matches) {
  163. if ($formatter = self::getPlaceholderFormatterDefinition($matches[1])) {
  164. return $formatter($this);
  165. }
  166. return $matches[0];
  167. }, $this->format ?? ''));
  168. }
  169. private function determineBestFormat(): string
  170. {
  171. switch ($this->output->getVerbosity()) {
  172. // OutputInterface::VERBOSITY_QUIET: display is disabled anyway
  173. case OutputInterface::VERBOSITY_VERBOSE:
  174. return $this->output->isDecorated() ? 'verbose' : 'verbose_no_ansi';
  175. case OutputInterface::VERBOSITY_VERY_VERBOSE:
  176. case OutputInterface::VERBOSITY_DEBUG:
  177. return $this->output->isDecorated() ? 'very_verbose' : 'very_verbose_no_ansi';
  178. default:
  179. return $this->output->isDecorated() ? 'normal' : 'normal_no_ansi';
  180. }
  181. }
  182. /**
  183. * Overwrites a previous message to the output.
  184. */
  185. private function overwrite(string $message)
  186. {
  187. if ($this->output->isDecorated()) {
  188. $this->output->write("\x0D\x1B[2K");
  189. $this->output->write($message);
  190. } else {
  191. $this->output->writeln($message);
  192. }
  193. }
  194. private function getCurrentTimeInMilliseconds(): float
  195. {
  196. return round(microtime(true) * 1000);
  197. }
  198. private static function initPlaceholderFormatters(): array
  199. {
  200. return [
  201. 'indicator' => function (self $indicator) {
  202. return $indicator->indicatorValues[$indicator->indicatorCurrent % \count($indicator->indicatorValues)];
  203. },
  204. 'message' => function (self $indicator) {
  205. return $indicator->message;
  206. },
  207. 'elapsed' => function (self $indicator) {
  208. return Helper::formatTime(time() - $indicator->startTime);
  209. },
  210. 'memory' => function () {
  211. return Helper::formatMemory(memory_get_usage(true));
  212. },
  213. ];
  214. }
  215. private static function initFormats(): array
  216. {
  217. return [
  218. 'normal' => ' %indicator% %message%',
  219. 'normal_no_ansi' => ' %message%',
  220. 'verbose' => ' %indicator% %message% (%elapsed:6s%)',
  221. 'verbose_no_ansi' => ' %message% (%elapsed:6s%)',
  222. 'very_verbose' => ' %indicator% %message% (%elapsed:6s%, %memory:6s%)',
  223. 'very_verbose_no_ansi' => ' %message% (%elapsed:6s%, %memory:6s%)',
  224. ];
  225. }
  226. }