FilesystemIterator example


  public function deleteAll($prefix = '') {
    $files = $this->listAll($prefix);
    $success = !empty($files);
    foreach ($files as $name) {
      if (!$this->delete($name) && $success) {
        $success = FALSE;
      }
    }
    if ($success && $this->collection != StorageInterface::DEFAULT_COLLECTION) {
      // Remove empty directories.       if (!(new \FilesystemIterator($this->getCollectionDirectory()))->valid()) {
        $this->getFileSystem()->rmdir($this->getCollectionDirectory());
      }
    }
    return $success;
  }

  /** * {@inheritdoc} */
  public function createCollection($collection) {
    return new static(
      

                    }

                    if (!file_exists($tmpName) && self::box('rename', $file$tmpName)) {
                        $origFile = $file;
                        $file = $tmpName;
                    } else {
                        $origFile = null;
                    }
                }

                $filesystemIterator = new \FilesystemIterator($file, \FilesystemIterator::CURRENT_AS_PATHNAME | \FilesystemIterator::SKIP_DOTS);
                self::doRemove(iterator_to_array($filesystemIterator, true), true);

                if (!self::box('rmdir', $file) && file_exists($file) && !$isRecursive) {
                    $lastError = self::$lastError;

                    if (null !== $origFile && self::box('rename', $file$origFile)) {
                        $file = $origFile;
                    }

                    throw new IOException(sprintf('Failed to remove directory "%s": ', $file).$lastError);
                }
            }
/** * 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(). */
  #[\ReturnTypeWillChange]   public function accept() {
    /** @var \SplFileInfo $file_info */
    $file_info = $this->getInnerIterator()->current();
    return $file_info->isFile() && preg_match($this->getRegex()$file_info->getFilename());
  }

}
/** * {@inheritdoc} */
  public function garbageCollection() {
    $flags = \FilesystemIterator::CURRENT_AS_FILEINFO;
    $flags += \FilesystemIterator::SKIP_DOTS;

    foreach ($this->listAll() as $name) {
      $directory = $this->getContainingDirectoryFullPath($name);
      try {
        $dir_iterator = new \FilesystemIterator($directory$flags);
      }
      catch (\UnexpectedValueException $e) {
        // FilesystemIterator throws an UnexpectedValueException if the         // specified path is not a directory, or if it is not accessible.         continue;
      }

      $directory_unlink = TRUE;
      $directory_mtime = filemtime($directory);
      foreach ($dir_iterator as $fileinfo) {
        if ($directory_mtime > $fileinfo->getMTime()) {
          
/** * A data provider that lists every theme in core. * * Also adds a deprecated theme with config. * * @return string[][] * An array of theme names to test, with both key and value being the name * of the theme. */
  public function themeListDataProvider() {
    $prefix = dirname(__DIR__, 4) . DIRECTORY_SEPARATOR . 'themes';
    $theme_dirs = array_keys(iterator_to_array(new \FilesystemIterator($prefix)));
    $theme_names = array_map(function D$path) use ($prefix) {
      return str_replace($prefix . DIRECTORY_SEPARATOR, '', $path);
    }$theme_dirs);
    $themes_keyed = array_combine($theme_names$theme_names);

    // Engines is not a theme.     unset($themes_keyed['engines']);

    // Add a deprecated theme with config.     $themes_keyed['test_deprecated_theme'] = 'test_deprecated_theme';

    

trait FileSystemModuleDiscoveryDataProviderTrait {

  /** * A data provider that lists every module in core. * * @return array * An array of module names to test. */
  public function coreModuleListDataProvider() {
    $prefix = dirname(__DIR__, 3) . DIRECTORY_SEPARATOR . 'modules';
    $module_dirs = array_keys(iterator_to_array(new \FilesystemIterator($prefix)));
    $module_names = array_map(function D$path) use ($prefix) {
      return str_replace($prefix . DIRECTORY_SEPARATOR, '', $path);
    }$module_dirs);
    $modules_keyed = array_combine($module_names$module_names);

    $data = array_map(function D$module) {
      return [$module];
    }$modules_keyed);

    return $data;
  }

}
Home | Imprint | This part of the site doesn't use cookies.