Installation:
composer require moox/progress
php artisan moox:install
moox:install command publishes config files (if any) and sets up default progress bar configurations in config/moox-progress.php.First Use Case:
Use the ProgressBar facade to display a progress bar in a Laravel command:
use Moox\Progress\Facades\ProgressBar;
// Inside a command's handle() method
$progress = ProgressBar::create('Processing items...', 100);
for ($i = 0; $i <= 100; $i++) {
$progress->advance();
// Simulate work
sleep(0.1);
}
Where to Look First:
Moox\Progress\Facades\ProgressBar (primary entry point).config/moox-progress.php (customize appearance, formats, and behavior).Basic Progress Bar:
$progress = ProgressBar::create('Task Name', $totalItems);
foreach ($items as $item) {
$progress->advance();
// Process $item
}
Custom Formats: Override the default format in the config or dynamically:
$progress = ProgressBar::create('Custom Format', 100)
->setFormat('|%current%/%max%| %bar% %percentage%');
Async Progress (e.g., Queues):
Use the ProgressBar::async() method to track progress in background jobs:
$progressId = ProgressBar::async('Queue Job', 100);
// Later, update progress from a job:
ProgressBar::update($progressId, $current);
Integration with Laravel Jobs:
Attach progress to a job and update it in handle():
class ProcessItemsJob implements ShouldQueue {
protected $progressId;
public function __construct() {
$this->progressId = ProgressBar::async('ProcessItemsJob', 100);
}
public function handle() {
foreach ($items as $item) {
// Process $item
ProgressBar::update($this->progressId, $current++);
}
}
}
Nested Progress Bars:
Use ProgressBar::createNested() for hierarchical progress:
$parent = ProgressBar::create('Parent Task', 3);
foreach ($tasks as $task) {
$child = ProgressBar::createNested($parent, 'Child Task', 100);
// Process child task
$child->finish();
}
$parent->finish();
ProgressBar in handle() for CLI feedback.ProgressBar in unit tests:
$progress = Mockery::mock('overload:Moox\Progress\Facades\ProgressBar');
$progress->shouldReceive('create')->andReturnSelf();
Async Progress Leaks:
ProgressBar::finish() or ProgressBar::delete() for async bars can clutter memory.try-catch-finally to ensure cleanup:
try {
$progressId = ProgressBar::async('Task', 100);
// Work...
} finally {
ProgressBar::delete($progressId);
}
Config Overrides:
->setConfig() dynamically:
$progress->setConfig(['format' => 'custom']);
Thread Safety:
ProgressBar::async()->lock() for concurrent updates (if supported in future versions).Performance Overhead:
advance() calls in tight loops may slow down processing.advance(10) instead of 10 individual calls).Missing Progress Bars:
moox:install command ran successfully (publishes config/blade views).ProgressBar facade is properly aliased in config/app.php.Stuck Progress Bars:
advance() is called with values ≤ $max.Custom Formats:
Extend the ProgressBar class to add new formats:
class CustomProgressBar extends \Moox\Progress\ProgressBar {
public function customFormat() {
return $this->setFormat('|%elapsed%/%estimated| %bar%');
}
}
Event Listeners:
Listen for progress events (e.g., progress.advanced):
ProgressBar::listen('progress.advanced', function ($progress) {
Log::info("Advanced to {$progress->current}");
});
Blade Directives:
Use the @progress directive in views to display async bars:
@progress('queue-job-123')
Testing Hooks:
Override the ProgressBar resolver in tests:
ProgressBar::swap(new MockProgressBar());
format: "%current%/%max% [%bar%] %percentage%").config/moox-progress.php:
'output' => [
'driver' => 'cli', // or 'blade', 'json'
'blade_view' => 'progress::bar',
],
How can I help you explore Laravel packages today?