-
Notifications
You must be signed in to change notification settings - Fork 358
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #390 from justinrainbow/6.0.0-dev
- Loading branch information
Showing
84 changed files
with
2,712 additions
and
1,004 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,29 +6,58 @@ | |
* @author Christian Weiske <[email protected]> | ||
*/ | ||
|
||
/** | ||
* Dead simple autoloader | ||
* | ||
* @param string $className Name of class to load | ||
* | ||
* @return void | ||
*/ | ||
function __autoload($className) | ||
{ | ||
$className = ltrim($className, '\\'); | ||
$fileName = ''; | ||
$namespace = ''; | ||
if ($lastNsPos = strrpos($className, '\\')) { | ||
$namespace = substr($className, 0, $lastNsPos); | ||
$className = substr($className, $lastNsPos + 1); | ||
$fileName = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR; | ||
} | ||
$fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php'; | ||
if (stream_resolve_include_path($fileName)) { | ||
require_once $fileName; | ||
// support running this tool from git checkout | ||
$projectDirectory = dirname(__DIR__); | ||
if (is_dir($projectDirectory . DIRECTORY_SEPARATOR . 'vendor')) { | ||
set_include_path($projectDirectory . PATH_SEPARATOR . get_include_path()); | ||
} | ||
|
||
// autoload composer classes | ||
$composerAutoload = stream_resolve_include_path('vendor/autoload.php'); | ||
if (!$composerAutoload) { | ||
echo("Cannot load json-schema library\n"); | ||
exit(1); | ||
} | ||
require($composerAutoload); | ||
|
||
$arOptions = array(); | ||
$arArgs = array(); | ||
array_shift($argv);//script itself | ||
foreach ($argv as $arg) { | ||
if ($arg{0} == '-') { | ||
$arOptions[$arg] = true; | ||
} else { | ||
$arArgs[] = $arg; | ||
} | ||
} | ||
|
||
if (count($arArgs) == 0 | ||
|| isset($arOptions['--help']) || isset($arOptions['-h']) | ||
) { | ||
echo <<<HLP | ||
Validate schema | ||
Usage: validate-json data.json | ||
or: validate-json data.json schema.json | ||
Options: | ||
--dump-schema Output full schema and exit | ||
--dump-schema-url Output URL of schema | ||
--verbose Show additional output | ||
--quiet Suppress all output | ||
-h --help Show this help | ||
HLP; | ||
exit(1); | ||
} | ||
|
||
if (count($arArgs) == 1) { | ||
$pathData = $arArgs[0]; | ||
$pathSchema = null; | ||
} else { | ||
$pathData = $arArgs[0]; | ||
$pathSchema = getUrlFromPath($arArgs[1]); | ||
} | ||
|
||
/** | ||
* Show the json parse error that happened last | ||
* | ||
|
@@ -44,7 +73,7 @@ function showJsonError() | |
} | ||
} | ||
|
||
echo 'JSON parse error: ' . $json_errors[json_last_error()] . "\n"; | ||
output('JSON parse error: ' . $json_errors[json_last_error()] . "\n"); | ||
} | ||
|
||
function getUrlFromPath($path) | ||
|
@@ -84,48 +113,18 @@ function parseHeaderValue($headerValue) | |
return $arData; | ||
} | ||
|
||
|
||
// support running this tool from git checkout | ||
if (is_dir(__DIR__ . '/../src/JsonSchema')) { | ||
set_include_path(__DIR__ . '/../src' . PATH_SEPARATOR . get_include_path()); | ||
} | ||
|
||
$arOptions = array(); | ||
$arArgs = array(); | ||
array_shift($argv);//script itself | ||
foreach ($argv as $arg) { | ||
if ($arg{0} == '-') { | ||
$arOptions[$arg] = true; | ||
} else { | ||
$arArgs[] = $arg; | ||
/** | ||
* Send a string to the output stream, but only if --quiet is not enabled | ||
* | ||
* @param $str A string output | ||
*/ | ||
function output($str) { | ||
global $arOptions; | ||
if (!isset($arOptions['--quiet'])) { | ||
echo $str; | ||
} | ||
} | ||
|
||
if (count($arArgs) == 0 | ||
|| isset($arOptions['--help']) || isset($arOptions['-h']) | ||
) { | ||
echo <<<HLP | ||
Validate schema | ||
Usage: validate-json data.json | ||
or: validate-json data.json schema.json | ||
Options: | ||
--dump-schema Output full schema and exit | ||
--dump-schema-url Output URL of schema | ||
-h --help Show this help | ||
HLP; | ||
exit(1); | ||
} | ||
|
||
if (count($arArgs) == 1) { | ||
$pathData = $arArgs[0]; | ||
$pathSchema = null; | ||
} else { | ||
$pathData = $arArgs[0]; | ||
$pathSchema = getUrlFromPath($arArgs[1]); | ||
} | ||
|
||
$urlData = getUrlFromPath($pathData); | ||
|
||
$context = stream_context_create( | ||
|
@@ -141,14 +140,14 @@ $context = stream_context_create( | |
); | ||
$dataString = file_get_contents($pathData, false, $context); | ||
if ($dataString == '') { | ||
echo "Data file is not readable or empty.\n"; | ||
output("Data file is not readable or empty.\n"); | ||
exit(3); | ||
} | ||
|
||
$data = json_decode($dataString); | ||
unset($dataString); | ||
if ($data === null) { | ||
echo "Error loading JSON data file\n"; | ||
output("Error loading JSON data file\n"); | ||
showJsonError(); | ||
exit(5); | ||
} | ||
|
@@ -182,9 +181,9 @@ if ($pathSchema === null) { | |
|
||
//autodetect schema | ||
if ($pathSchema === null) { | ||
echo "JSON data must be an object and have a \$schema property.\n"; | ||
echo "You can pass the schema file on the command line as well.\n"; | ||
echo "Schema autodetection failed.\n"; | ||
output("JSON data must be an object and have a \$schema property.\n"); | ||
output("You can pass the schema file on the command line as well.\n"); | ||
output("Schema autodetection failed.\n"); | ||
exit(6); | ||
} | ||
} | ||
|
@@ -202,9 +201,9 @@ try { | |
exit(); | ||
} | ||
} catch (Exception $e) { | ||
echo "Error loading JSON schema file\n"; | ||
echo $urlSchema . "\n"; | ||
echo $e->getMessage() . "\n"; | ||
output("Error loading JSON schema file\n"); | ||
output($urlSchema . "\n"); | ||
output($e->getMessage() . "\n"); | ||
exit(2); | ||
} | ||
$refResolver = new JsonSchema\SchemaStorage($retriever, $resolver); | ||
|
@@ -221,17 +220,19 @@ try { | |
$validator->check($data, $schema); | ||
|
||
if ($validator->isValid()) { | ||
echo "OK. The supplied JSON validates against the schema.\n"; | ||
if(isset($arOptions['--verbose'])) { | ||
output("OK. The supplied JSON validates against the schema.\n"); | ||
} | ||
} else { | ||
echo "JSON does not validate. Violations:\n"; | ||
output("JSON does not validate. Violations:\n"); | ||
foreach ($validator->getErrors() as $error) { | ||
echo sprintf("[%s] %s\n", $error['property'], $error['message']); | ||
output(sprintf("[%s] %s\n", $error['property'], $error['message'])); | ||
} | ||
exit(23); | ||
} | ||
} catch (Exception $e) { | ||
echo "JSON does not validate. Error:\n"; | ||
echo $e->getMessage() . "\n"; | ||
echo "Error code: " . $e->getCode() . "\n"; | ||
output("JSON does not validate. Error:\n"); | ||
output($e->getMessage() . "\n"); | ||
output("Error code: " . $e->getCode() . "\n"); | ||
exit(24); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.