|
10 | 10 | use Drupal\patternkit\PatternEditorConfig;
|
11 | 11 | use Drupal\patternkit\PatternLibraryJSONParserTrait;
|
12 | 12 | use Drupal\patternkit\PatternLibraryParserBase;
|
| 13 | +use Symfony\Component\Finder\Iterator\RecursiveDirectoryIterator; |
13 | 14 |
|
14 | 15 | /**
|
15 | 16 | * Parses a File pattern library collection into usable metadata.
|
@@ -39,6 +40,50 @@ public function __construct(
|
39 | 40 | parent::__construct($root, $module_handler, $theme_manager);
|
40 | 41 | }
|
41 | 42 |
|
| 43 | + /** |
| 44 | + * Returns an array of file components grouped by file basename and extension. |
| 45 | + * |
| 46 | + * @param string $path |
| 47 | + * The fully-qualified path to discover component files. |
| 48 | + * @param array $filter |
| 49 | + * An optional filter of file extensions to search for. |
| 50 | + * |
| 51 | + * @return array |
| 52 | + * Array of file components. |
| 53 | + * [basename][extension] = filename. |
| 54 | + */ |
| 55 | + public static function discoverComponents($path, array $filter = []): array { |
| 56 | + $components = []; |
| 57 | + $rdit = new RecursiveDirectoryIterator($path, \FilesystemIterator::KEY_AS_PATHNAME | \FilesystemIterator::CURRENT_AS_FILEINFO); |
| 58 | + /** @var \SplFileInfo $file */ |
| 59 | + foreach (new \RecursiveIteratorIterator($rdit) as $file) { |
| 60 | + // Skip directories and non-files. |
| 61 | + if (!$file->isFile()) { |
| 62 | + continue; |
| 63 | + } |
| 64 | + $file_path = $file->getPath(); |
| 65 | + |
| 66 | + // Skip tests folders. |
| 67 | + if (strpos($file_path, '/tests') !== FALSE) { |
| 68 | + continue; |
| 69 | + } |
| 70 | + |
| 71 | + // Get the file extension for the file. |
| 72 | + $file_ext = $file->getExtension(); |
| 73 | + if (!in_array(strtolower($file_ext), $filter, TRUE)) { |
| 74 | + continue; |
| 75 | + } |
| 76 | + |
| 77 | + // We use file_basename as a unique key, it is required that the |
| 78 | + // JSON and twig file share this basename. |
| 79 | + $file_basename = $file->getBasename('.' . $file_ext); |
| 80 | + |
| 81 | + // Build an array of all the filenames of interest, keyed by name. |
| 82 | + $components[$file_basename][$file_ext] = "$file_path/$file_basename.$file_ext"; |
| 83 | + } |
| 84 | + return $components; |
| 85 | + } |
| 86 | + |
42 | 87 | /**
|
43 | 88 | * Fetches all assets for a pattern.
|
44 | 89 | *
|
|
0 commit comments