| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace cweagans\Composer\Command; |
| 6 |
|
| 7 |
use Composer\DependencyResolver\Operation\UninstallOperation; |
| 8 |
use Symfony\Component\Console\Input\ArrayInput; |
| 9 |
use Symfony\Component\Console\Input\InputInterface; |
| 10 |
use Symfony\Component\Console\Output\OutputInterface; |
| 11 |
|
| 12 |
class RepatchCommand extends PatchesCommandBase |
| 13 |
{ |
| 14 |
protected function configure(): void |
| 15 |
{ |
| 16 |
$this->setName('patches-repatch'); |
| 17 |
$this->setDescription('Delete, re-download, and re-patch each dependency with any patches defined.'); |
| 18 |
$this->setAliases(['prp']); |
| 19 |
} |
| 20 |
|
| 21 |
protected function execute(InputInterface $input, OutputInterface $output): int |
| 22 |
{ |
| 23 |
$plugin = $this->getPatchesPluginInstance(); |
| 24 |
if (is_null($plugin)) { |
| 25 |
return 1; |
| 26 |
} |
| 27 |
|
| 28 |
$plugin->loadLockedPatches(); |
| 29 |
$patchCollection = $plugin->getPatchCollection(); |
| 30 |
if (is_null($patchCollection)) { |
| 31 |
return 1; |
| 32 |
} |
| 33 |
|
| 34 |
$localRepository = $this->requireComposer() |
| 35 |
->getRepositoryManager() |
| 36 |
->getLocalRepository(); |
| 37 |
|
| 38 |
$patched_packages = $patchCollection->getPatchedPackages(); |
| 39 |
$packages = array_filter($localRepository->getPackages(), function ($val) use ($patched_packages) { |
| 40 |
return in_array($val->getName(), $patched_packages); |
| 41 |
}); |
| 42 |
|
| 43 |
// Remove patched packages so that we can re-install/re-patch. |
| 44 |
$promises = []; |
| 45 |
foreach ($packages as $package) { |
| 46 |
$uninstallOperation = new UninstallOperation($package); |
| 47 |
$promises[] = $this->requireComposer() |
| 48 |
->getInstallationManager() |
| 49 |
->uninstall($localRepository, $uninstallOperation); |
| 50 |
} |
| 51 |
// Wait for uninstalls to finish. |
| 52 |
$promises = array_filter($promises); |
| 53 |
if (!empty($promises)) { |
| 54 |
$this->requireComposer()->getLoop()->wait($promises); |
| 55 |
} |
| 56 |
|
| 57 |
$input = new ArrayInput(['command' => 'install']); |
| 58 |
$application = $this->getApplication(); |
| 59 |
$application->setAutoExit(false); |
| 60 |
$application->run($input, $output); |
| 61 |
|
| 62 |
return 0; |
| 63 |
} |
| 64 |
} |
| 65 |
|