function FileStorage::getAllCollectionNamesHelper

Same name in other branches
  1. 9 core/lib/Drupal/Core/Config/FileStorage.php \Drupal\Core\Config\FileStorage::getAllCollectionNamesHelper()
  2. 8.9.x core/lib/Drupal/Core/Config/FileStorage.php \Drupal\Core\Config\FileStorage::getAllCollectionNamesHelper()
  3. 10 core/lib/Drupal/Core/Config/FileStorage.php \Drupal\Core\Config\FileStorage::getAllCollectionNamesHelper()

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:

[
    '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. It is the responsibility of the caller to ensure the directory exists.

Return value

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

1 call to FileStorage::getAllCollectionNamesHelper()
FileStorage::getAllCollectionNames in core/lib/Drupal/Core/Config/FileStorage.php
Gets the existing collections.

File

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

Class

FileStorage
Defines the file storage.

Namespace

Drupal\Core\Config

Code

protected function getAllCollectionNamesHelper($directory) {
    $collections = [];
    $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;
}

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