|
12 | 12 | namespace Symfony\Flex\Command;
|
13 | 13 |
|
14 | 14 | use Composer\Command\RequireCommand as BaseRequireCommand;
|
| 15 | +use Composer\Factory; |
| 16 | +use Composer\Json\JsonFile; |
| 17 | +use Composer\Json\JsonManipulator; |
15 | 18 | use Composer\Plugin\PluginInterface;
|
16 | 19 | use Symfony\Component\Console\Input\InputInterface;
|
17 | 20 | use Symfony\Component\Console\Input\InputOption;
|
@@ -51,15 +54,35 @@ protected function execute(InputInterface $input, OutputInterface $output)
|
51 | 54 | $input->setOption('no-suggest', true);
|
52 | 55 | }
|
53 | 56 |
|
54 |
| - $ret = parent::execute($input, $output) ?? 0; |
| 57 | + $file = Factory::getComposerFile(); |
| 58 | + $contents = file_get_contents($file); |
| 59 | + $json = JsonFile::parseJson($contents); |
55 | 60 |
|
56 |
| - if (0 !== $ret || $input->getOption('no-unpack') || $input->getOption('no-update')) { |
57 |
| - return $ret; |
| 61 | + if (\array_key_exists('require-dev', $json) && !$json['require-dev'] && (new JsonManipulator($contents))->removeMainKey('require-dev')) { |
| 62 | + $manipulator = new JsonManipulator($contents); |
| 63 | + $manipulator->addLink('require-dev', 'php', '*'); |
| 64 | + file_put_contents($file, $manipulator->getContents()); |
| 65 | + } else { |
| 66 | + $file = null; |
58 | 67 | }
|
| 68 | + unset($contents, $json, $manipulator); |
59 | 69 |
|
60 |
| - $unpackCommand = new UnpackCommand($this->resolver); |
61 |
| - $unpackCommand->setApplication($this->getApplication()); |
| 70 | + try { |
| 71 | + $ret = parent::execute($input, $output) ?? 0; |
62 | 72 |
|
63 |
| - return $unpackCommand->execute($input, $output); |
| 73 | + if (0 !== $ret || $input->getOption('no-unpack') || $input->getOption('no-update')) { |
| 74 | + return $ret; |
| 75 | + } |
| 76 | + |
| 77 | + $unpackCommand = new UnpackCommand($this->resolver); |
| 78 | + $unpackCommand->setApplication($this->getApplication()); |
| 79 | + |
| 80 | + return $unpackCommand->execute($input, $output); |
| 81 | + } finally { |
| 82 | + if (null !== $file) { |
| 83 | + $manipulator = new JsonManipulator(file_get_contents($file)); |
| 84 | + $manipulator->removeSubNode('require-dev', 'php'); |
| 85 | + } |
| 86 | + } |
64 | 87 | }
|
65 | 88 | }
|
0 commit comments