ValidateCommand.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 make a complete validation'),
  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. EOT
  53. );
  54. }
  55. /**
  56. * @param InputInterface $input
  57. * @param OutputInterface $output
  58. *
  59. * @return int
  60. */
  61. protected function execute(InputInterface $input, OutputInterface $output)
  62. {
  63. $file = $input->getArgument('file') ?: Factory::getComposerFile();
  64. $io = $this->getIO();
  65. if (!file_exists($file)) {
  66. $io->writeError('<error>' . $file . ' not found.</error>');
  67. return 3;
  68. }
  69. if (!is_readable($file)) {
  70. $io->writeError('<error>' . $file . ' is not readable.</error>');
  71. return 3;
  72. }
  73. $validator = new ConfigValidator($io);
  74. $checkAll = $input->getOption('no-check-all') ? 0 : ValidatingArrayLoader::CHECK_ALL;
  75. $checkPublish = !$input->getOption('no-check-publish');
  76. $checkLock = !$input->getOption('no-check-lock');
  77. $isStrict = $input->getOption('strict');
  78. list($errors, $publishErrors, $warnings) = $validator->validate($file, $checkAll);
  79. $lockErrors = array();
  80. $composer = Factory::create($io, $file, $input->hasParameterOption('--no-plugins'));
  81. $locker = $composer->getLocker();
  82. if ($locker->isLocked() && !$locker->isFresh()) {
  83. $lockErrors[] = 'The lock file is not up to date with the latest changes in composer.json, it is recommended that you run `composer update`.';
  84. }
  85. $this->outputResult($io, $file, $errors, $warnings, $checkPublish, $publishErrors, $checkLock, $lockErrors, true, $isStrict);
  86. // $errors include publish and lock errors when exists
  87. $exitCode = $errors ? 2 : ($isStrict && $warnings ? 1 : 0);
  88. if ($input->getOption('with-dependencies')) {
  89. $localRepo = $composer->getRepositoryManager()->getLocalRepository();
  90. foreach ($localRepo->getPackages() as $package) {
  91. $path = $composer->getInstallationManager()->getInstallPath($package);
  92. $file = $path . '/composer.json';
  93. if (is_dir($path) && file_exists($file)) {
  94. list($errors, $publishErrors, $warnings) = $validator->validate($file, $checkAll);
  95. $this->outputResult($io, $package->getPrettyName(), $errors, $warnings, $checkPublish, $publishErrors);
  96. $depCode = $errors ? 2 : ($isStrict && $warnings ? 1 : 0);
  97. $exitCode = max($depCode, $exitCode);
  98. }
  99. }
  100. }
  101. $commandEvent = new CommandEvent(PluginEvents::COMMAND, 'validate', $input, $output);
  102. $eventCode = $composer->getEventDispatcher()->dispatch($commandEvent->getName(), $commandEvent);
  103. $exitCode = max($eventCode, $exitCode);
  104. return $exitCode;
  105. }
  106. private function outputResult($io, $name, &$errors, &$warnings, $checkPublish = false, $publishErrors = array(), $checkLock = false, $lockErrors = array(), $printSchemaUrl = false, $isStrict = false)
  107. {
  108. if (!$errors && !$publishErrors && !$warnings) {
  109. $io->write('<info>' . $name . ' is valid</info>');
  110. } elseif (!$errors && !$publishErrors) {
  111. $io->writeError('<info>' . $name . ' is valid, but with a few warnings</info>');
  112. if ($printSchemaUrl) {
  113. $io->writeError('<warning>See https://getcomposer.org/doc/04-schema.md for details on the schema</warning>');
  114. }
  115. } elseif (!$errors) {
  116. $io->writeError('<info>' . $name . ' is valid for simple usage with composer but has</info>');
  117. $io->writeError('<info>strict errors that make it unable to be published as a package:</info>');
  118. if ($printSchemaUrl) {
  119. $io->writeError('<warning>See https://getcomposer.org/doc/04-schema.md for details on the schema</warning>');
  120. }
  121. } else {
  122. $io->writeError('<error>' . $name . ' is invalid, the following errors/warnings were found:</error>');
  123. }
  124. // If checking publish errors, display them as errors, otherwise just show them as warnings
  125. // Skip when it is a strict check and we don't want to check publish errors
  126. if ($checkPublish) {
  127. $errors = array_merge($errors, $publishErrors);
  128. } elseif (!$isStrict) {
  129. $warnings = array_merge($warnings, $publishErrors);
  130. }
  131. // If checking lock errors, display them as errors, otherwise just show them as warnings
  132. // Skip when it is a strict check and we don't want to check lock errors
  133. if ($checkLock) {
  134. $errors = array_merge($errors, $lockErrors);
  135. } elseif (!$isStrict) {
  136. $warnings = array_merge($warnings, $lockErrors);
  137. }
  138. $messages = array(
  139. 'error' => $errors,
  140. 'warning' => $warnings,
  141. );
  142. foreach ($messages as $style => $msgs) {
  143. foreach ($msgs as $msg) {
  144. $io->writeError('<' . $style . '>' . $msg . '</' . $style . '>');
  145. }
  146. }
  147. }
  148. }