W3cubDocs

/Drupal 8

protected function FileStorage::getAllCollectionNamesHelper

protected FileStorage::getAllCollectionNamesHelper($directory)

Helper function for getAllCollectionNames().

If the file storage has the following subdirectory structure: ./another_collection/one ./another_collection/two ./collection/sub/one ./collection/sub/two this function will return:

  array(
    'another_collection.one',
    'another_collection.two',
    'collection.sub.one',
    'collection.sub.two',
  );

Parameters

string $directory: The directory to check for sub directories. This allows this function to be used recursively to discover all the collections in the storage.

Return value

array A list of collection names contained within the provided directory.

File

core/lib/Drupal/Core/Config/FileStorage.php, line 287

Class

FileStorage
Defines the file storage.

Namespace

Drupal\Core\Config

Code

protected function getAllCollectionNamesHelper($directory) {
  $collections = array();
  $pattern = '/\.' . preg_quote($this->getFileExtension(), '/') . '$/';
  foreach (new \DirectoryIterator($directory) as $fileinfo) {
    if ($fileinfo->isDir() && !$fileinfo->isDot()) {
      $collection = $fileinfo->getFilename();
      // Recursively call getAllCollectionNamesHelper() to discover if there
      // are subdirectories. Subdirectories represent a dotted collection
      // name.
      $sub_collections = $this->getAllCollectionNamesHelper($directory . '/' . $collection);
      if (!empty($sub_collections)) {
        // Build up the collection name by concatenating the subdirectory
        // names with the current directory name.
        foreach ($sub_collections as $sub_collection) {
          $collections[] = $collection . '.' . $sub_collection;
        }
      }
      // Check that the collection is valid by searching it for configuration
      // objects. A directory without any configuration objects is not a valid
      // collection.
      // @see \Drupal\Core\Config\FileStorage::listAll()
      foreach (scandir($directory . '/' . $collection) as $file) {
        if ($file[0] !== '.' && preg_match($pattern, $file)) {
          $collections[] = $collection;
          break;
        }
      }
    }
  }
  return $collections;
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Config!FileStorage.php/function/FileStorage::getAllCollectionNamesHelper/8.1.x