-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathResizeImagesCommand.php
More file actions
66 lines (49 loc) · 1.89 KB
/
Copy pathResizeImagesCommand.php
File metadata and controls
66 lines (49 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
namespace JustBetter\ImageOptimize\Commands;
use Illuminate\Bus\Batch;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use JustBetter\ImageOptimize\Contracts\ResizesImages;
use JustBetter\ImageOptimize\Jobs\ResizeImagesJob;
/** @codeCoverageIgnore */
class ResizeImagesCommand extends Command
{
protected $signature = 'justbetter:optimize:images {--forceAll}';
protected $description = 'Optimize all images in the asset library';
public function handle(ResizesImages $resizesImages): int
{
/** @var bool $forceAll */
$forceAll = $this->option('forceAll');
try {
DB::connection()->getPdo();
} catch (\Exception $e) {
$this->error('You need an active database connection in order to use the optimize addon.');
return static::FAILURE;
}
if ($this->getOutput()->isVerbose()) {
$this->line('Starting the resize images job');
if ($forceAll) {
$this->comment('Forcing to optimize all images');
}
$batch = $resizesImages->resize($forceAll);
$progress = $this->output->createProgressBar($batch->totalJobs);
$progress->start();
while ($batch->pendingJobs && ! $batch->finished() && ! $batch->cancelled()) {
/** @var mixed $refreshed */
$refreshed = $batch->fresh();
if (! $refreshed instanceof Batch) {
break;
}
$batch = $refreshed;
$progress->setProgress($batch->processedJobs());
}
$progress->finish();
$this->output->newLine(2);
$this->info('All images have been resized');
} else {
ResizeImagesJob::dispatch($forceAll);
$this->info('Jobs dispatched');
}
return static::SUCCESS;
}
}