ValidateCommand.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 Symfony\Component\Console\Input\InputInterface;
  13. use Symfony\Component\Console\Input\InputArgument;
  14. use Symfony\Component\Console\Output\OutputInterface;
  15. use Composer\Json\JsonFile;
  16. use Composer\Json\JsonValidationException;
  17. use Composer\Util\RemoteFilesystem;
  18. use Composer\Util\SpdxLicenseIdentifier;
  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')
  35. ->setDefinition(array(
  36. new InputArgument('file', InputArgument::OPTIONAL, 'path to composer.json file', './composer.json')
  37. ))
  38. ->setHelp(<<<EOT
  39. The validate command validates a given composer.json
  40. EOT
  41. );
  42. }
  43. /**
  44. * @param InputInterface $input
  45. * @param OutputInterface $output
  46. *
  47. * @return int
  48. */
  49. protected function execute(InputInterface $input, OutputInterface $output)
  50. {
  51. $file = $input->getArgument('file');
  52. if (!file_exists($file)) {
  53. $output->writeln('<error>' . $file . ' not found.</error>');
  54. return 1;
  55. }
  56. if (!is_readable($file)) {
  57. $output->writeln('<error>' . $file . ' is not readable.</error>');
  58. return 1;
  59. }
  60. $laxValid = false;
  61. try {
  62. $json = new JsonFile($file, new RemoteFilesystem($this->getIO()));
  63. $manifest = $json->read();
  64. $json->validateSchema(JsonFile::LAX_SCHEMA);
  65. $laxValid = true;
  66. $json->validateSchema();
  67. } catch (JsonValidationException $e) {
  68. if ($laxValid) {
  69. $output->writeln('<info>' . $file . ' is valid for simple usage with composer but has</info>');
  70. $output->writeln('<info>strict errors that make it unable to be published as a package:</info>');
  71. } else {
  72. $output->writeln('<error>' . $file . ' is invalid, the following errors were found:</error>');
  73. }
  74. foreach ($e->getErrors() as $message) {
  75. $output->writeln('<error>' . $message . '</error>');
  76. }
  77. return 1;
  78. } catch (\Exception $e) {
  79. $output->writeln('<error>' . $file . ' contains a JSON Syntax Error:</error>');
  80. $output->writeln('<error>' . $e->getMessage() . '</error>');
  81. return 1;
  82. }
  83. if (isset($manifest['license'])) {
  84. $licenseValidator = new SpdxLicenseIdentifier();
  85. if (!$licenseValidator->validate($manifest['license'])) {
  86. $output->writeln(sprintf(
  87. '<warning>License "%s" is not a valid SPDX license identifier</warning>'."\n".
  88. '<warning>see http://www.spdx.org/licenses/ and http://getcomposer.org/doc/04-schema.md#license</warning>',
  89. print_r($manifest['license'], true)
  90. ));
  91. }
  92. } else {
  93. $output->writeln('<warning>No license specified.</warning>');
  94. }
  95. $output->writeln('<info>' . $file . ' is valid</info>');
  96. return 0;
  97. }
  98. }