RegexDirectoryIterator.php

Same filename in other branches
  1. 9 core/lib/Drupal/Component/FileSystem/RegexDirectoryIterator.php
  2. 8.9.x core/lib/Drupal/Component/FileSystem/RegexDirectoryIterator.php
  3. 11.x core/lib/Drupal/Component/FileSystem/RegexDirectoryIterator.php

Namespace

Drupal\Component\FileSystem

File

core/lib/Drupal/Component/FileSystem/RegexDirectoryIterator.php

View source
<?php

namespace Drupal\Component\FileSystem;


/**
 * Iterates over files whose names match a regular expression in a directory.
 */
class RegexDirectoryIterator extends \RegexIterator {
    
    /**
     * 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) {
        parent::__construct(new \FilesystemIterator($path), $regex);
    }
    
    /**
     * Implements \RegexIterator::accept().
     */
    public function accept() {
        
        /** @var \SplFileInfo $file_info */
        $file_info = $this->getInnerIterator()
            ->current();
        return $file_info->isFile() && preg_match($this->getRegex(), $file_info->getFilename());
    }

}

Classes

Title Deprecated Summary
RegexDirectoryIterator Iterates over files whose names match a regular expression in a directory.

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