jsonlint 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #!/usr/bin/env php
  2. <?php
  3. /*
  4. * This file is part of the JSON Lint package.
  5. *
  6. * (c) 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. function includeIfExists($file)
  12. {
  13. if (file_exists($file)) {
  14. return include $file;
  15. }
  16. }
  17. if ((!$loader = includeIfExists(__DIR__.'/../vendor/autoload.php')) && (!$loader = includeIfExists(__DIR__.'/../../../autoload.php'))) {
  18. $msg = 'You must set up the project dependencies, run the following commands:'.PHP_EOL.
  19. 'curl -sS https://getcomposer.org/installer | php'.PHP_EOL.
  20. 'php composer.phar install'.PHP_EOL;
  21. fwrite(STDERR, $msg);
  22. exit(1);
  23. }
  24. use Seld\JsonLint\JsonParser;
  25. $files = array();
  26. $quiet = false;
  27. if (isset($_SERVER['argc']) && $_SERVER['argc'] > 1) {
  28. for ($i = 1; $i < $_SERVER['argc']; $i++) {
  29. $arg = $_SERVER['argv'][$i];
  30. if ($arg == '-q' || $arg == '--quiet') {
  31. $quiet = true;
  32. } else {
  33. if ($arg == '-h' || $arg == '--help') {
  34. showUsage();
  35. } else {
  36. $files[] = $arg;
  37. }
  38. }
  39. }
  40. }
  41. if (!empty($files)) {
  42. // file linting
  43. $exitCode = 0;
  44. foreach ($files as $file) {
  45. $result = lintFile($file, $quiet);
  46. if ($result === false) {
  47. $exitCode = 1;
  48. }
  49. }
  50. exit($exitCode);
  51. } else {
  52. //stdin linting
  53. if ($contents = file_get_contents('php://stdin')) {
  54. lint($contents);
  55. } else {
  56. fwrite(STDERR, 'No file name or json input given' . PHP_EOL);
  57. exit(1);
  58. }
  59. }
  60. // stdin lint function
  61. function lint($content, $quiet = false)
  62. {
  63. $parser = new JsonParser();
  64. if ($err = $parser->lint($content)) {
  65. fwrite(STDERR, $err->getMessage() . ' (stdin)' . PHP_EOL);
  66. exit(1);
  67. }
  68. if (!$quiet) {
  69. echo 'Valid JSON (stdin)' . PHP_EOL;
  70. exit(0);
  71. }
  72. }
  73. // file lint function
  74. function lintFile($file, $quiet = false)
  75. {
  76. if (!preg_match('{^https?://}i', $file)) {
  77. if (!file_exists($file)) {
  78. fwrite(STDERR, 'File not found: ' . $file . PHP_EOL);
  79. return false;
  80. }
  81. if (!is_readable($file)) {
  82. fwrite(STDERR, 'File not readable: ' . $file . PHP_EOL);
  83. return false;
  84. }
  85. }
  86. $content = file_get_contents($file);
  87. $parser = new JsonParser();
  88. if ($err = $parser->lint($content)) {
  89. fwrite(STDERR, $file . ': ' . $err->getMessage() . PHP_EOL);
  90. return false;
  91. }
  92. if (!$quiet) {
  93. echo 'Valid JSON (' . $file . ')' . PHP_EOL;
  94. }
  95. return true;
  96. }
  97. // usage text function
  98. function showUsage()
  99. {
  100. echo 'Usage: jsonlint file [options]'.PHP_EOL;
  101. echo PHP_EOL;
  102. echo 'Options:'.PHP_EOL;
  103. echo ' -q, --quiet Cause jsonlint to be quiet when no errors are found'.PHP_EOL;
  104. echo ' -h, --help Show this message'.PHP_EOL;
  105. exit(0);
  106. }