1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace drupol\ComposerPackages\Commands; |
6
|
|
|
|
7
|
|
|
use Composer\Command\BaseCommand as Command; |
8
|
|
|
use ComposerPackages\Dependencies as PackageDependencies; |
9
|
|
|
use ComposerPackages\Packages; |
10
|
|
|
use ComposerPackages\Versions; |
11
|
|
|
use PBergman\Console\Helper\TreeHelper; |
12
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
13
|
|
|
use Symfony\Component\Console\Input\InputOption; |
14
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
15
|
|
|
use Symfony\Component\Console\Question\Question; |
16
|
|
|
|
17
|
|
|
class Dependencies extends Command |
18
|
|
|
{ |
19
|
|
|
protected $color; |
20
|
|
|
|
21
|
|
|
protected function configure(): void |
22
|
|
|
{ |
23
|
|
|
$this->setName('dependencies'); |
24
|
|
|
$this->setDescription('Return the package dependencies.'); |
25
|
|
|
$this->addOption('recursive', 'r', InputOption::VALUE_NONE, 'Return dependencies recursively.'); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
29
|
|
|
{ |
30
|
|
|
$helper = $this->getHelper('question'); |
31
|
|
|
$tree = new TreeHelper(); |
32
|
|
|
|
33
|
|
|
$packages = array_keys(iterator_to_array(Packages::packages())); |
34
|
|
|
$question = new Question('<question>Please type the package name:</question>' . \PHP_EOL); |
35
|
|
|
|
36
|
|
|
if (method_exists($question, 'setAutocompleterCallback')) { |
37
|
|
|
$callback = static function (string $userInput) use ($packages): array { |
38
|
|
|
return array_filter($packages, static function (string $packageName) use ($userInput) { |
39
|
|
|
return false !== mb_stripos($packageName, $userInput); |
40
|
|
|
}); |
41
|
|
|
}; |
42
|
|
|
$question->setAutocompleterCallback($callback); |
43
|
|
|
} else { |
44
|
|
|
$question->setAutocompleterValues($packages); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$rows = $this->getDependenciesOf( |
48
|
|
|
$helper->ask($input, $output, $question) ?? '', |
49
|
|
|
(bool) $input->getOption('recursive') |
50
|
|
|
); |
51
|
|
|
|
52
|
|
|
$tree->addArray($rows); |
53
|
|
|
$tree->printTree($output); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
private function getDependenciesOf(string $search, bool $isRecursive, bool $isChild = false): array |
|
|
|
|
57
|
|
|
{ |
58
|
|
|
$rows = []; |
59
|
|
|
$dependencies = PackageDependencies::get($search); |
60
|
|
|
|
61
|
|
|
foreach ($dependencies as $number => $dependency) { |
62
|
|
|
$row = $dependency . ': <fg=yellow>' . (Versions::get($dependency) ?? '~') . '</>'; |
63
|
|
|
|
64
|
|
|
if ($isRecursive) { |
65
|
|
|
$rows += $this->getDependenciesOf($dependency, $isRecursive, true); |
66
|
|
|
} else { |
67
|
|
|
$rows[$row] = []; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return [$search . ': <fg=yellow>' . (Versions::get($search) ?? '~') . '</>' => $rows]; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.