ValidateCommand.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. /*
  3. * This file is part of Composer.
  4. *
  5. * (c) Nils Adermann <naderman@naderman.de>
  6. * Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Composer\Command;
  12. use Composer\Factory;
  13. use Composer\Package\Loader\ValidatingArrayLoader;
  14. use Composer\Plugin\CommandEvent;
  15. use Composer\Plugin\PluginEvents;
  16. use Composer\Util\ConfigValidator;
  17. use Symfony\Component\Console\Input\InputArgument;
  18. use Symfony\Component\Console\Input\InputInterface;
  19. use Symfony\Component\Console\Input\InputOption;
  20. use Symfony\Component\Console\Output\OutputInterface;
  21. /**
  22. * ValidateCommand
  23. *
  24. * @author Robert Schönthal <seroscho@googlemail.com>
  25. * @author Jordi Boggiano <j.boggiano@seld.be>
  26. */
  27. class ValidateCommand extends BaseCommand
  28. {
  29. /**
  30. * configure
  31. */
  32. protected function configure()
  33. {
  34. $this
  35. ->setName('validate')
  36. ->setDescription('Validates a composer.json and composer.lock.')
  37. ->setDefinition(array(
  38. new InputOption('no-check-all', null, InputOption::VALUE_NONE, 'Do not validate requires for overly strict/loose constraints'),
  39. new InputOption('no-check-lock', null, InputOption::VALUE_NONE, 'Do not check if lock file is up to date'),
  40. new InputOption('no-check-publish', null, InputOption::VALUE_NONE, 'Do not check for publish errors'),
  41. new InputOption('with-dependencies', 'A', InputOption::VALUE_NONE, 'Also validate the composer.json of all installed dependencies'),
  42. new InputOption('strict', null, InputOption::VALUE_NONE, 'Return a non-zero exit code for warnings as well as errors'),
  43. new InputArgument('file', InputArgument::OPTIONAL, 'path to composer.json file'),
  44. ))
  45. ->setHelp(
  46. <<<EOT
  47. The validate command validates a given composer.json and composer.lock
  48. Exit codes in case of errors are:
  49. 1 validation warning(s), only when --strict is given
  50. 2 validation error(s)
  51. 3 file unreadable or missing
  52. Read more at https://getcomposer.org/doc/03-cli.md#validate
  53. EOT
  54. );
  55. }
  56. /**
  57. * @param InputInterface $input
  58. * @param OutputInterface $output
  59. *
  60. * @return int
  61. */
  62. protected function execute(InputInterface $input, OutputInterface $output)
  63. {
  64. $file = $input->getArgument('file') ?: Factory::getComposerFile();
  65. $io = $this->getIO();
  66. if (!file_exists($file)) {
  67. $io->writeError('<error>' . $file . ' not found.</error>');
  68. return 3;
  69. }
  70. if (!is_readable($file)) {
  71. $io->writeError('<error>' . $file . ' is not readable.</error>');
  72. return 3;
  73. }
  74. $validator = new ConfigValidator($io);
  75. $checkAll = $input->getOption('no-check-all') ? 0 : ValidatingArrayLoader::CHECK_ALL;
  76. $checkPublish = !$input->getOption('no-check-publish');
  77. $checkLock = !$input->getOption('no-check-lock');
  78. $isStrict = $input->getOption('strict');
  79. list($errors, $publishErrors, $warnings) = $validator->validate($file, $checkAll);
  80. $lockErrors = array();
  81. $composer = Factory::create($io, $file, $input->hasParameterOption('--no-plugins'));
  82. $locker = $composer->getLocker();
  83. if ($locker->isLocked() && !$locker->isFresh()) {
  84. $lockErrors[] = 'The lock file is not up to date with the latest changes in composer.json, it is recommended that you run `composer update`.';
  85. }
  86. $this->outputResult($io, $file, $errors, $warnings, $checkPublish, $publishErrors, $checkLock, $lockErrors, true, $isStrict);
  87. // $errors include publish and lock errors when exists
  88. $exitCode = $errors ? 2 : ($isStrict && $warnings ? 1 : 0);
  89. if ($input->getOption('with-dependencies')) {
  90. $localRepo = $composer->getRepositoryManager()->getLocalRepository();
  91. foreach ($localRepo->getPackages() as $package) {
  92. $path = $composer->getInstallationManager()->getInstallPath($package);
  93. $file = $path . '/composer.json';
  94. if (is_dir($path) && file_exists($file)) {
  95. list($errors, $publishErrors, $warnings) = $validator->validate($file, $checkAll);
  96. $this->outputResult($io, $package->getPrettyName(), $errors, $warnings, $checkPublish, $publishErrors);
  97. $depCode = $errors ? 2 : ($isStrict && $warnings ? 1 : 0);
  98. $exitCode = max($depCode, $exitCode);
  99. }
  100. }
  101. }
  102. $commandEvent = new CommandEvent(PluginEvents::COMMAND, 'validate', $input, $output);
  103. $eventCode = $composer->getEventDispatcher()->dispatch($commandEvent->getName(), $commandEvent);
  104. $exitCode = max($eventCode, $exitCode);
  105. return $exitCode;
  106. }
  107. private function outputResult($io, $name, &$errors, &$warnings, $checkPublish = false, $publishErrors = array(), $checkLock = false, $lockErrors = array(), $printSchemaUrl = false, $isStrict = false)
  108. {
  109. if (!$errors && !$publishErrors && !$warnings) {
  110. $io->write('<info>' . $name . ' is valid</info>');
  111. } elseif (!$errors && !$publishErrors) {
  112. $io->writeError('<info>' . $name . ' is valid, but with a few warnings</info>');
  113. if ($printSchemaUrl) {
  114. $io->writeError('<warning>See https://getcomposer.org/doc/04-schema.md for details on the schema</warning>');
  115. }
  116. } elseif (!$errors) {
  117. $io->writeError('<info>' . $name . ' is valid for simple usage with composer but has</info>');
  118. $io->writeError('<info>strict errors that make it unable to be published as a package:</info>');
  119. if ($printSchemaUrl) {
  120. $io->writeError('<warning>See https://getcomposer.org/doc/04-schema.md for details on the schema</warning>');
  121. }
  122. } else {
  123. $io->writeError('<error>' . $name . ' is invalid, the following errors/warnings were found:</error>');
  124. }
  125. // If checking publish errors, display them as errors, otherwise just show them as warnings
  126. // Skip when it is a strict check and we don't want to check publish errors
  127. if ($checkPublish) {
  128. $errors = array_merge($errors, $publishErrors);
  129. } elseif (!$isStrict) {
  130. $warnings = array_merge($warnings, $publishErrors);
  131. }
  132. // If checking lock errors, display them as errors, otherwise just show them as warnings
  133. // Skip when it is a strict check and we don't want to check lock errors
  134. if ($checkLock) {
  135. $errors = array_merge($errors, $lockErrors);
  136. } elseif (!$isStrict) {
  137. $warnings = array_merge($warnings, $lockErrors);
  138. }
  139. $messages = array(
  140. 'error' => $errors,
  141. 'warning' => $warnings,
  142. );
  143. foreach ($messages as $style => $msgs) {
  144. foreach ($msgs as $msg) {
  145. $io->writeError('<' . $style . '>' . $msg . '</' . $style . '>');
  146. }
  147. }
  148. }
  149. }