null); if (isset($args[0])) { $args['callback'] = $args[0]; } if (!$args['callback'] || !is_callable($args['callback'])) { throw new RuntimeException('Callback option must be a callable.'); } $this->init($args); $callback = $args['callback']; while ($this->_progress < $this->_total) { $callback($this); $this->draw(); } $this->_consoleOutput->write(''); } /** * Initialize the progress bar for use. * * - `total` The total number of items in the progress bar. Defaults * to 100. * - `width` The width of the progress bar. Defaults to 80. * * @param array $args The initialization data. * @return void */ public function init(array $args = array()) { $args += array('total' => 100, 'width' => 80); $this->_progress = 0; $this->_width = $args['width']; $this->_total = $args['total']; } /** * Increment the progress bar. * * @param int $num The amount of progress to advance by. * @return void */ public function increment($num = 1) { $this->_progress = min(max(0, $this->_progress + $num), $this->_total); } /** * Render the progress bar based on the current state. * * @return void */ public function draw() { $numberLen = strlen(' 100%'); $complete = round($this->_progress / $this->_total, 2); $barLen = ($this->_width - $numberLen) * ($this->_progress / $this->_total); $bar = ''; if ($barLen > 1) { $bar = str_repeat('=', $barLen - 1) . '>'; } $pad = ceil($this->_width - $numberLen - $barLen); if ($pad > 0) { $bar .= str_repeat(' ', $pad); } $percent = ($complete * 100) . '%'; $bar .= str_pad($percent, $numberLen, ' ', STR_PAD_LEFT); $this->_consoleOutput->overwrite($bar, 0); } }