ValidateCommand.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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\Package\Loader\ValidatingArrayLoader;
  13. use Composer\Util\ConfigValidator;
  14. use Symfony\Component\Console\Input\InputArgument;
  15. use Symfony\Component\Console\Input\InputInterface;
  16. use Symfony\Component\Console\Input\InputOption;
  17. use Symfony\Component\Console\Output\OutputInterface;
  18. /**
  19. * ValidateCommand
  20. *
  21. * @author Robert Schönthal <seroscho@googlemail.com>
  22. * @author Jordi Boggiano <j.boggiano@seld.be>
  23. */
  24. class ValidateCommand extends Command
  25. {
  26. /**
  27. * configure
  28. */
  29. protected function configure()
  30. {
  31. $this
  32. ->setName('validate')
  33. ->setDescription('Validates a composer.json')
  34. ->setDefinition(array(
  35. new InputOption('no-check-all', null, InputOption::VALUE_NONE, 'Do not make a complete validation'),
  36. new InputOption('no-check-publish', null, InputOption::VALUE_NONE, 'Do not check for publish errors'),
  37. new InputArgument('file', InputArgument::OPTIONAL, 'path to composer.json file', './composer.json')
  38. ))
  39. ->setHelp(<<<EOT
  40. The validate command validates a given composer.json
  41. EOT
  42. );
  43. }
  44. /**
  45. * @param InputInterface $input
  46. * @param OutputInterface $output
  47. *
  48. * @return int
  49. */
  50. protected function execute(InputInterface $input, OutputInterface $output)
  51. {
  52. $file = $input->getArgument('file');
  53. if (!file_exists($file)) {
  54. $this->getIO()->writeError('<error>' . $file . ' not found.</error>');
  55. return 1;
  56. }
  57. if (!is_readable($file)) {
  58. $this->getIO()->writeError('<error>' . $file . ' is not readable.</error>');
  59. return 1;
  60. }
  61. $validator = new ConfigValidator($this->getIO());
  62. $checkAll = $input->getOption('no-check-all') ? 0 : ValidatingArrayLoader::CHECK_ALL;
  63. $checkPublish = !$input->getOption('no-check-publish');
  64. list($errors, $publishErrors, $warnings) = $validator->validate($file, $checkAll);
  65. // output errors/warnings
  66. if (!$errors && !$publishErrors && !$warnings) {
  67. $this->getIO()->write('<info>' . $file . ' is valid</info>');
  68. } elseif (!$errors && !$publishErrors) {
  69. $this->getIO()->writeError('<info>' . $file . ' is valid, but with a few warnings</info>');
  70. $this->getIO()->writeError('<warning>See http://getcomposer.org/doc/04-schema.md for details on the schema</warning>');
  71. } elseif (!$errors) {
  72. $this->getIO()->writeError('<info>' . $file . ' is valid for simple usage with composer but has</info>');
  73. $this->getIO()->writeError('<info>strict errors that make it unable to be published as a package:</info>');
  74. $this->getIO()->writeError('<warning>See http://getcomposer.org/doc/04-schema.md for details on the schema</warning>');
  75. } else {
  76. $this->getIO()->writeError('<error>' . $file . ' is invalid, the following errors/warnings were found:</error>');
  77. }
  78. $messages = array(
  79. 'error' => $errors,
  80. 'warning' => $warnings,
  81. );
  82. // If checking publish errors, display them errors, otherwise just show them as warnings
  83. if ($checkPublish) {
  84. $messages['error'] = array_merge($messages['error'], $publishErrors);
  85. } else {
  86. $messages['warning'] = array_merge($messages['warning'], $publishErrors);
  87. }
  88. foreach ($messages as $style => $msgs) {
  89. foreach ($msgs as $msg) {
  90. $this->getIO()->writeError('<' . $style . '>' . $msg . '</' . $style . '>');
  91. }
  92. }
  93. return $errors || ($publishErrors && $checkPublish) ? 1 : 0;
  94. }
  95. }