From 2b79b5889d00348d401d333569498588181ac21a Mon Sep 17 00:00:00 2001 From: patrickkusebauch Date: Sat, 18 Jun 2022 20:59:31 +0200 Subject: [PATCH 1/2] Add option to clear cache. There is no clear place to put this into documentation, however. --- src/Console/Application.php | 9 +++++++++ .../ServiceContainerBuilder.php | 17 +++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/src/Console/Application.php b/src/Console/Application.php index 320dcceca..df9318789 100644 --- a/src/Console/Application.php +++ b/src/Console/Application.php @@ -38,6 +38,12 @@ protected function getDefaultInputDefinition(): InputDefinition InputOption::VALUE_NONE, 'Disable caching mechanisms (wins over --cache-file)' ), + new InputOption( + '--clear-cache', + null, + InputOption::VALUE_NONE, + 'Clears cache file before run' + ), new InputOption( '--cache-file', null, @@ -86,6 +92,9 @@ public function doRun(InputInterface $input, OutputInterface $output): int : $currentWorkingDirectory.DIRECTORY_SEPARATOR.'.deptrac.cache'; $factory = new ServiceContainerBuilder($currentWorkingDirectory); + if (false === $input->hasParameterOption('--clear-cache', true)) { + $factory = $factory->clearCache($cache); + } if (!in_array($input->getArgument('command'), ['init', 'list', 'help', 'completion'], true)) { $factory = $factory->withConfig($config); } diff --git a/src/DependencyInjection/ServiceContainerBuilder.php b/src/DependencyInjection/ServiceContainerBuilder.php index fe309afa4..0dd4b90b9 100644 --- a/src/DependencyInjection/ServiceContainerBuilder.php +++ b/src/DependencyInjection/ServiceContainerBuilder.php @@ -64,6 +64,23 @@ public function withCache(?string $cacheFile): self return $builder; } + public function clearCache(?string $cacheFile): self + { + if (null === $cacheFile) { + return $this; + } + + $builder = clone $this; + + if (Path::isRelative($cacheFile)) { + $cacheFile = Path::makeAbsolute($cacheFile, $this->workingDirectory); + } + + unlink($cacheFile); + + return $builder; + } + public function build(): ContainerBuilder { $container = new ContainerBuilder(); From f31b6d4189c1fb968f9ea8d781614968836189c0 Mon Sep 17 00:00:00 2001 From: patrickkusebauch Date: Mon, 20 Jun 2022 21:08:13 +0200 Subject: [PATCH 2/2] correct condition --- src/Console/Application.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Console/Application.php b/src/Console/Application.php index df9318789..65fdbd569 100644 --- a/src/Console/Application.php +++ b/src/Console/Application.php @@ -92,7 +92,7 @@ public function doRun(InputInterface $input, OutputInterface $output): int : $currentWorkingDirectory.DIRECTORY_SEPARATOR.'.deptrac.cache'; $factory = new ServiceContainerBuilder($currentWorkingDirectory); - if (false === $input->hasParameterOption('--clear-cache', true)) { + if ($input->hasParameterOption('--clear-cache', true)) { $factory = $factory->clearCache($cache); } if (!in_array($input->getArgument('command'), ['init', 'list', 'help', 'completion'], true)) {