1 | <?php |
||
2 | |||
3 | namespace Backend\Modules\MediaLibrary\Console; |
||
4 | |||
5 | use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
||
6 | use Symfony\Component\Console\Input\InputInterface; |
||
7 | use Symfony\Component\Console\Input\InputOption; |
||
8 | use Symfony\Component\Console\Output\OutputInterface; |
||
9 | use Symfony\Component\Finder\Finder; |
||
10 | |||
11 | /** |
||
12 | * Clear media library cache |
||
13 | * Example: "bin/console media_library:cache:clear", will only clear all frontend MediaLibrary cached-thumbnails |
||
14 | * Example: "bin/console media_library:cache:clear --all", will clear all MediaLibrary cached-thumbnails |
||
15 | */ |
||
16 | class CacheClearCommand extends ContainerAwareCommand |
||
0 ignored issues
–
show
Deprecated Code
introduced
by
Loading history...
|
|||
17 | { |
||
18 | /** |
||
19 | * Should we clear all |
||
20 | * |
||
21 | * @var bool |
||
22 | */ |
||
23 | protected $clearAll = false; |
||
24 | |||
25 | protected function configure(): void |
||
26 | { |
||
27 | $this |
||
28 | ->setName('media_library:cache:clear') |
||
29 | ->setDescription('Clear all cached-thumbnails.') |
||
30 | ->addOption( |
||
31 | 'all', |
||
32 | null, |
||
33 | InputOption::VALUE_NONE, |
||
34 | 'If set, the backend cached thumbnails will be cleared as well.' |
||
35 | ); |
||
36 | } |
||
37 | |||
38 | protected function execute(InputInterface $input, OutputInterface $output): void |
||
39 | { |
||
40 | $this->checkOptions($input); |
||
41 | $this->deleteCachedFolders(); |
||
42 | $output->writeln('<info>' . $this->getMessage() . '</info>'); |
||
43 | } |
||
44 | |||
45 | private function checkOptions(InputInterface $input): void |
||
46 | { |
||
47 | if ($input->getOption('all')) { |
||
48 | $this->clearAll = true; |
||
49 | } |
||
50 | } |
||
51 | |||
52 | private function deleteCachedFolders(): void |
||
53 | { |
||
54 | $foldersToDelete = $this->getFoldersToDelete(); |
||
55 | foreach ($foldersToDelete as $folderPath) { |
||
56 | $this->getContainer()->get('media_library.manager.file')->deleteFolder($folderPath); |
||
57 | } |
||
58 | } |
||
59 | |||
60 | public function getMessage(): string |
||
61 | { |
||
62 | if ($this->clearAll) { |
||
63 | return '[OK] Front- and backend cache cleared for "MediaLibrary".'; |
||
64 | } |
||
65 | |||
66 | return '[OK] Frontend cache cleared for "MediaLibrary".'; |
||
67 | } |
||
68 | |||
69 | public function getFoldersToDelete(): array |
||
70 | { |
||
71 | $finder = new Finder(); |
||
72 | $results = $finder->directories()->in(FRONTEND_FILES_PATH . '/Cache') |
||
73 | ->name('media_library_*'); |
||
74 | |||
75 | if (!$this->clearAll) { |
||
76 | $results->exclude('media_library_backend_thumbnail'); |
||
77 | } |
||
78 | |||
79 | return array_map( |
||
80 | function ($folder) { |
||
81 | return $folder->getPathname(); |
||
82 | }, |
||
83 | iterator_to_array($results) |
||
84 | ); |
||
85 | } |
||
86 | } |
||
87 |