|
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
|
2 |
|
protected function configure(): void |
|
22
|
|
|
{ |
|
23
|
2 |
|
$this->setName('dependencies'); |
|
24
|
2 |
|
$this->setDescription('Return the package dependencies.'); |
|
25
|
2 |
|
$this->addOption('recursive', 'r', InputOption::VALUE_NONE, 'Return dependencies recursively.'); |
|
26
|
2 |
|
} |
|
27
|
|
|
|
|
28
|
2 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
29
|
|
|
{ |
|
30
|
2 |
|
$helper = $this->getHelper('question'); |
|
31
|
2 |
|
$tree = new TreeHelper(); |
|
32
|
|
|
|
|
33
|
2 |
|
$packages = array_keys(iterator_to_array(Packages::packages())); |
|
34
|
2 |
|
$question = new Question('<question>Please type the package name:</question>' . \PHP_EOL); |
|
35
|
|
|
|
|
36
|
2 |
|
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
|
2 |
|
}; |
|
42
|
2 |
|
$question->setAutocompleterCallback($callback); |
|
43
|
|
|
} else { |
|
44
|
|
|
$question->setAutocompleterValues($packages); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
2 |
|
$rows = $this->getDependenciesOf( |
|
48
|
2 |
|
$helper->ask($input, $output, $question) ?? '', |
|
49
|
2 |
|
(bool) $input->getOption('recursive') |
|
50
|
|
|
); |
|
51
|
|
|
|
|
52
|
2 |
|
$tree->addArray($rows); |
|
53
|
2 |
|
$tree->printTree($output); |
|
54
|
2 |
|
} |
|
55
|
|
|
|
|
56
|
2 |
|
private function getDependenciesOf(string $search, bool $isRecursive, bool $isChild = false): array |
|
|
|
|
|
|
57
|
|
|
{ |
|
58
|
2 |
|
$rows = []; |
|
59
|
2 |
|
$dependencies = PackageDependencies::get($search); |
|
60
|
|
|
|
|
61
|
2 |
|
foreach ($dependencies as $number => $dependency) { |
|
62
|
2 |
|
$row = $dependency . ': <fg=yellow>' . (Versions::get($dependency) ?? '~') . '</>'; |
|
63
|
|
|
|
|
64
|
2 |
|
if ($isRecursive) { |
|
65
|
1 |
|
$rows += $this->getDependenciesOf($dependency, $isRecursive, true); |
|
66
|
|
|
} else { |
|
67
|
1 |
|
$rows[$row] = []; |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
2 |
|
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.