66 * @author Christian Weiske <[email protected] > 77 */
88
9- /**
10- * Dead simple autoloader
11- *
12- * @param string $className Name of class to load
13- *
14- * @return void
15- */
16- function __autoload ($ className )
17- {
18- $ className = ltrim ($ className , '\\' );
19- $ fileName = '' ;
20- $ namespace = '' ;
21- if ($ lastNsPos = strrpos ($ className , '\\' )) {
22- $ namespace = substr ($ className , 0 , $ lastNsPos );
23- $ className = substr ($ className , $ lastNsPos + 1 );
24- $ fileName = str_replace ('\\' , DIRECTORY_SEPARATOR , $ namespace ) . DIRECTORY_SEPARATOR ;
25- }
26- $ fileName .= str_replace ('_ ' , DIRECTORY_SEPARATOR , $ className ) . '.php ' ;
27- if (stream_resolve_include_path ($ fileName )) {
28- require_once $ fileName ;
9+ // support running this tool from git checkout
10+ $ projectDirectory = dirname (__DIR__ );
11+ if (is_dir ($ projectDirectory . DIRECTORY_SEPARATOR . 'vendor ' )) {
12+ set_include_path ($ projectDirectory . PATH_SEPARATOR . get_include_path ());
13+ }
14+
15+ // autoload composer classes
16+ $ composerAutoload = stream_resolve_include_path ('vendor/autoload.php ' );
17+ if (!$ composerAutoload ) {
18+ echo ("Cannot load json-schema library \n" );
19+ exit (1 );
20+ }
21+ require ($ composerAutoload );
22+
23+ $ arOptions = array ();
24+ $ arArgs = array ();
25+ array_shift ($ argv );//script itself
26+ foreach ($ argv as $ arg ) {
27+ if ($ arg {0 } == '- ' ) {
28+ $ arOptions [$ arg ] = true ;
29+ } else {
30+ $ arArgs [] = $ arg ;
2931 }
3032}
3133
34+ if (count ($ arArgs ) == 0
35+ || isset ($ arOptions ['--help ' ]) || isset ($ arOptions ['-h ' ])
36+ ) {
37+ echo <<<HLP
38+ Validate schema
39+ Usage: validate-json data.json
40+ or: validate-json data.json schema.json
41+
42+ Options:
43+ --dump-schema Output full schema and exit
44+ --dump-schema-url Output URL of schema
45+ --verbose Show additional output
46+ --quiet Suppress all output
47+ -h --help Show this help
48+
49+ HLP ;
50+ exit (1 );
51+ }
52+
53+ if (count ($ arArgs ) == 1 ) {
54+ $ pathData = $ arArgs [0 ];
55+ $ pathSchema = null ;
56+ } else {
57+ $ pathData = $ arArgs [0 ];
58+ $ pathSchema = getUrlFromPath ($ arArgs [1 ]);
59+ }
60+
3261/**
3362 * Show the json parse error that happened last
3463 *
@@ -44,7 +73,7 @@ function showJsonError()
4473 }
4574 }
4675
47- echo 'JSON parse error: ' . $ json_errors [json_last_error ()] . "\n" ;
76+ output ( 'JSON parse error: ' . $ json_errors [json_last_error ()] . "\n" ) ;
4877}
4978
5079function getUrlFromPath ($ path )
@@ -84,48 +113,18 @@ function parseHeaderValue($headerValue)
84113 return $ arData ;
85114}
86115
87-
88- // support running this tool from git checkout
89- if (is_dir (__DIR__ . '/../src/JsonSchema ' )) {
90- set_include_path (__DIR__ . '/../src ' . PATH_SEPARATOR . get_include_path ());
91- }
92-
93- $ arOptions = array ();
94- $ arArgs = array ();
95- array_shift ($ argv );//script itself
96- foreach ($ argv as $ arg ) {
97- if ($ arg {0 } == '- ' ) {
98- $ arOptions [$ arg ] = true ;
99- } else {
100- $ arArgs [] = $ arg ;
116+ /**
117+ * Send a string to the output stream, but only if --quiet is not enabled
118+ *
119+ * @param $str A string output
120+ */
121+ function output ($ str ) {
122+ global $ arOptions ;
123+ if (!isset ($ arOptions ['--quiet ' ])) {
124+ echo $ str ;
101125 }
102126}
103127
104- if (count ($ arArgs ) == 0
105- || isset ($ arOptions ['--help ' ]) || isset ($ arOptions ['-h ' ])
106- ) {
107- echo <<<HLP
108- Validate schema
109- Usage: validate-json data.json
110- or: validate-json data.json schema.json
111-
112- Options:
113- --dump-schema Output full schema and exit
114- --dump-schema-url Output URL of schema
115- -h --help Show this help
116-
117- HLP ;
118- exit (1 );
119- }
120-
121- if (count ($ arArgs ) == 1 ) {
122- $ pathData = $ arArgs [0 ];
123- $ pathSchema = null ;
124- } else {
125- $ pathData = $ arArgs [0 ];
126- $ pathSchema = getUrlFromPath ($ arArgs [1 ]);
127- }
128-
129128$ urlData = getUrlFromPath ($ pathData );
130129
131130$ context = stream_context_create (
@@ -141,14 +140,14 @@ $context = stream_context_create(
141140);
142141$ dataString = file_get_contents ($ pathData , false , $ context );
143142if ($ dataString == '' ) {
144- echo "Data file is not readable or empty. \n" ;
143+ output ( "Data file is not readable or empty. \n" ) ;
145144 exit (3 );
146145}
147146
148147$ data = json_decode ($ dataString );
149148unset($ dataString );
150149if ($ data === null ) {
151- echo "Error loading JSON data file \n" ;
150+ output ( "Error loading JSON data file \n" ) ;
152151 showJsonError ();
153152 exit (5 );
154153}
@@ -182,9 +181,9 @@ if ($pathSchema === null) {
182181
183182 //autodetect schema
184183 if ($ pathSchema === null ) {
185- echo "JSON data must be an object and have a \$schema property. \n" ;
186- echo "You can pass the schema file on the command line as well. \n" ;
187- echo "Schema autodetection failed. \n" ;
184+ output ( "JSON data must be an object and have a \$schema property. \n" ) ;
185+ output ( "You can pass the schema file on the command line as well. \n" ) ;
186+ output ( "Schema autodetection failed. \n" ) ;
188187 exit (6 );
189188 }
190189}
@@ -202,9 +201,9 @@ try {
202201 exit ();
203202 }
204203} catch (Exception $ e ) {
205- echo "Error loading JSON schema file \n" ;
206- echo $ urlSchema . "\n" ;
207- echo $ e ->getMessage () . "\n" ;
204+ output ( "Error loading JSON schema file \n" ) ;
205+ output ( $ urlSchema . "\n" ) ;
206+ output ( $ e ->getMessage () . "\n" ) ;
208207 exit (2 );
209208}
210209$ refResolver = new JsonSchema \SchemaStorage ($ retriever , $ resolver );
@@ -221,17 +220,19 @@ try {
221220 $ validator ->check ($ data , $ schema );
222221
223222 if ($ validator ->isValid ()) {
224- echo "OK. The supplied JSON validates against the schema. \n" ;
223+ if (isset ($ arOptions ['--verbose ' ])) {
224+ output ("OK. The supplied JSON validates against the schema. \n" );
225+ }
225226 } else {
226- echo "JSON does not validate. Violations: \n" ;
227+ output ( "JSON does not validate. Violations: \n" ) ;
227228 foreach ($ validator ->getErrors () as $ error ) {
228- echo sprintf ("[%s] %s \n" , $ error ['property ' ], $ error ['message ' ]);
229+ output ( sprintf ("[%s] %s \n" , $ error ['property ' ], $ error ['message ' ]) );
229230 }
230231 exit (23 );
231232 }
232233} catch (Exception $ e ) {
233- echo "JSON does not validate. Error: \n" ;
234- echo $ e ->getMessage () . "\n" ;
235- echo "Error code: " . $ e ->getCode () . "\n" ;
234+ output ( "JSON does not validate. Error: \n" ) ;
235+ output ( $ e ->getMessage () . "\n" ) ;
236+ output ( "Error code: " . $ e ->getCode () . "\n" ) ;
236237 exit (24 );
237238}
0 commit comments