ValidateCommand.php 3.3 KB

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