diff --git a/ansible/playbook.yml b/ansible/playbook.yml index ace6da1..c853343 100644 --- a/ansible/playbook.yml +++ b/ansible/playbook.yml @@ -224,6 +224,11 @@ become: false args: chdir: "{{ path }}" + - name: Exécuter app:images:warmup dans le répertoire de l application + ansible.builtin.command: php bin/console app:images:warmup + become: false + args: + chdir: "{{ path }}" when: ansible_os_family == "Debian" # Added a when condition here, often missed - name: Exécuter pwa:compile dans le répertoire de l application ansible.builtin.command: php -d memory_limit=-1 bin/console pwa:compile diff --git a/src/Command/AppWarmupImagesCommand.php b/src/Command/AppWarmupImagesCommand.php new file mode 100644 index 0000000..b7196e0 --- /dev/null +++ b/src/Command/AppWarmupImagesCommand.php @@ -0,0 +1,111 @@ +productRepository = $productRepository; + $this->optionsRepository = $optionsRepository; + $this->cacheManager = $cacheManager; + $this->dataManager = $dataManager; + $this->filterManager = $filterManager; + $this->uploaderHelper = $uploaderHelper; + } + + protected function execute(InputInterface $input, OutputInterface $output): int + { + $io = new SymfonyStyle($input, $output); + + $products = $this->productRepository->findAll(); + $options = $this->optionsRepository->findAll(); + + $io->title('Régénération FORCÉE du cache LiipImagine'); + $io->note('Le cache existant sera supprimé avant chaque génération.'); + + $io->section('Produits'); + $this->processCollection($products, 'imageFile', $io); + + $io->section('Options'); + $this->processCollection($options, 'imageFile', $io); + + $io->success('Toutes les images ont été régénérées avec succès.'); + + return Command::SUCCESS; + } + + private function processCollection(array $entities, string $fieldName, SymfonyStyle $io): void + { + $progressBar = $io->createProgressBar(count($entities) * count(self::FILTERS)); + $progressBar->start(); + + foreach ($entities as $entity) { + $assetPath = $this->uploaderHelper->asset($entity, $fieldName); + + if (!$assetPath) { + $progressBar->advance(count(self::FILTERS)); + continue; + } + + // Nettoyage du path (on retire le / initial) + $path = ltrim(parse_url($assetPath, PHP_URL_PATH), '/'); + + foreach (self::FILTERS as $filter) { + try { + // 1. On supprime le cache existant pour ce filtre spécifique + $this->cacheManager->remove($path, $filter); + + // 2. On récupère la source originale + $binary = $this->dataManager->find($filter, $path); + // 3. On applique les filtres et on stocke le nouveau résultat + $this->cacheManager->store( + $this->filterManager->applyFilter($binary, $filter), + $path, + $filter + ); + } catch (\Exception $e) { + dd($e); + // Erreur ignorée (souvent fichier source manquant sur le disque) + } + $progressBar->advance(); + } + } + + $progressBar->finish(); + $io->newLine(2); + } +}