ValidateCommand.php 6.7 KB

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