1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\MediaLibrary\Commands; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
6
|
|
|
use Spatie\{ |
7
|
|
|
MediaLibrary\FileManipulator, |
8
|
|
|
MediaLibrary\Media, |
9
|
|
|
MediaLibrary\MediaRepository |
10
|
|
|
}; |
11
|
|
|
|
12
|
|
|
class RegenerateCommand extends Command |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* The console command name. |
16
|
|
|
* |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
protected $signature = 'medialibrary:regenerate {modelType?} {--ids=*}'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The console command description. |
23
|
|
|
* |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $description = 'Regenerate the derived images of media'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var \Spatie\MediaLibrary\MediaRepository |
30
|
|
|
*/ |
31
|
|
|
protected $mediaRepository; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var \Spatie\MediaLibrary\FileManipulator |
35
|
|
|
*/ |
36
|
|
|
protected $fileManipulator; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* RegenerateCommand constructor. |
40
|
|
|
* |
41
|
|
|
* @param MediaRepository $mediaRepository |
42
|
|
|
* @param FileManipulator $fileManipulator |
43
|
|
|
*/ |
44
|
|
|
public function __construct(MediaRepository $mediaRepository, FileManipulator $fileManipulator) |
45
|
|
|
{ |
46
|
|
|
parent::__construct(); |
47
|
|
|
|
48
|
|
|
$this->mediaRepository = $mediaRepository; |
49
|
|
|
$this->fileManipulator = $fileManipulator; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Handle regeneration. |
54
|
|
|
*/ |
55
|
|
|
public function handle() |
56
|
|
|
{ |
57
|
|
|
$this->getMediaToBeRegenerated()->map(function (Media $media) { |
58
|
|
|
$this->fileManipulator->createDerivedFiles($media); |
59
|
|
|
$this->info(sprintf('Media %s regenerated', $media->id)); |
60
|
|
|
}); |
61
|
|
|
|
62
|
|
|
$this->info('All done!'); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return \Illuminate\Database\Eloquent\Collection |
67
|
|
|
*/ |
68
|
|
|
public function getMediaToBeRegenerated() |
69
|
|
|
{ |
70
|
|
|
$modelType = $this->argument('modelType') ?? ''; |
71
|
|
|
$mediaIds = $this->option('ids'); |
72
|
|
|
|
73
|
|
|
if ($modelType === '' && !$mediaIds) { |
74
|
|
|
return $this->mediaRepository->all(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
if ($mediaIds) { |
78
|
|
|
|
79
|
|
|
if (! is_array($mediaIds)) { |
80
|
|
|
$mediaIds = explode(',', $mediaIds); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $this->mediaRepository->getByIds($mediaIds); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $this->mediaRepository->getByModelType($modelType); |
|
|
|
|
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
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.