Skip to content
Snippets Groups Projects

Draft: Issue #2707849 by Wim Leers, COBadger: Allow components to define asset libraries

5 files
+ 458
0
Compare changes
  • Side-by-side
  • Inline
Files
5
<?php
namespace Drupal\components\Component\Discovery;
use Drupal\Component\Discovery\YamlDirectoryDiscovery;
/**
* Discovers directories each with one YAML file in a set of directories.
*
* @todo Move into Drupal\Component\Discovery namespace in core.
*/
class DirectoryWithYamlDiscovery extends YamlDirectoryDiscovery {
/**
* The subdirectory to scan.
*
* @var string
*/
protected $subdirectory;
/**
* Constructs a DirectoryWithYamlDiscovery object.
*
* @param array $directories
* An array of directories to scan, keyed by the provider. The value can
* either be a string or an array of strings. The string values should be
* the path of a directory to scan.
* @param string $subdirectory
* The subdirectory to scan in each of the passed $directories.
*/
public function __construct(array $directories, $subdirectory) {
parent::__construct($directories, 'directory_with_yaml:' . $subdirectory);
$this->subdirectory = $subdirectory;
}
/**
* {@inheritdoc}
*/
protected function getIdentifier($file, array $data) {
return basename($data[static::FILE_KEY], '.yml');
}
/**
* Returns an array of providers keyed by file path.
*
* @return array
* An array of providers keyed by file path.
*/
protected function findFiles() {
$file_list = [];
foreach ($this->directories as $provider => $directories) {
$directories = (array) $directories;
foreach ($directories as $directory) {
// Check if there is a subdirectory with the specified name.
if (is_dir($directory) && is_dir($directory . '/' . $this->subdirectory)) {
// Now iterate over all subdirectories below the specifically named
// subdirectory, and check if a .yml file exists with the same name.
// For example:
// - Assuming $this->subdirectory === 'fancy'
// - Then this checks for 'fancy/foo/foo.yml', 'fancy/bar/bar.yml'
$iterator = new \FilesystemIterator($directory . '/' . $this->subdirectory);
/** @var \SplFileInfo $file_info */
foreach ($iterator as $file_info) {
if ($file_info->isDir()) {
$this->findFile($file_info, $provider, $file_list);
// Allow for two levels.
$nested_iterator = new \FilesystemIterator($directory . '/' . $this->subdirectory . '/' . $file_info->getBasename());
foreach ($nested_iterator as $nested_file_info) {
if ($nested_file_info->isDir()) {
$this->findFile($nested_file_info, $provider, $file_list);
}
}
}
}
}
}
}
return $file_list;
}
protected function findFile($file_info, $provider, array &$file_list) {
$yml_file_in_directory = $file_info->getPath() . '/' . $file_info->getBasename() . '/' . $file_info->getBasename() . '.yml';
if (is_file($yml_file_in_directory)) {
$file_list[$yml_file_in_directory] = $provider;
}
}
}
\ No newline at end of file
Loading