Skip to content

Commit ca2dc6c

Browse files
Optimize by run single list --format=xml
The existing code is very slow with more than a couple of commands (especially so on a Docker for Mac osxfs host volume mount). The symfony/console library supports fetching a single combined xml of all commands, so using this reduces the number of console command runs to one, massively speeding it up.
1 parent 19ac917 commit ca2dc6c

File tree

1 file changed

+9
-22
lines changed

1 file changed

+9
-22
lines changed

src/DumpCommand.php

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -73,42 +73,29 @@ protected function execute(InputInterface $input, OutputInterface $output)
7373
$scriptOptions = $input->getOption('script-options');
7474

7575
// find all commands
76-
$process = new Process($script . ' ' . $scriptOptions . ' list --raw | awk \'{if (NF>1) print $1 " " substr($0, index($0,$2)); else print $1}\'');
76+
$process = new Process($script . ' ' . $scriptOptions . ' list --format=xml');
7777
$process->run();
7878
if (!$process->isSuccessful()) {
7979
throw new \RuntimeException($process->getErrorOutput());
8080
}
8181

82-
$rawCommands = explode("\n", $process->getOutput());
83-
array_pop($rawCommands);
82+
$xmlCommands = $process->getOutput();
83+
$xml = simplexml_load_string($xmlCommands);
8484

8585
$commands = array();
8686
$commandsDescriptions = array();
8787
$commandsOptionsDescriptions = array();
88-
foreach ($rawCommands as $rawCommand) {
89-
$rawCommand = explode(' ', $rawCommand, 2);
90-
$commands[] = $rawCommand[0];
91-
$commandsDescriptions[$rawCommand[0]] = !empty($rawCommand[1]) ? $rawCommand[1] : null;
92-
$commandsOptionsDescriptions[$rawCommand[0]] = array();
93-
}
94-
9588
// find all options
9689
$commandsOptions = array();
9790
$globalOptions = array();
9891
$optionsDescriptions = array();
99-
foreach ($commands as $command) {
100-
// get command help as xml
101-
$process = new Process($script . ' ' . $scriptOptions . ' help --format=xml ' . $command);
102-
$process->run();
103-
if (!$process->isSuccessful()) {
104-
throw new \RuntimeException($process->getErrorOutput());
105-
}
106-
$xmlHelp = $process->getOutput();
107-
108-
// extract options from xml help
92+
foreach ($xml->xpath('/symfony/commands/command') as $xmlCommand) {
93+
$command = (string) $xmlCommand['name'];
94+
$commands[] = $command;
95+
$commandsDescriptions[$command] = !empty($xmlCommand->description) ? $xmlCommand->description : null;
96+
$commandsOptionsDescriptions[$command] = array();
10997
$commandOptions = array();
110-
$xml = simplexml_load_string($xmlHelp);
111-
foreach ($xml->xpath('/command/options/option') as $commandOption) {
98+
foreach ($xmlCommand->xpath('options/option') as $commandOption) {
11299
$name = (string)$commandOption['name'];
113100
$commandOptions[] = $name;
114101
$optionsDescriptions[$name] = $commandsOptionsDescriptions[$command][$name] = (string)$commandOption->description;

0 commit comments

Comments
 (0)