|
2 | 2 |
|
3 | 3 | namespace Drupal\patternkit;
|
4 | 4 |
|
| 5 | +use Drupal\Core\Url; |
5 | 6 | use Drupal\patternkit\Entity\PatternInterface;
|
6 | 7 |
|
7 | 8 | /**
|
@@ -39,7 +40,7 @@ public function fetchJsonSchemaAsset($file_uri) {
|
39 | 40 | * @return object
|
40 | 41 | * The updated schema properties object.
|
41 | 42 | *
|
42 |
| - * @todo JSON Schema technically allows the following valid refs: |
| 43 | + * @example JSON Schema technically allows the following valid refs: |
43 | 44 | * - URI "example.json" "../../example.json" "./example.json"
|
44 | 45 | * - URL "https://json-schema.org/person.schema.json"
|
45 | 46 | * - JSON Pointer "#/definitions/example" "example.json#/example"
|
@@ -76,15 +77,23 @@ public static function schemaDereference($properties, PatternInterface $pattern)
|
76 | 77 | }
|
77 | 78 | $path = $value;
|
78 | 79 | if (strpos($value,'@') === 0) {
|
79 |
| - $library_name = strstr($value, '/', TRUE); |
80 |
| - $pattern_library = $library->getLibraryDefinitions()[trim($library_name, '@')]; |
81 |
| - $path = substr($value, strlen()); |
| 80 | + $library_namespace = strstr($value, '/', TRUE); |
| 81 | + $path = substr($value, strlen("$library_namespace/")); |
| 82 | + } |
| 83 | + $ref = ''; |
| 84 | + if (strstr($path, '#/')) { |
| 85 | + $ref = explode('#/', $path); |
| 86 | + $path = reset($ref); |
| 87 | + $ref = !empty($ref[1]) ? '#/' . $ref[1] : ''; |
82 | 88 | }
|
| 89 | + $library_name = $pattern->getLibrary(); |
83 | 90 | if (strstr($path, './')) {
|
| 91 | + $pattern_library = $library->getLibraryDefinitions()[trim($library_name, '@')]; |
| 92 | + $library_path = $pattern_library->getPatternInfo()['data'] ?? $pattern_library->getExtension()->getPath(); |
84 | 93 | /** @var \Drupal\Core\File\FileSystem $filesystem */
|
85 |
| - $path = realpath($pattern->getPath() . $path); |
| 94 | + $path = substr(str_replace('\\', '/', realpath($library_path . '/' . $path)), strlen(\Drupal::root() . '/' . $library_path . '/')); |
86 | 95 | }
|
87 |
| - $properties[$property] = 'api/patternkit/' . trim($value, '.json'); |
| 96 | + $properties[$property] = Url::fromUserInput('/api/patternkit/' . trim($library_namespace ?? $library_name, '@') . '/' . trim($path, '.json') . '/json')->toString() . $ref; |
88 | 97 | }
|
89 | 98 | return $properties;
|
90 | 99 | }
|
|
0 commit comments