@@ -22,7 +22,7 @@ class SchemaDefinitionDictionary extends ArrayObject
2222 /**
2323 * SchemaDefinitionDictionary constructor.
2424 */
25- public function __construct (private ? JsonSchema $ schema = null )
25+ public function __construct (private JsonSchema $ schema )
2626 {
2727 parent ::__construct ();
2828 }
@@ -135,104 +135,24 @@ private function parseExternalFile(
135135 SchemaProcessor $ schemaProcessor ,
136136 array &$ path ,
137137 ): ?SchemaDefinition {
138- $ jsonSchemaFilePath = $ this ->getFullRefURL ($ jsonSchemaFile ) ?: $ this ->getLocalRefPath ($ jsonSchemaFile );
139-
140- if ($ jsonSchemaFilePath === null ) {
141- throw new SchemaException ("Reference to non existing JSON-Schema file $ jsonSchemaFile " );
142- }
143-
144- $ jsonSchema = file_get_contents ($ jsonSchemaFilePath );
145-
146- if (!$ jsonSchema || !($ decodedJsonSchema = json_decode ($ jsonSchema , true ))) {
147- throw new SchemaException ("Invalid JSON-Schema file $ jsonSchemaFilePath " );
148- }
138+ $ jsonSchema = $ schemaProcessor ->getSchemaProvider ()->getRef (
139+ $ this ->schema ->getFile (),
140+ $ this ->schema ->getJson ()['$id ' ] ?? null ,
141+ $ jsonSchemaFile ,
142+ );
149143
150144 // set up a dummy schema to fetch the definitions from the external file
151145 $ schema = new Schema (
152146 '' ,
153147 $ schemaProcessor ->getCurrentClassPath (),
154148 'ExternalSchema ' ,
155- $ externalSchema = new JsonSchema ( $ jsonSchemaFilePath , $ decodedJsonSchema ) ,
156- new self ($ externalSchema ),
149+ $ jsonSchema ,
150+ new self ($ jsonSchema ),
157151 );
158152
159153 $ schema ->getSchemaDictionary ()->setUpDefinitionDictionary ($ schemaProcessor , $ schema );
160154 $ this ->parsedExternalFileSchemas [$ jsonSchemaFile ] = $ schema ;
161155
162156 return $ schema ->getSchemaDictionary ()->getDefinition ($ externalKey , $ schemaProcessor , $ path );
163157 }
164-
165- /**
166- * Try to build a full URL to fetch the schema from utilizing the $id field of the schema
167- */
168- private function getFullRefURL (string $ jsonSchemaFile ): ?string
169- {
170- if (filter_var ($ jsonSchemaFile , FILTER_VALIDATE_URL )) {
171- return $ jsonSchemaFile ;
172- }
173-
174- if ($ this ->schema === null
175- || !filter_var ($ this ->schema ->getJson ()['$id ' ] ?? $ this ->schema ->getFile (), FILTER_VALIDATE_URL )
176- || ($ idURL = parse_url ($ this ->schema ->getJson ()['$id ' ] ?? $ this ->schema ->getFile ())) === false
177- ) {
178- return null ;
179- }
180-
181- $ baseURL = $ idURL ['scheme ' ] . ':// ' . $ idURL ['host ' ] . (isset ($ idURL ['port ' ]) ? ': ' . $ idURL ['port ' ] : '' );
182-
183- // root relative $ref
184- if (str_starts_with ($ jsonSchemaFile , '/ ' )) {
185- return $ baseURL . $ jsonSchemaFile ;
186- }
187-
188- // relative $ref against the path of $id
189- $ segments = explode ('/ ' , rtrim (dirname ($ idURL ['path ' ] ?? '/ ' ), '/ ' ) . '/ ' . $ jsonSchemaFile );
190- $ output = [];
191-
192- foreach ($ segments as $ seg ) {
193- if ($ seg === '' || $ seg === '. ' ) {
194- continue ;
195- }
196- if ($ seg === '.. ' ) {
197- array_pop ($ output );
198- continue ;
199- }
200- $ output [] = $ seg ;
201- }
202-
203- return $ baseURL . '/ ' . implode ('/ ' , $ output );
204- }
205-
206- private function getLocalRefPath (string $ jsonSchemaFile ): ?string
207- {
208- $ currentDir = dirname ($ this ->schema ->getFile ());
209- // windows compatibility
210- $ jsonSchemaFile = str_replace ('\\' , '/ ' , $ jsonSchemaFile );
211-
212- // relative paths to the current location
213- if (!str_starts_with ($ jsonSchemaFile , '/ ' )) {
214- $ candidate = $ currentDir . '/ ' . $ jsonSchemaFile ;
215-
216- return file_exists ($ candidate ) ? $ candidate : null ;
217- }
218-
219- // absolute paths: traverse up to find the context root directory
220- $ relative = ltrim ($ jsonSchemaFile , '/ ' );
221-
222- $ dir = $ currentDir ;
223- while (true ) {
224- $ candidate = $ dir . '/ ' . $ relative ;
225- if (file_exists ($ candidate )) {
226- return $ candidate ;
227- }
228-
229- $ parent = dirname ($ dir );
230- if ($ parent === $ dir ) {
231- break ;
232- }
233- $ dir = $ parent ;
234- }
235-
236- return null ;
237- }
238158}
0 commit comments