|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Drupal\patternkit\PatternLibraryParser; |
| 4 | + |
| 5 | +use Drupal\Component\Serialization\SerializationInterface; |
| 6 | +use Drupal\Core\Asset\Exception\InvalidLibraryFileException; |
| 7 | +use Drupal\Core\Extension\ModuleHandlerInterface; |
| 8 | +use Drupal\Core\Theme\ThemeManagerInterface; |
| 9 | +use Drupal\patternkit\Pattern; |
| 10 | +use Drupal\patternkit\PatternEditorConfig; |
| 11 | +use Drupal\patternkit\PatternLibraryJSONParserTrait; |
| 12 | +use Drupal\patternkit\PatternLibraryParserBase; |
| 13 | + |
| 14 | +/** |
| 15 | + * Parses a File pattern library collection into usable metadata. |
| 16 | + */ |
| 17 | +class FilePatternLibraryParser extends PatternLibraryParserBase { |
| 18 | + use PatternLibraryJSONParserTrait; |
| 19 | + |
| 20 | + /** |
| 21 | + * FilePatternLibraryParser constructor. |
| 22 | + * |
| 23 | + * @param \Drupal\Component\Serialization\SerializationInterface $serializer |
| 24 | + * Serializes and de-serializes data. |
| 25 | + * @param string $root |
| 26 | + * The application root path. |
| 27 | + * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler |
| 28 | + * Allows modules to alter library parsing. |
| 29 | + * @param \Drupal\Core\Theme\ThemeManagerInterface $theme_manager |
| 30 | + * Allows themes to alter library parsing. |
| 31 | + */ |
| 32 | + public function __construct( |
| 33 | + SerializationInterface $serializer, |
| 34 | + $root, |
| 35 | + ModuleHandlerInterface $module_handler, |
| 36 | + ThemeManagerInterface $theme_manager) { |
| 37 | + |
| 38 | + $this->serializer = $serializer; |
| 39 | + parent::__construct($root, $module_handler, $theme_manager); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Fetches all assets for a pattern. |
| 44 | + * |
| 45 | + * {@inheritDoc} |
| 46 | + */ |
| 47 | + public function fetchPatternAssets(Pattern $pattern, |
| 48 | + PatternEditorConfig $config): Pattern { |
| 49 | + // @todo Add support for File lib attachments such as JS and images. |
| 50 | + $pattern->attachments = array(); |
| 51 | + return $pattern; |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Parses a given library file and allows modules and themes to alter it. |
| 56 | + * |
| 57 | + * This method sets the parsed information onto the library property. |
| 58 | + * |
| 59 | + * Library information is parsed from *.libraries.yml files; see |
| 60 | + * editor.libraries.yml for an example. Each entry starts with a machine name |
| 61 | + * and defines the following elements: |
| 62 | + * - patterns: A list of pattern libraries and subtypes to include. Each |
| 63 | + * subtype is keyed by the subtype path. |
| 64 | + * @code |
| 65 | + * patterns: |
| 66 | + * path/atoms: {type: File, category: atoms} |
| 67 | + * path/molecules: {type: File, category: molecules} |
| 68 | + * path/organisms: {} |
| 69 | + * @endcode |
| 70 | + * - dependencies: A list of libraries this library depends on. |
| 71 | + * - version: The library version. The string "VERSION" can be used to mean |
| 72 | + * the current Drupal core version. |
| 73 | + * - header: By default, JavaScript files are included in the footer. If the |
| 74 | + * script must be included in the header (along with all its dependencies), |
| 75 | + * set this to true. Defaults to false. |
| 76 | + * - minified: If the file is already minified, set this to true to avoid |
| 77 | + * minifying it again. Defaults to false. |
| 78 | + * - remote: If the library is a third-party script, this provides the |
| 79 | + * repository URL for reference. |
| 80 | + * - license: If the remote property is set, the license information is |
| 81 | + * required. It has 3 properties: |
| 82 | + * - name: The human-readable name of the license. |
| 83 | + * - url: The URL of the license file/information for the version of the |
| 84 | + * library used. |
| 85 | + * - gpl-compatible: A Boolean for whether this library is GPL compatible. |
| 86 | + * |
| 87 | + * See https://www.drupal.org/node/2274843#define-library for more |
| 88 | + * information. |
| 89 | + * |
| 90 | + * @param array $library |
| 91 | + * The data of the library that was registered. |
| 92 | + * @param string $path |
| 93 | + * The relative path to the extension. |
| 94 | + * |
| 95 | + * @return array |
| 96 | + * An array of parsed library data. |
| 97 | + * |
| 98 | + * @throws \Drupal\Core\Asset\Exception\InvalidLibraryFileException |
| 99 | + * Thrown when a parser exception was thrown. |
| 100 | + */ |
| 101 | + public function parsePatternLibraryInfo(array $library, $path): array { |
| 102 | + if (!file_exists($path)) { |
| 103 | + throw new InvalidLibraryFileException("Path $path does not exist."); |
| 104 | + } |
| 105 | + $metadata = []; |
| 106 | + // @todo Grab the extension from the plugin. |
| 107 | + $type = strtok($library['plugin'], '.'); |
| 108 | + if ($type !== 'file') { |
| 109 | + return []; |
| 110 | + } |
| 111 | + $ext = strtok('.'); |
| 112 | + foreach (self::discoverComponents($path, [$ext]) as $name => $data) { |
| 113 | + if (empty($data[$ext]) || !file_exists($data[$ext])) { |
| 114 | + continue; |
| 115 | + } |
| 116 | + // If the component has a json file, create the pattern from it. |
| 117 | + $category = $library['category'] ?? 'default'; |
| 118 | + $library_defaults = [ |
| 119 | + '$schema' => NULL, |
| 120 | + 'category' => $category, |
| 121 | + 'title' => $name, |
| 122 | + 'type' => 'object', |
| 123 | + 'format' => 'grid', |
| 124 | + 'license' => $library['license'] ?? [], |
| 125 | + 'name' => $name, |
| 126 | + 'properties' => (object) [], |
| 127 | + 'required' => [], |
| 128 | + 'version' => $library['version'] ?? '', |
| 129 | + ]; |
| 130 | + // Create the pattern from defaults. |
| 131 | + // @todo Have this cleverly infer defaults from the template. |
| 132 | + $pattern = $this->createPattern($name, $library_defaults); |
| 133 | + $pattern->filename = trim(substr($data[$ext], strlen($path)), '/\\'); |
| 134 | + $pattern->path = substr($pattern->filename, 0, -strlen('.' . $ext)); |
| 135 | + $pattern->template = file_get_contents($data[$ext]); |
| 136 | + // URL is redundant for the File based components, so we use it to |
| 137 | + // store namespace. |
| 138 | + $pattern->url = $library['name']; |
| 139 | + // @todo add default of library version fallback to extension version. |
| 140 | + $pattern->version = $pattern->version ?? 'VERSION'; |
| 141 | + $metadata['@' . $library['name'] . '/' . $pattern->path] = $pattern; |
| 142 | + } |
| 143 | + |
| 144 | + foreach ($metadata as $pattern_type => $pattern) { |
| 145 | + // Replace any $ref links with relative paths. |
| 146 | + if (!isset($pattern->schema['properties'])) { |
| 147 | + continue; |
| 148 | + } |
| 149 | + $pattern->schema['properties'] = static::schemaDereference( |
| 150 | + $pattern->schema['properties'], |
| 151 | + $metadata |
| 152 | + ); |
| 153 | + $metadata[$pattern_type] = $pattern; |
| 154 | + } |
| 155 | + return $metadata; |
| 156 | + } |
| 157 | + |
| 158 | +} |
0 commit comments