ConfigValidator.php 3.6 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\Util;
  12. use Composer\Package\Loader\ArrayLoader;
  13. use Composer\Package\Loader\ValidatingArrayLoader;
  14. use Composer\Json\JsonValidationException;
  15. use Composer\IO\IOInterface;
  16. use Composer\Json\JsonFile;
  17. /**
  18. * Validates a composer configuration.
  19. *
  20. * @author Robert Schönthal <seroscho@googlemail.com>
  21. * @author Jordi Boggiano <j.boggiano@seld.be>
  22. */
  23. class ConfigValidator
  24. {
  25. private $io;
  26. public function __construct(IOInterface $io)
  27. {
  28. $this->io = $io;
  29. }
  30. /**
  31. * Validates the config, and returns the result.
  32. *
  33. * @param string $file The path to the file
  34. *
  35. * @return array a triple containing the errors, publishable errors, and warnings
  36. */
  37. public function validate($file)
  38. {
  39. $errors = array();
  40. $publishErrors = array();
  41. $warnings = array();
  42. // validate json schema
  43. $laxValid = false;
  44. $valid = false;
  45. try {
  46. $json = new JsonFile($file, new RemoteFilesystem($this->io));
  47. $manifest = $json->read();
  48. $json->validateSchema(JsonFile::LAX_SCHEMA);
  49. $laxValid = true;
  50. $json->validateSchema();
  51. $valid = true;
  52. } catch (JsonValidationException $e) {
  53. foreach ($e->getErrors() as $message) {
  54. if ($laxValid) {
  55. $publishErrors[] = $message;
  56. } else {
  57. $errors[] = $message;
  58. }
  59. }
  60. } catch (\Exception $e) {
  61. $errors[] = $e->getMessage();
  62. return array($errors, $publishErrors, $warnings);
  63. }
  64. // validate actual data
  65. if (!empty($manifest['license'])) {
  66. $licenseValidator = new SpdxLicenseIdentifier();
  67. if (!$licenseValidator->validate($manifest['license'])) {
  68. $warnings[] = sprintf(
  69. 'License %s is not a valid SPDX license identifier, see http://www.spdx.org/licenses/ if you use an open license',
  70. json_encode($manifest['license'])
  71. );
  72. }
  73. } else {
  74. $warnings[] = 'No license specified, it is recommended to do so';
  75. }
  76. if (!empty($manifest['name']) && preg_match('{[A-Z]}', $manifest['name'])) {
  77. $suggestName = preg_replace('{(?:([a-z])([A-Z])|([A-Z])([A-Z][a-z]))}', '\\1\\3-\\2\\4', $manifest['name']);
  78. $suggestName = strtolower($suggestName);
  79. $warnings[] = sprintf(
  80. 'Name "%s" does not match the best practice (e.g. lower-cased/with-dashes). We suggest using "%s" instead. As such you will not be able to submit it to Packagist.',
  81. $manifest['name'],
  82. $suggestName
  83. );
  84. }
  85. // TODO validate package repositories' packages using the same technique as below
  86. try {
  87. $loader = new ValidatingArrayLoader(new ArrayLoader(), false);
  88. if (!isset($manifest['version'])) {
  89. $manifest['version'] = '1.0.0';
  90. }
  91. if (!isset($manifest['name'])) {
  92. $manifest['name'] = 'dummy/dummy';
  93. }
  94. $loader->load($manifest);
  95. } catch (\Exception $e) {
  96. $errors = array_merge($errors, explode("\n", $e->getMessage()));
  97. }
  98. return array($errors, $publishErrors, $warnings);
  99. }
  100. }