ValidateCommand.php 6.4 KB

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