ExtensionJsonValidator.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /**
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along
  14. * with this program; if not, write to the Free Software Foundation, Inc.,
  15. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  16. * http://www.gnu.org/copyleft/gpl.html
  17. *
  18. * @file
  19. */
  20. use Composer\Spdx\SpdxLicenses;
  21. use JsonSchema\Validator;
  22. /**
  23. * @since 1.29
  24. */
  25. class ExtensionJsonValidator {
  26. /**
  27. * @var callable
  28. */
  29. private $missingDepCallback;
  30. /**
  31. * @param callable $missingDepCallback
  32. */
  33. public function __construct( callable $missingDepCallback ) {
  34. $this->missingDepCallback = $missingDepCallback;
  35. }
  36. /**
  37. * @codeCoverageIgnore
  38. * @return bool
  39. */
  40. public function checkDependencies() {
  41. if ( !class_exists( Validator::class ) ) {
  42. call_user_func( $this->missingDepCallback,
  43. 'The JsonSchema library cannot be found, please install it through composer.'
  44. );
  45. return false;
  46. } elseif ( !class_exists( SpdxLicenses::class ) ) {
  47. call_user_func( $this->missingDepCallback,
  48. 'The spdx-licenses library cannot be found, please install it through composer.'
  49. );
  50. return false;
  51. }
  52. return true;
  53. }
  54. /**
  55. * @param string $path file to validate
  56. * @return bool true if passes validation
  57. * @throws ExtensionJsonValidationError on any failure
  58. */
  59. public function validate( $path ) {
  60. $data = json_decode( file_get_contents( $path ) );
  61. if ( !is_object( $data ) ) {
  62. throw new ExtensionJsonValidationError( "$path is not valid JSON" );
  63. }
  64. if ( !isset( $data->manifest_version ) ) {
  65. throw new ExtensionJsonValidationError(
  66. "$path does not have manifest_version set." );
  67. }
  68. $version = $data->manifest_version;
  69. $schemaPath = __DIR__ . "/../../docs/extension.schema.v$version.json";
  70. // Not too old
  71. if ( $version < ExtensionRegistry::OLDEST_MANIFEST_VERSION ) {
  72. throw new ExtensionJsonValidationError(
  73. "$path is using a non-supported schema version"
  74. );
  75. } elseif ( $version > ExtensionRegistry::MANIFEST_VERSION ) {
  76. throw new ExtensionJsonValidationError(
  77. "$path is using a non-supported schema version"
  78. );
  79. }
  80. $licenseError = false;
  81. // Check if it's a string, if not, schema validation will display an error
  82. if ( isset( $data->{'license-name'} ) && is_string( $data->{'license-name'} ) ) {
  83. $licenses = new SpdxLicenses();
  84. $valid = $licenses->validate( $data->{'license-name'} );
  85. if ( !$valid ) {
  86. $licenseError = '[license-name] Invalid SPDX license identifier, '
  87. . 'see <https://spdx.org/licenses/>';
  88. }
  89. }
  90. $validator = new Validator;
  91. $validator->check( $data, (object)[ '$ref' => 'file://' . $schemaPath ] );
  92. if ( $validator->isValid() && !$licenseError ) {
  93. // All good.
  94. return true;
  95. } else {
  96. $out = "$path did not pass validation.\n";
  97. foreach ( $validator->getErrors() as $error ) {
  98. $out .= "[{$error['property']}] {$error['message']}\n";
  99. }
  100. if ( $licenseError ) {
  101. $out .= "$licenseError\n";
  102. }
  103. throw new ExtensionJsonValidationError( $out );
  104. }
  105. }
  106. }