These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Spatie\MediaLibrary\Commands; |
||
| 4 | |||
| 5 | use Exception; |
||
| 6 | use Illuminate\Console\Command; |
||
| 7 | use Illuminate\Support\Collection; |
||
| 8 | use Spatie\MediaLibrary\Models\Media; |
||
| 9 | use Illuminate\Console\ConfirmableTrait; |
||
| 10 | use Spatie\MediaLibrary\FileManipulator; |
||
| 11 | use Spatie\MediaLibrary\MediaRepository; |
||
| 12 | |||
| 13 | class RegenerateCommand extends Command |
||
| 14 | { |
||
| 15 | use ConfirmableTrait; |
||
| 16 | |||
| 17 | protected $signature = 'medialibrary:regenerate {modelType?} {--ids=*} |
||
| 18 | {--only=* : Regenerate specific conversions} |
||
| 19 | {--only-missing : Regenerate only missing conversions} |
||
| 20 | {--force : Force the operation to run when in production}'; |
||
| 21 | |||
| 22 | protected $description = 'Regenerate the derived images of media'; |
||
| 23 | |||
| 24 | /** @var \Spatie\MediaLibrary\MediaRepository */ |
||
| 25 | protected $mediaRepository; |
||
| 26 | |||
| 27 | /** @var \Spatie\MediaLibrary\FileManipulator */ |
||
| 28 | protected $fileManipulator; |
||
| 29 | |||
| 30 | /** @var array */ |
||
| 31 | protected $erroredMediaIds = []; |
||
| 32 | |||
| 33 | public function __construct(MediaRepository $mediaRepository, FileManipulator $fileManipulator) |
||
| 34 | { |
||
| 35 | parent::__construct(); |
||
| 36 | |||
| 37 | $this->mediaRepository = $mediaRepository; |
||
| 38 | $this->fileManipulator = $fileManipulator; |
||
| 39 | } |
||
| 40 | |||
| 41 | public function handle() |
||
| 42 | { |
||
| 43 | if (! $this->confirmToProceed()) { |
||
| 44 | return; |
||
| 45 | } |
||
| 46 | |||
| 47 | $mediaFiles = $this->getMediaToBeRegenerated(); |
||
| 48 | |||
| 49 | $progressBar = $this->output->createProgressBar($mediaFiles->count()); |
||
| 50 | |||
| 51 | $this->errorMessages = []; |
||
| 52 | |||
| 53 | $mediaFiles->each(function (Media $media) use ($progressBar) { |
||
| 54 | try { |
||
| 55 | $this->fileManipulator->createDerivedFiles( |
||
| 56 | $media, |
||
| 57 | array_wrap($this->option('only')), |
||
|
0 ignored issues
–
show
|
|||
| 58 | $this->option('only-missing') |
||
| 59 | ); |
||
| 60 | } catch (Exception $exception) { |
||
| 61 | $this->errorMessages[$media->id] = $exception->getMessage(); |
||
| 62 | } |
||
| 63 | |||
| 64 | $progressBar->advance(); |
||
| 65 | }); |
||
| 66 | |||
| 67 | $progressBar->finish(); |
||
| 68 | |||
| 69 | if (count($this->errorMessages)) { |
||
| 70 | $this->warn('All done, but with some error messages:'); |
||
| 71 | |||
| 72 | foreach ($this->errorMessages as $mediaId => $message) { |
||
| 73 | $this->warn("Media id {$mediaId}: `{$message}`"); |
||
| 74 | } |
||
| 75 | } |
||
| 76 | |||
| 77 | $this->info('All done!'); |
||
| 78 | } |
||
| 79 | |||
| 80 | public function getMediaToBeRegenerated(): Collection |
||
| 81 | { |
||
| 82 | $modelType = $this->argument('modelType') ?? ''; |
||
| 83 | $mediaIds = $this->getMediaIds(); |
||
| 84 | |||
| 85 | if ($modelType === '' && count($mediaIds) === 0) { |
||
| 86 | return $this->mediaRepository->all(); |
||
| 87 | } |
||
| 88 | |||
| 89 | if (! count($mediaIds)) { |
||
| 90 | return $this->mediaRepository->getByModelType($modelType); |
||
| 91 | } |
||
| 92 | |||
| 93 | return $this->mediaRepository->getByIds($mediaIds); |
||
| 94 | } |
||
| 95 | |||
| 96 | protected function getMediaIds(): array |
||
| 97 | { |
||
| 98 | $mediaIds = $this->option('ids'); |
||
| 99 | |||
| 100 | if (! is_array($mediaIds)) { |
||
| 101 | $mediaIds = explode(',', $mediaIds); |
||
| 102 | } |
||
| 103 | |||
| 104 | if (count($mediaIds) === 1 && str_contains($mediaIds[0], ',')) { |
||
|
0 ignored issues
–
show
The function
str_contains() has been deprecated with message: Str::contains() should be used directly instead. Will be removed in Laravel 5.9.
This function has been deprecated. The supplier of the file has supplied an explanatory message. The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead. Loading history...
|
|||
| 105 | $mediaIds = explode(',', $mediaIds[0]); |
||
| 106 | } |
||
| 107 | |||
| 108 | return $mediaIds; |
||
| 109 | } |
||
| 110 | } |
||
| 111 |
This function has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.