|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * @file |
| 5 | + * Utility functions for Patternkit. |
| 6 | + */ |
| 7 | + |
| 8 | +/** |
| 9 | + * Utility function to get all Patternkit module metadata. |
| 10 | + * |
| 11 | + * @param string|null $subtype |
| 12 | + * If specified, return only the first metadata found for this subtype. |
| 13 | + * @param bool $reset |
| 14 | + * Optionally force the meta data to be reloaded. |
| 15 | + * |
| 16 | + * @return array|\PatternkitPattern |
| 17 | + * Array of metadata objects found or object if specific module requested. |
| 18 | + * |
| 19 | + * @todo Add support for multiple library implementation of a pattern. |
| 20 | + */ |
| 21 | +function _patternkit_get_metadata($subtype = NULL, $reset = FALSE) { |
| 22 | + $metadata = array(); |
| 23 | + foreach (patternkit_pattern_libraries() as $lib) { |
| 24 | + if ($subtype === NULL) { |
| 25 | + $metadata += $lib->getCachedMetadata(NULL, $reset); |
| 26 | + continue; |
| 27 | + } |
| 28 | + $module = $lib->getCachedMetadata($subtype, $reset); |
| 29 | + if ($module !== NULL) { |
| 30 | + return $module; |
| 31 | + } |
| 32 | + } |
| 33 | + return $metadata; |
| 34 | +} |
| 35 | + |
| 36 | +/** |
| 37 | + * Parses schema properties for $ref and updates their location. |
| 38 | + * |
| 39 | + * @param object $properties |
| 40 | + * The properties to parse. |
| 41 | + * @param array &$metadata |
| 42 | + * The library metadata to lookup referenced patterns in. |
| 43 | + * |
| 44 | + * @return object |
| 45 | + * The updated schema properties object. |
| 46 | + */ |
| 47 | +function _patternkit_schema_ref($properties, array &$metadata) { |
| 48 | + foreach ($properties as $property => $value) { |
| 49 | + if (!is_scalar($value)) { |
| 50 | + $new_value = _patternkit_schema_ref($value, $metadata); |
| 51 | + if (is_object($properties)) { |
| 52 | + $properties->{$property} = $new_value; |
| 53 | + } |
| 54 | + if (is_array($properties)) { |
| 55 | + $properties[$property] = $new_value; |
| 56 | + } |
| 57 | + continue; |
| 58 | + } |
| 59 | + if ($property !== '$ref') { |
| 60 | + continue; |
| 61 | + } |
| 62 | + $pattern = strstr($value, '.json', TRUE); |
| 63 | + $ref = substr($value, strlen("$pattern.json")); |
| 64 | + if (!isset($metadata[$pattern])) { |
| 65 | + unset($properties->{$property}); |
| 66 | + continue; |
| 67 | + } |
| 68 | + $properties->{$property} = $metadata[$pattern]->url . $ref; |
| 69 | + } |
| 70 | + return $properties; |
| 71 | +} |
0 commit comments