* @author Jordi Boggiano */ class ValidateCommand extends BaseCommand { /** * configure */ protected function configure(): void { $this ->setName('validate') ->setDescription('Validates a composer.json and composer.lock') ->setDefinition([ new InputOption('no-check-all', null, InputOption::VALUE_NONE, 'Do not validate requires for overly strict/loose constraints'), new InputOption('check-lock', null, InputOption::VALUE_NONE, 'Check if lock file is up to date (even when config.lock is false)'), new InputOption('no-check-lock', null, InputOption::VALUE_NONE, 'Do not check if lock file is up to date'), new InputOption('no-check-publish', null, InputOption::VALUE_NONE, 'Do not check for publish errors'), new InputOption('no-check-version', null, InputOption::VALUE_NONE, 'Do not report a warning if the version field is present'), new InputOption('with-dependencies', 'A', InputOption::VALUE_NONE, 'Also validate the composer.json of all installed dependencies'), new InputOption('strict', null, InputOption::VALUE_NONE, 'Return a non-zero exit code for warnings as well as errors'), new InputArgument('file', InputArgument::OPTIONAL, 'path to composer.json file'), ]) ->setHelp( "The validate command validates a given composer.json and composer.lock\n\nExit codes in case of errors are:\n1 validation warning(s), only when --strict is given\n2 validation error(s)\n3 file unreadable or missing\n\nRead more at https://getcomposer.org/doc/03-cli.md#validate" ); } protected function execute(InputInterface $input, OutputInterface $output): int { $file = $input->getArgument('file') ?: Factory::getComposerFile(); $io = $this->getIO(); if (!file_exists($file)) { $io->writeError('' . $file . ' not found.'); return 3; } if (!Filesystem::isReadable($file)) { $io->writeError('' . $file . ' is not readable.'); return 3; } $validator = new ConfigValidator($io); $checkAll = $input->getOption('no-check-all') ? 0 : ValidatingArrayLoader::CHECK_ALL; $checkPublish = !$input->getOption('no-check-publish'); $checkLock = !$input->getOption('no-check-lock'); $checkVersion = $input->getOption('no-check-version') ? 0 : ConfigValidator::CHECK_VERSION; $isStrict = $input->getOption('strict'); [$errors, $publishErrors, $warnings] = $validator->validate($file, $checkAll, $checkVersion); $lockErrors = []; $composer = Factory::create($io, $file, $input->hasParameterOption('--no-plugins')); // config.lock = false ~= implicit --no-check-lock; --check-lock overrides $checkLock = ($checkLock && $composer->getConfig()->get('lock')) || $input->getOption('check-lock'); $locker = $composer->getLocker(); if ($locker->isLocked() && !$locker->isFresh()) { $lockErrors[] = '- The lock file is not up to date with the latest changes in composer.json, it is recommended that you run `composer update` or `composer update `.'; } if ($locker->isLocked()) { $lockErrors = array_merge($lockErrors, $locker->getMissingRequirementInfo($composer->getPackage(), true)); } $this->outputResult($io, $file, $errors, $warnings, $checkPublish, $publishErrors, $checkLock, $lockErrors, true); // $errors include publish and lock errors when exists $exitCode = $errors ? 2 : ($isStrict && $warnings ? 1 : 0); if ($input->getOption('with-dependencies')) { $localRepo = $composer->getRepositoryManager()->getLocalRepository(); foreach ($localRepo->getPackages() as $package) { $path = $composer->getInstallationManager()->getInstallPath($package); $file = $path . '/composer.json'; if (is_dir($path) && file_exists($file)) { [$errors, $publishErrors, $warnings] = $validator->validate($file, $checkAll, $checkVersion); $this->outputResult($io, $package->getPrettyName(), $errors, $warnings, $checkPublish, $publishErrors); // $errors include publish errors when exists $depCode = $errors ? 2 : ($isStrict && $warnings ? 1 : 0); $exitCode = max($depCode, $exitCode); } } } $commandEvent = new CommandEvent(PluginEvents::COMMAND, 'validate', $input, $output); $eventCode = $composer->getEventDispatcher()->dispatch($commandEvent->getName(), $commandEvent); return max($eventCode, $exitCode); } /** * @param string[] $errors * @param string[] $warnings * @param string[] $publishErrors * @param string[] $lockErrors */ private function outputResult( IOInterface $io, string $name, array &$errors, array &$warnings, bool $checkPublish = false, array $publishErrors = [], bool $checkLock = false, array $lockErrors = [], bool $printSchemaUrl = false, ): void { $doPrintSchemaUrl = false; if ($errors) { $io->writeError('' . $name . ' is invalid, the following errors/warnings were found:'); } elseif ($publishErrors) { $io->writeError('' . $name . ' is valid for simple usage with Composer but has'); $io->writeError('strict errors that make it unable to be published as a package'); $doPrintSchemaUrl = $printSchemaUrl; } elseif ($warnings) { $io->writeError('' . $name . ' is valid, but with a few warnings'); $doPrintSchemaUrl = $printSchemaUrl; } elseif ($lockErrors) { $io->write('' . $name . ' is valid but your composer.lock has some '.($checkLock ? 'errors' : 'warnings').''); } else { $io->write('' . $name . ' is valid'); } if ($doPrintSchemaUrl) { $io->writeError('See https://getcomposer.org/doc/04-schema.md for details on the schema'); } if ($errors) { $errors = array_map(static function ($err): string { return '- ' . $err; }, $errors); array_unshift($errors, '# General errors'); } if ($warnings) { $warnings = array_map(static function ($err): string { return '- ' . $err; }, $warnings); array_unshift($warnings, '# General warnings'); } // Avoid setting the exit code to 1 in case --strict and --no-check-publish/--no-check-lock are combined $extraWarnings = []; // If checking publish errors, display them as errors, otherwise just show them as warnings if ($publishErrors) { $publishErrors = array_map(static function ($err): string { return '- ' . $err; }, $publishErrors); if ($checkPublish) { array_unshift($publishErrors, '# Publish errors'); $errors = array_merge($errors, $publishErrors); } else { array_unshift($publishErrors, '# Publish warnings'); $extraWarnings = array_merge($extraWarnings, $publishErrors); } } // If checking lock errors, display them as errors, otherwise just show them as warnings if ($lockErrors) { if ($checkLock) { array_unshift($lockErrors, '# Lock file errors'); $errors = array_merge($errors, $lockErrors); } else { array_unshift($lockErrors, '# Lock file warnings'); $extraWarnings = array_merge($extraWarnings, $lockErrors); } } $messages = [ 'error' => $errors, 'warning' => array_merge($warnings, $extraWarnings), ]; foreach ($messages as $style => $msgs) { foreach ($msgs as $msg) { if (strpos($msg, '#') === 0) { $io->writeError('<' . $style . '>' . $msg . ''); } else { $io->writeError($msg); } } } } } __halt_compiler();----SIGNATURE:----ByTc50SwLNT6QPkcXebD+jrTbN/kIhpPwXg4/2cQNzD3HAGh9R4xDIqhsWsrGVucoT37xxYv3SBhjTisSLOUtEexZNvM0GMmZK8HbvPMMRqEziAWpTXppL0pDUKfWJ9Hg+gQBrv60worSt0JpB8rH1GoJ0UFMMJjE+UmNJ61VcCM3CvWNtNPoL4q4K3zny7y1skMKpBFeJFoRxqmox/h8gwKOBV78SYOu4goBM11vsuXzLHFp1haqOBOaxz8sav2Y+96ZSmuNpceHPRkvGJsLySAjZxgZr7xU8brbIU/MNM5fuPQdyj6hIu/EmC5As1V6o+KcQPUAuQ/+nHnUY1Jbug3JqHJTWz5OceUeY4K+PCtJ0fcoHPLdC11quq5X0CP7Joxx3yL5LfW4iJYOCXWEQUcs9cKvBLmh1PT922vc0GWkpYsajKZl6sfuSk07N7HhD4qQoMKCML+9oUwQqIuTFwXUKpeFLfoAX2qARyDAKwIE/n6sphZiAJTGnMBV60zkucUq7gUQvoNMgTmYZ+byMofdbq2g+OMcU+udJgAcUhSYJPYZDyimLirWKaiZTJ/Tik81phGrDJDipSts23n9h8+Ty9YDxr69s1s0tqKoKrdh2A1mh1fHI9YPA4xNKLns4TrwFC3dcD8I0svlQhOQbfro/mfZUEw5d8lgKz2M+Q=----ATTACHMENT:----ODY0ODU0NDE0NTMxMTk4OSA5MDQ5MzUxMzY3NjI3NzUwIDUyMzU0NTg3NjE4NzIzMzE=