123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- <?php
- declare(strict_types=1);
- namespace Drush\Commands\composer_check;
- use Consolidation\OutputFormatters\StructuredData\RowsOfFields;
- use Drush\Commands\DrushCommands;
- use Symfony\Component\Yaml\Yaml;
- class ComposerCheckCommands extends DrushCommands {
-
- private $yaml;
- public function __construct() {
- $this->yaml = new Yaml();
- }
-
-
- public function composerCheck(string $lockPath = NULL) {
- $lockPath = $this->validateLockPath($lockPath);
- $jsonPath = $this->validateJsonPath($lockPath);
- ['run' => $jrPack, 'dev' => $jdPack] = $this->decodeJsonPackages($jsonPath);
- ['run' => $lrPack, 'dev' => $ldPack] = $this->decodeLockPackages($lockPath);
- $all = !!$this->input()->getOption('all');
- $yaml = !!$this->input()->getOption('yaml');
- $packages = ['run' => [], 'dev' => []];
- foreach ($jrPack as $package => $requirement) {
- if ($all || !empty($requirement)) {
- $package = mb_strtolower($package);
- $packages['run'][$package]['requirement'] = $requirement;
- }
- }
- foreach ($jdPack as $package => $requirement) {
- if ($all || !empty($requirement)) {
- $package = mb_strtolower($package);
- $packages['dev'][$package]['requirement'] = $requirement;
- }
- }
- foreach ($lrPack as $packageInfo) {
- $package = mb_strtolower($packageInfo['name']);
- if ($all || !empty($packages['run'][$package])) {
- $version = $packageInfo['version'];
- $packages['run'][$package]['version'] = $version;
- }
- }
- foreach ($ldPack as $packageInfo) {
- $package = mb_strtolower($packageInfo['name']);
- if ($all || !empty($packages['dev'][$package])) {
- $version = $packageInfo['version'];
- $packages['dev'][$package]['version'] = $version;
- }
- }
- ksort($packages['dev']);
- ksort($packages['run']);
- if ($yaml) {
- $this->output()->writeln($this->yaml->dump($packages, 3, 2));
- return NULL;
- }
- return $this->humanOutput($packages);
- }
-
- protected function humanOutput($packages): RowsOfFields {
-
- $rows = [];
- foreach ($packages as $kind => $kindPackages) {
- foreach ($kindPackages as $package => $info) {
- $rows["$package/$kind"] = [
- 'name' => $package,
- 'kind' => $kind,
- 'req' => $info['requirement'] ?? '',
- 'ver' => $info['version'] ?? '',
- ];
- }
- }
- ksort($rows);
- return new RowsOfFields($rows);
- }
-
- protected function decodeJsonPackages(string $jsonPath): array {
- $json = json_decode(file_get_contents($jsonPath), TRUE);
- $jsonPackages = $json['require'] ?? [];
- $jsonDevPackages = $json['require-dev'] ?? [];
- return ['run' => $jsonPackages, 'dev' => $jsonDevPackages];
- }
- protected function decodeLockPackages(string $lockPath): array {
- $file = json_decode(file_get_contents($lockPath), TRUE);
- $run = $file['packages'];
- $dev = $file['packages-dev'];
- $platform = $file['platform'];
- array_walk($platform, function (&$requirement, $component) {
- $requirement = [
- 'name' => $component,
- 'version' => $requirement,
- ];
- });
- $run = array_merge($run, $platform);
- $dev = array_merge($dev, $platform);
- return ['run' => $run, 'dev' => $dev];
- }
-
- protected function validateJsonPath(string $lockPath): string {
- $jsonPath = dirname($lockPath) . '/composer.json';
- if (!is_file($jsonPath) && is_readable($jsonPath)) {
- throw new \Exception("Cannot read composer.json file");
- }
- return $jsonPath;
- }
-
- protected function validateLockPath(?string $lockPath): ?string {
- if (empty($lockPath)) {
- $lockPath = dirname(DRUPAL_ROOT) . '/composer.lock';
- }
- if (!is_file($lockPath) && is_readable($lockPath)) {
- throw new \Exception("Cannot read composer.lock file");
- }
- return $lockPath;
- }
- }
|