function AttributeClassDiscovery::getDefinitions

Same name and namespace in other branches
  1. 10 core/lib/Drupal/Component/Plugin/Discovery/AttributeClassDiscovery.php \Drupal\Component\Plugin\Discovery\AttributeClassDiscovery::getDefinitions()

Overrides DiscoveryTrait::getDefinitions

1 call to AttributeClassDiscovery::getDefinitions()
AttributeDiscoveryWithAnnotations::getDefinitions in core/lib/Drupal/Core/Plugin/Discovery/AttributeDiscoveryWithAnnotations.php
Gets the definition of all plugins for this type.
1 method overrides AttributeClassDiscovery::getDefinitions()
AttributeDiscoveryWithAnnotations::getDefinitions in core/lib/Drupal/Core/Plugin/Discovery/AttributeDiscoveryWithAnnotations.php
Gets the definition of all plugins for this type.

File

core/lib/Drupal/Component/Plugin/Discovery/AttributeClassDiscovery.php, line 59

Class

AttributeClassDiscovery
Defines a discovery mechanism to find plugins with attributes.

Namespace

Drupal\Component\Plugin\Discovery

Code

public function getDefinitions() {
    $definitions = [];
    // Search for classes within all PSR-4 namespace locations.
    foreach ($this->getPluginNamespaces() as $namespace => $dirs) {
        foreach ($dirs as $dir) {
            if (file_exists($dir)) {
                $iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($dir, \RecursiveDirectoryIterator::SKIP_DOTS));
                foreach ($iterator as $fileinfo) {
                    assert($fileinfo instanceof \SplFileInfo);
                    if ($fileinfo->getExtension() === 'php') {
                        if ($cached = $this->fileCache
                            ->get($fileinfo->getPathName())) {
                            if (isset($cached['id'])) {
                                // Explicitly unserialize this to create a new object instance.
                                $definitions[$cached['id']] = unserialize($cached['content']);
                            }
                            continue;
                        }
                        $sub_path = $iterator->getSubIterator()
                            ->getSubPath();
                        $sub_path = $sub_path ? str_replace(DIRECTORY_SEPARATOR, '\\', $sub_path) . '\\' : '';
                        $class = $namespace . '\\' . $sub_path . $fileinfo->getBasename('.php');
                        [
                            'id' => $id,
                            'content' => $content,
                        ] = $this->parseClass($class, $fileinfo);
                        if ($id) {
                            $definitions[$id] = $content;
                            // Explicitly serialize this to create a new object instance.
                            $this->fileCache
                                ->set($fileinfo->getPathName(), [
                                'id' => $id,
                                'content' => serialize($content),
                            ]);
                        }
                        else {
                            // Store a NULL object, so the file is not parsed again.
                            $this->fileCache
                                ->set($fileinfo->getPathName(), [
                                NULL,
                            ]);
                        }
                    }
                }
            }
        }
    }
    // Plugin discovery is a memory expensive process due to reflection and the
    // number of files involved. Collect cycles at the end of discovery to be as
    // efficient as possible.
    gc_collect_cycles();
    return $definitions;
}

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