|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Command; |
| 4 | + |
| 5 | +use PHPStan\Analyser\ResultCache\ResultCacheManager; |
| 6 | +use Symfony\Component\Console\Command\Command; |
| 7 | +use Symfony\Component\Console\Input\InputInterface; |
| 8 | +use Symfony\Component\Console\Input\InputOption; |
| 9 | +use Symfony\Component\Console\Output\OutputInterface; |
| 10 | + |
| 11 | +class ClearResultCacheCommand extends Command |
| 12 | +{ |
| 13 | + |
| 14 | + private const NAME = 'clear-result-cache'; |
| 15 | + |
| 16 | + /** @var string[] */ |
| 17 | + private $composerAutoloaderProjectPaths; |
| 18 | + |
| 19 | + /** |
| 20 | + * @param string[] $composerAutoloaderProjectPaths |
| 21 | + */ |
| 22 | + public function __construct( |
| 23 | + array $composerAutoloaderProjectPaths |
| 24 | + ) |
| 25 | + { |
| 26 | + parent::__construct(); |
| 27 | + $this->composerAutoloaderProjectPaths = $composerAutoloaderProjectPaths; |
| 28 | + } |
| 29 | + |
| 30 | + protected function configure(): void |
| 31 | + { |
| 32 | + $this->setName(self::NAME) |
| 33 | + ->setDescription('Clears the result cache.') |
| 34 | + ->setDefinition([ |
| 35 | + new InputOption('configuration', 'c', InputOption::VALUE_REQUIRED, 'Path to project configuration file'), |
| 36 | + new InputOption('autoload-file', 'a', InputOption::VALUE_REQUIRED, 'Project\'s additional autoload file path'), |
| 37 | + ]); |
| 38 | + } |
| 39 | + |
| 40 | + protected function execute(InputInterface $input, OutputInterface $output): int |
| 41 | + { |
| 42 | + $autoloadFile = $input->getOption('autoload-file'); |
| 43 | + $configuration = $input->getOption('configuration'); |
| 44 | + |
| 45 | + if ( |
| 46 | + (!is_string($autoloadFile) && $autoloadFile !== null) |
| 47 | + || (!is_string($configuration) && $configuration !== null) |
| 48 | + ) { |
| 49 | + throw new \PHPStan\ShouldNotHappenException(); |
| 50 | + } |
| 51 | + |
| 52 | + try { |
| 53 | + $inceptionResult = CommandHelper::begin( |
| 54 | + $input, |
| 55 | + $output, |
| 56 | + ['.'], |
| 57 | + null, |
| 58 | + null, |
| 59 | + $autoloadFile, |
| 60 | + $this->composerAutoloaderProjectPaths, |
| 61 | + $configuration, |
| 62 | + '0', |
| 63 | + false, |
| 64 | + false |
| 65 | + ); |
| 66 | + } catch (\PHPStan\Command\InceptionNotSuccessfulException $e) { |
| 67 | + return 1; |
| 68 | + } |
| 69 | + |
| 70 | + $container = $inceptionResult->getContainer(); |
| 71 | + |
| 72 | + /** @var ResultCacheManager $resultCacheManager */ |
| 73 | + $resultCacheManager = $container->getByType(ResultCacheManager::class); |
| 74 | + $path = $resultCacheManager->clear(); |
| 75 | + |
| 76 | + $output->writeln('<info>Result cache cleared from directory:</info>'); |
| 77 | + $output->writeln($path); |
| 78 | + |
| 79 | + return 0; |
| 80 | + } |
| 81 | + |
| 82 | +} |
0 commit comments