function AttributeClassDiscovery::getDefinitions

Same name and namespace in other branches
  1. 11.x core/lib/Drupal/Component/Plugin/Discovery/AttributeClassDiscovery.php \Drupal\Component\Plugin\Discovery\AttributeClassDiscovery::getDefinitions()
  2. 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 74

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.
                $dependencies = isset($cached['dependencies']) ? unserialize($cached['dependencies']) : [];
                if (!$this->hasMissingDependencies($dependencies ?? [])) {
                  $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');
            // Plugins may rely on classes, interfaces, or traits defined by
            // modules that are not installed. In such a case, an error may
            // be thrown from reflection. However, this is an unavoidable
            // situation with optional dependencies and plugins. Therefore,
            // silently skip over this class and avoid writing to the cache,
            // so that it is scanned each time. This ensures that the plugin
            // definition will be found if the module it requires is enabled.
            try {
              if (!class_exists($class)) {
                continue;
              }
            } catch (\Error) {
              continue;
            }
            $result = $this->parseClass($class, $fileinfo);
            ['id' => $id, 'content' => $content] = $result;
            if ($id) {
              if (!$this->hasMissingDependencies($result['dependencies'] ?? [])) {
                $definitions[$id] = $content;
              }
              // Explicitly serialize this to create a new object instance.
              $this->fileCache
                ->set($fileinfo->getPathName(), [
                'id' => $id,
                'content' => serialize($content),
                'dependencies' => serialize($result['dependencies'] ?? NULL),
              ]);
            }
            else {
              // Store a NULL object, so that the file is not parsed again.
              $this->fileCache
                ->set($fileinfo->getPathName(), [
                NULL,
              ]);
            }
          }
        }
      }
    }
  }
  return $definitions;
}

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