You are here

public function AnnotatedClassDiscoveryAutomatedProviders::getDefinitions in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/migrate/src/Plugin/Discovery/AnnotatedClassDiscoveryAutomatedProviders.php \Drupal\migrate\Plugin\Discovery\AnnotatedClassDiscoveryAutomatedProviders::getDefinitions()

Gets the definition of all plugins for this type.

Return value

mixed[] An array of plugin definitions (empty array if no definitions were found). Keys are plugin IDs.

Overrides AnnotatedClassDiscovery::getDefinitions

See also

\Drupal\Core\Plugin\FilteredPluginManagerInterface::getFilteredDefinitions()

File

core/modules/migrate/src/Plugin/Discovery/AnnotatedClassDiscoveryAutomatedProviders.php, line 74

Class

AnnotatedClassDiscoveryAutomatedProviders
Determines providers based on a class's and its parent's namespaces.

Namespace

Drupal\migrate\Plugin\Discovery

Code

public function getDefinitions() {
  $definitions = [];
  $reader = $this
    ->getAnnotationReader();

  // Clear the annotation loaders of any previous annotation classes.
  AnnotationRegistry::reset();

  // Register the namespaces of classes that can be used for annotations.
  AnnotationRegistry::registerLoader('class_exists');

  // 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) {
          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');

            // The filename is already known, so there is no need to find the
            // file. However, StaticReflectionParser needs a finder, so use a
            // mock version.
            $finder = MockFileFinder::create($fileinfo
              ->getPathName());
            $parser = new BaseStaticReflectionParser($class, $finder, FALSE);

            /** @var \Drupal\Component\Annotation\AnnotationInterface $annotation */
            if ($annotation = $reader
              ->getClassAnnotation($parser
              ->getReflectionClass(), $this->pluginDefinitionAnnotationName)) {
              $this
                ->prepareAnnotationDefinition($annotation, $class, $parser);
              $id = $annotation
                ->getId();
              $content = $annotation
                ->get();
              $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 reparsed again.
              $this->fileCache
                ->set($fileinfo
                ->getPathName(), [
                NULL,
              ]);
            }
          }
        }
      }
    }
  }

  // Don't let annotation loaders pile up.
  AnnotationRegistry::reset();
  return $definitions;
}