diff --git a/src/Command/UpdateAllProjects.php b/src/Command/UpdateAllProjects.php index 41d7c46298552527d0a9898932ecbf11f077179e..4292e60ced4fe1bf412445b851ad54415918641b 100644 --- a/src/Command/UpdateAllProjects.php +++ b/src/Command/UpdateAllProjects.php @@ -20,15 +20,16 @@ use Symfony\Component\HttpKernel\KernelInterface; )] class UpdateAllProjects extends Command { - /** - * @var Filesystem $filesystem - */ - private $filesystem; + private Filesystem $filesystem; + + private KernelInterface $kernel; /** - * @var KernelInterface $kernel + * @var string[] $black_list */ - private $kernel; + private array $black_list = + ['css', 'create.zip', 'favicon.ico', 'la-page-des-malins.php', 'js', 'php', 'shared', '.', '..']; + public function __construct(Filesystem $filesystem, KernelInterface $kernel) { @@ -58,11 +59,6 @@ class UpdateAllProjects extends Command throw new \LogicException('This command accepts only an instance of "ConsoleOutputInterface".'); } - if (!$this->filesystem instanceof Filesystem) { - $output->writeln("filesystem is not defined"); - return Command::INVALID; - } - ProgressBar::setFormatDefinition( 'project_update_message', ' %current%/%max% [%bar%] %percent%% : Updating project :%project% %elapsed%' @@ -71,18 +67,15 @@ class UpdateAllProjects extends Command $legacy_root = $this->kernel->getProjectDir() . '/legacy'; $capsule_prototype_root = './capsule-prototype'; - $black_list = ['css', 'create.zip', 'favicon.ico', 'la-page-des-malins.php', 'js', 'php', 'shared', '.', '..']; $directory_files = scandir($legacy_root); if (false === $directory_files) { return Command::FAILURE; } - $all_file_in_directory = array_diff($directory_files, $black_list); + $all_file_in_directory = $this->getProjectsDirectories($directory_files); - $php_files = glob($capsule_prototype_root . '/php/*.php'); - // add +1 for index.html file - $php_files_count_per_projects = count($php_files ?: []) + 1; + $php_files_count_per_projects = $this->getPhpFilesCountPerProjects($capsule_prototype_root); $all_projects_progress_bar = new ProgressBar($output->section()); $all_projects_progress_bar->setFormat( @@ -91,8 +84,8 @@ class UpdateAllProjects extends Command $all_projects_progress_bar->SetMessage('Projects update progress'); $all_projects_progress_bar->start(); - foreach ($all_projects_progress_bar->iterate($all_file_in_directory) as $value) { - $current_legacy_path = Path::normalize($legacy_root . '/' . $value); + foreach ($all_projects_progress_bar->iterate($all_file_in_directory) as $project_directory) { + $current_legacy_path = Path::normalize($legacy_root . '/' . $project_directory); if (!$this->filesystem->exists($current_legacy_path)) { continue; @@ -102,33 +95,16 @@ class UpdateAllProjects extends Command continue; } - if (in_array($value, $black_list)) { + if (in_array($project_directory, $this->black_list)) { continue; } - $progress_bar = new ProgressBar($output->section(), $php_files_count_per_projects); - $progress_bar->setFormat('project_update_message'); - $progress_bar->setMessage($current_legacy_path, 'project'); - - $progress_bar->start(); - - // override index.html - $this->filesystem->copy( - Path::normalize($capsule_prototype_root . '/index.html'), - Path::normalize($current_legacy_path . '/index.html'), - true + $this->updateProjectDirectory( + $output, + $php_files_count_per_projects, + $current_legacy_path, + $capsule_prototype_root ); - $progress_bar->advance(); - - // override php files - $this->filesystem->mirror( - Path::normalize($capsule_prototype_root . '/php'), - Path::normalize($current_legacy_path . '/php'), - null, - ['override' => true, 'delete' => true ] - ); - $progress_bar->advance($php_files_count_per_projects - 1); - $progress_bar->finish(); $all_projects_progress_bar->advance($php_files_count_per_projects); } @@ -136,4 +112,62 @@ class UpdateAllProjects extends Command return Command::SUCCESS; } + + /** + * @param string[] $directory_files + * @return string[] projects directories + */ + private function getProjectsDirectories(array $directory_files): array + { + return array_diff($directory_files, $this->black_list); + } + + /** + * @param string $capsule_prototype_root + * @return int + */ + protected function getPhpFilesCountPerProjects(string $capsule_prototype_root): int + { + $php_files = glob($capsule_prototype_root . '/php/*.php'); + // add +1 for index.html file + return count($php_files ?: []) + 1; + } + + /** + * @param ConsoleOutputInterface $output + * @param int $php_files_count_per_projects + * @param string $current_legacy_path + * @param string $capsule_prototype_root + * @return void + */ + protected function updateProjectDirectory( + ConsoleOutputInterface $output, + int $php_files_count_per_projects, + string $current_legacy_path, + string $capsule_prototype_root + ): void { + $progress_bar = new ProgressBar($output->section(), $php_files_count_per_projects); + $progress_bar->setFormat('project_update_message'); + $progress_bar->setMessage($current_legacy_path, 'project'); + + $progress_bar->start(); + + // override index.html + $this->filesystem->copy( + Path::normalize($capsule_prototype_root . '/index.html'), + Path::normalize($current_legacy_path . '/index.html'), + true + ); + $progress_bar->advance(); + + // override php files + $this->filesystem->mirror( + Path::normalize($capsule_prototype_root . '/php'), + Path::normalize($current_legacy_path . '/php'), + null, + ['override' => true, 'delete' => true] + ); + $progress_bar->advance($php_files_count_per_projects - 1); + $progress_bar->finish(); + } }