You are here

class RegexDirectoryIterator in Drupal 9

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Component/FileSystem/RegexDirectoryIterator.php \Drupal\Component\FileSystem\RegexDirectoryIterator

Iterates over files whose names match a regular expression in a directory.

Hierarchy

  • class \Drupal\Component\FileSystem\RegexDirectoryIterator extends \Drupal\Component\FileSystem\FilterIterator

Expanded class hierarchy of RegexDirectoryIterator

3 files declare their use of RegexDirectoryIterator
HelpTopicDiscovery.php in core/modules/help_topics/src/HelpTopicDiscovery.php
RegexDirectoryIteratorTest.php in core/tests/Drupal/Tests/Component/FileSystem/RegexDirectoryIteratorTest.php
YamlDirectoryDiscovery.php in core/lib/Drupal/Component/Discovery/YamlDirectoryDiscovery.php

File

core/lib/Drupal/Component/FileSystem/RegexDirectoryIterator.php, line 8

Namespace

Drupal\Component\FileSystem
View source
class RegexDirectoryIterator extends \FilterIterator {

  /**
   * The regular expression to match.
   *
   * @var string
   */
  protected $regex;

  /**
   * RegexDirectoryIterator constructor.
   *
   * @param string $path
   *   The path to scan.
   * @param string $regex
   *   The regular expression to match, including delimiters. For example,
   *   /\.yml$/ would list only files ending in .yml.
   */
  public function __construct($path, $regex) {

    // Use FilesystemIterator to not iterate over the . and .. directories.
    $iterator = new \FilesystemIterator($path);
    parent::__construct($iterator);
    $this->regex = $regex;
  }

  /**
   * Implements \FilterIterator::accept().
   */
  public function accept() {

    /** @var \SplFileInfo $file_info */
    $file_info = $this
      ->getInnerIterator()
      ->current();
    return $file_info
      ->isFile() && preg_match($this->regex, $file_info
      ->getFilename());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
RegexDirectoryIterator::$regex protected property The regular expression to match.
RegexDirectoryIterator::accept public function Implements \FilterIterator::accept().
RegexDirectoryIterator::__construct public function RegexDirectoryIterator constructor.