1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\MediaLibrary\Commands; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Illuminate\Console\Command; |
7
|
|
|
use Illuminate\Console\ConfirmableTrait; |
8
|
|
|
use Illuminate\Support\Collection; |
9
|
|
|
use Spatie\{ |
10
|
|
|
MediaLibrary\FileManipulator, |
11
|
|
|
MediaLibrary\Media, |
12
|
|
|
MediaLibrary\MediaRepository |
13
|
|
|
}; |
14
|
|
|
|
15
|
|
|
class RegenerateCommand extends Command |
16
|
|
|
{ |
17
|
|
|
use ConfirmableTrait; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The console command name. |
21
|
|
|
* |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $signature = 'medialibrary:regenerate {modelType?} {--ids=*} |
25
|
|
|
{-- force : Force the operation to run when in production}'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The console command description. |
29
|
|
|
* |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
protected $description = 'Regenerate the derived images of media'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var \Spatie\MediaLibrary\MediaRepository |
36
|
|
|
*/ |
37
|
|
|
protected $mediaRepository; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var \Spatie\MediaLibrary\FileManipulator |
41
|
|
|
*/ |
42
|
|
|
protected $fileManipulator; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var array |
46
|
|
|
*/ |
47
|
|
|
protected $erroredMediaIds = []; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* RegenerateCommand constructor. |
51
|
|
|
* |
52
|
|
|
* @param MediaRepository $mediaRepository |
53
|
|
|
* @param FileManipulator $fileManipulator |
54
|
|
|
*/ |
55
|
|
|
public function __construct(MediaRepository $mediaRepository, FileManipulator $fileManipulator) |
56
|
|
|
{ |
57
|
|
|
parent::__construct(); |
58
|
|
|
|
59
|
|
|
$this->mediaRepository = $mediaRepository; |
60
|
|
|
$this->fileManipulator = $fileManipulator; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Handle regeneration. |
65
|
|
|
*/ |
66
|
|
|
public function handle() |
67
|
|
|
{ |
68
|
|
|
if (!$this->confirmToProceed()) { |
69
|
|
|
return; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$this->getMediaToBeRegenerated()->each(function (Media $media) { |
73
|
|
|
try { |
74
|
|
|
$this->fileManipulator->createDerivedFiles($media); |
75
|
|
|
$this->info("Media {$media->id} regenerated"); |
76
|
|
|
} catch (Exception $exception) { |
77
|
|
|
$this->error("Media {$media->id} could not be regenerated because `{$exception->getMessage()}`"); |
78
|
|
|
$this->erroredMediaIds[] = $media->id; |
79
|
|
|
} |
80
|
|
|
}); |
81
|
|
|
|
82
|
|
|
if (count($this->erroredMediaIds)) { |
83
|
|
|
$this->warn('The derived files of these media ids could not be regenerated: ' . implode(',', $this->erroredMediaIds)); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$this->info('All done!'); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function getMediaToBeRegenerated(): Collection |
90
|
|
|
{ |
91
|
|
|
$modelType = $this->argument('modelType') ?? ''; |
92
|
|
|
$mediaIds = $this->option('ids'); |
93
|
|
|
|
94
|
|
|
if ($modelType === '' && !$mediaIds) { |
95
|
|
|
return $this->mediaRepository->all(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
if ($mediaIds) { |
99
|
|
|
|
100
|
|
|
if (!is_array($mediaIds)) { |
101
|
|
|
$mediaIds = explode(',', $mediaIds); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $this->mediaRepository->getByIds($mediaIds); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return $this->mediaRepository->getByModelType($modelType); |
|
|
|
|
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.