6
6
* @author Christian Weiske <[email protected] >
7
7
*/
8
8
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 ;
29
31
}
30
32
}
31
33
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
+
32
61
/**
33
62
* Show the json parse error that happened last
34
63
*
@@ -44,7 +73,7 @@ function showJsonError()
44
73
}
45
74
}
46
75
47
- echo 'JSON parse error: ' . $ json_errors [json_last_error ()] . "\n" ;
76
+ output ( 'JSON parse error: ' . $ json_errors [json_last_error ()] . "\n" ) ;
48
77
}
49
78
50
79
function getUrlFromPath ($ path )
@@ -84,48 +113,18 @@ function parseHeaderValue($headerValue)
84
113
return $ arData ;
85
114
}
86
115
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 ;
101
125
}
102
126
}
103
127
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
-
129
128
$ urlData = getUrlFromPath ($ pathData );
130
129
131
130
$ context = stream_context_create (
@@ -141,14 +140,14 @@ $context = stream_context_create(
141
140
);
142
141
$ dataString = file_get_contents ($ pathData , false , $ context );
143
142
if ($ dataString == '' ) {
144
- echo "Data file is not readable or empty. \n" ;
143
+ output ( "Data file is not readable or empty. \n" ) ;
145
144
exit (3 );
146
145
}
147
146
148
147
$ data = json_decode ($ dataString );
149
148
unset($ dataString );
150
149
if ($ data === null ) {
151
- echo "Error loading JSON data file \n" ;
150
+ output ( "Error loading JSON data file \n" ) ;
152
151
showJsonError ();
153
152
exit (5 );
154
153
}
@@ -182,9 +181,9 @@ if ($pathSchema === null) {
182
181
183
182
//autodetect schema
184
183
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" ) ;
188
187
exit (6 );
189
188
}
190
189
}
@@ -202,9 +201,9 @@ try {
202
201
exit ();
203
202
}
204
203
} 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" ) ;
208
207
exit (2 );
209
208
}
210
209
$ refResolver = new JsonSchema \SchemaStorage ($ retriever , $ resolver );
@@ -221,17 +220,19 @@ try {
221
220
$ validator ->check ($ data , $ schema );
222
221
223
222
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
+ }
225
226
} else {
226
- echo "JSON does not validate. Violations: \n" ;
227
+ output ( "JSON does not validate. Violations: \n" ) ;
227
228
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 ' ]) );
229
230
}
230
231
exit (23 );
231
232
}
232
233
} 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" ) ;
236
237
exit (24 );
237
238
}
0 commit comments