|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +/** |
| 5 | + * CakePHP(tm) : Rapid Development Framework (https://cakephp.org) |
| 6 | + * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org) |
| 7 | + * |
| 8 | + * Licensed under The MIT License |
| 9 | + * For full copyright and license information, please see the LICENSE.txt |
| 10 | + * Redistributions of files must retain the above copyright notice. |
| 11 | + * |
| 12 | + * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org) |
| 13 | + * @link https://cakephp.org CakePHP Project |
| 14 | + * @since 3.1.0 |
| 15 | + * @license https://opensource.org/licenses/mit-license.php MIT License |
| 16 | + */ |
| 17 | +namespace Cake\Upgrade\Command\Helper; |
| 18 | + |
| 19 | +use Cake\Console\Helper; |
| 20 | +use InvalidArgumentException; |
| 21 | + |
| 22 | +/** |
| 23 | + * Create a progress bar using a supplied callback. |
| 24 | + * |
| 25 | + * ## Usage |
| 26 | + * |
| 27 | + * The ProgressHelper can be accessed from shells using the helper() method |
| 28 | + * |
| 29 | + * ``` |
| 30 | + * $this->helper('Progress')->output(['callback' => function ($progress) { |
| 31 | + * // Do work |
| 32 | + * $progress->increment(); |
| 33 | + * }); |
| 34 | + * ``` |
| 35 | + */ |
| 36 | +class ProgressHelper extends Helper |
| 37 | +{ |
| 38 | + /** |
| 39 | + * Default value for progress bar total value. |
| 40 | + * Percent completion is derived from progress/total |
| 41 | + */ |
| 42 | + protected const DEFAULT_TOTAL = 100; |
| 43 | + |
| 44 | + /** |
| 45 | + * Default value for progress bar width |
| 46 | + */ |
| 47 | + protected const DEFAULT_WIDTH = 80; |
| 48 | + |
| 49 | + /** |
| 50 | + * The current progress. |
| 51 | + * |
| 52 | + * @var float|int |
| 53 | + */ |
| 54 | + protected float|int $_progress = 0; |
| 55 | + |
| 56 | + /** |
| 57 | + * The total number of 'items' to progress through. |
| 58 | + * |
| 59 | + * @var int |
| 60 | + */ |
| 61 | + protected int $_total = self::DEFAULT_TOTAL; |
| 62 | + |
| 63 | + /** |
| 64 | + * The width of the bar. |
| 65 | + * |
| 66 | + * @var int |
| 67 | + */ |
| 68 | + protected int $_width = self::DEFAULT_WIDTH; |
| 69 | + |
| 70 | + /** |
| 71 | + * Output a progress bar. |
| 72 | + * |
| 73 | + * Takes a number of options to customize the behavior: |
| 74 | + * |
| 75 | + * - `total` The total number of items in the progress bar. Defaults |
| 76 | + * to 100. |
| 77 | + * - `width` The width of the progress bar. Defaults to 80. |
| 78 | + * - `callback` The callback that will be called in a loop to advance the progress bar. |
| 79 | + * |
| 80 | + * @param array $args The arguments/options to use when outputting the progress bar. |
| 81 | + * @return void |
| 82 | + */ |
| 83 | + public function output(array $args): void |
| 84 | + { |
| 85 | + $args += ['callback' => null]; |
| 86 | + if (isset($args[0])) { |
| 87 | + $args['callback'] = $args[0]; |
| 88 | + } |
| 89 | + if (!$args['callback'] || !is_callable($args['callback'])) { |
| 90 | + throw new InvalidArgumentException('Callback option must be a callable.'); |
| 91 | + } |
| 92 | + $this->init($args); |
| 93 | + |
| 94 | + $callback = $args['callback']; |
| 95 | + |
| 96 | + $this->_io->out('', 0); |
| 97 | + while ($this->_progress < $this->_total) { |
| 98 | + $callback($this); |
| 99 | + $this->draw(); |
| 100 | + } |
| 101 | + $this->_io->out(''); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Initialize the progress bar for use. |
| 106 | + * |
| 107 | + * - `total` The total number of items in the progress bar. Defaults |
| 108 | + * to 100. |
| 109 | + * - `width` The width of the progress bar. Defaults to 80. |
| 110 | + * |
| 111 | + * @param array<string, mixed> $args The initialization data. |
| 112 | + * @return $this |
| 113 | + */ |
| 114 | + public function init(array $args = []) |
| 115 | + { |
| 116 | + $args += ['total' => self::DEFAULT_TOTAL, 'width' => self::DEFAULT_WIDTH]; |
| 117 | + $this->_progress = 0; |
| 118 | + $this->_width = $args['width']; |
| 119 | + $this->_total = $args['total']; |
| 120 | + |
| 121 | + return $this; |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Increment the progress bar. |
| 126 | + * |
| 127 | + * @param float|int $num The amount of progress to advance by. |
| 128 | + * @return $this |
| 129 | + */ |
| 130 | + public function increment(float|int $num = 1) |
| 131 | + { |
| 132 | + $this->_progress = min(max(0, $this->_progress + $num), $this->_total); |
| 133 | + |
| 134 | + return $this; |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Render the progress bar based on the current state. |
| 139 | + * |
| 140 | + * @return $this |
| 141 | + */ |
| 142 | + public function draw() |
| 143 | + { |
| 144 | + $numberLen = strlen(' 100%'); |
| 145 | + $complete = round($this->_progress / $this->_total, 2); |
| 146 | + $barLen = ($this->_width - $numberLen) * $this->_progress / $this->_total; |
| 147 | + $bar = ''; |
| 148 | + if ($barLen > 1) { |
| 149 | + $bar = str_repeat('=', (int)$barLen - 1) . '>'; |
| 150 | + } |
| 151 | + |
| 152 | + $pad = ceil($this->_width - $numberLen - $barLen); |
| 153 | + if ($pad > 0) { |
| 154 | + $bar .= str_repeat(' ', (int)$pad); |
| 155 | + } |
| 156 | + $percent = ($complete * 100) . '%'; |
| 157 | + $bar .= str_pad($percent, $numberLen, ' ', STR_PAD_LEFT); |
| 158 | + |
| 159 | + $this->_io->overwrite($bar, 0); |
| 160 | + |
| 161 | + return $this; |
| 162 | + } |
| 163 | +} |
0 commit comments