function IconFinder::getFilesFromPath

Get files from a local path.

This is a wrapper to use Symfony Finder with 2 extras features {group} and {icon_id}.

Parameters

string $source: The source path, which can be absolute (starting with '/') or relative to the definition folder.

string $relative_path: The relative path to the definition folder.

Return value

array<string, array<string, string|null>> The file discovered.

1 call to IconFinder::getFilesFromPath()
IconFinder::getFilesFromSources in core/lib/Drupal/Core/Theme/Icon/IconFinder.php
Create files from source paths.

File

core/lib/Drupal/Core/Theme/Icon/IconFinder.php, line 194

Class

IconFinder
Icon finder to discover files under specific paths or URLs.

Namespace

Drupal\Core\Theme\Icon

Code

private function getFilesFromPath(string $source, string $relative_path) : array {
  $path_info = pathinfo($source);
  $dirname = $path_info['dirname'] ?? '';
  $extension = $path_info['extension'] ?? '';
  $filename = $path_info['filename'] ?? '';
  if (empty($dirname)) {
    return [];
  }
  // Set extension to wildcard if empty, and validate against allowed
  // extensions.
  $extension = empty($extension) ? '*' : $extension;
  if ('*' !== $extension && !in_array($extension, self::ALLOWED_EXTENSION, TRUE)) {
    $this->logger
      ->warning('Invalid icon path extension @filename.@extension in source: @source', [
      '@filename' => $filename,
      '@extension' => $extension,
      '@source' => $source,
    ]);
    return [];
  }
  // Use allowed extension bracket for Finder if wildcard.
  $extension = '*' === $extension ? '{' . implode(',', self::ALLOWED_EXTENSION) . '}' : $extension;
  // Prepare filename wildcard if empty or with {icon_id} pattern.
  $filename_wildcard = empty($filename) ? '*' : str_replace(self::ICON_ID_PATTERN, '*', $filename);
  // If icons are in the same folder dirname is 'dot'.
  if ('.' === $dirname) {
    $dirname = '';
  }
  // Prepare path to search for icons for Finder::in().
  $path = str_starts_with($source, '/') ? $this->appRoot . $dirname : sprintf('%s/%s/%s', $this->appRoot, $relative_path, $dirname);
  // Prepare file names for Finder::name().
  $names = sprintf('%s.%s', $filename_wildcard, $extension);
  if (!($finder = $this->findFiles($path, $names))) {
    return [];
  }
  // Wildcard around filename are ignored for extractIconIdFromFilename.
  $filename = str_replace('*', '', $filename);
  return $this->processFoundFiles($finder, $source, $filename, self::determineGroupPosition($path));
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.