This is not supported by the ProgressBar
helper.
The estimated time is calculated by the ProgressBar#getEstimated()
method, which is very simple:
public function getEstimated(): float
{
if (!$this->step) {
return 0;
}
return round((time() - $this->startTime) / $this->step * $this->max);
}
This only takes into account the amount of time since the progress bar has been started (which is set when one calls ProgressBar#start()
, or on the constructor otherwise), and the total amount of "steps" it's taken so far.
There is no distinction between "real" steps, and "fake" steps. Even if one were to modify ProgresBar#step
before calling start()
(e.g. on the constructor), the result would be the same, since the calculation for estimated time would work exactly the same way.
The class is marked final
, which I think it's unfortunate for a helper class, so you cannot simply extend it and add your logic (e.g. with an additional int $resumedAt
property that one could use when calculating the estimated remaining time).
If you really, really need this, I'd simply make a copy of the class in your project and add the necessary logic there.
As a simple proof of concept, I'd copied ProgressBar
into App\ProgressBar
, and added this:
private int $resumedSteps = 0;
public function resume(int $max = null, int $step = 0): void
{
$this->startTime = time();
$this->resumedStep = $step;
$this->step = $step;
if (null !== $max) {
$this->setMaxSteps($max);
}
$this->setProgress($step);
$this->display();
}
public function getEstimated(): float
{
if ($this->step === 0 || $this->step === $this->resumedStep) {
return 0;
}
return round((time() - $this->startTime) / ($this->step - $this->resumedStep) * $this->max);
}
public function getRemaining(): float
{
if ($this->step === 0 || $this->step === $this->resumedStep) {
return 0;
}
return round((time() - $this->startTime) / ($this->step - $this->resumedStep) * ($this->max - $this->step));
}
One would simply use this as:
$itemCount = 1_000_000;
$progressBar = new App\ProgressBar($output, $itemCount);
$progressBar->setFormat(
$progressBar->getFormatDefinition(ProgressBar::FORMAT_DEBUG)
);
$startItem > 0 ? $progressBar->resume($startItem) : $progressBar->start();