You are here

public function AnnotatedClassDiscovery::getDefinitions in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/lib/Drupal/Component/Annotation/Plugin/Discovery/AnnotatedClassDiscovery.php \Drupal\Component\Annotation\Plugin\Discovery\AnnotatedClassDiscovery::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 DiscoveryTrait::getDefinitions

File

core/lib/Drupal/Component/Annotation/Plugin/Discovery/AnnotatedClassDiscovery.php, line 84
Contains \Drupal\Component\Annotation\Plugin\Discovery\AnnotatedClassDiscovery.

Class

AnnotatedClassDiscovery
Defines a discovery mechanism to find annotated plugins in PSR-0 namespaces.

Namespace

Drupal\Component\Annotation\Plugin\Discovery

Code

public function getDefinitions() {
  $definitions = array();
  $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-0 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') {
            $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 StaticReflectionParser($class, $finder, TRUE);

            /** @var $annotation \Drupal\Component\Annotation\AnnotationInterface */
            if ($annotation = $reader
              ->getClassAnnotation($parser
              ->getReflectionClass(), $this->pluginDefinitionAnnotationName)) {
              $this
                ->prepareAnnotationDefinition($annotation, $class);
              $definitions[$annotation
                ->getId()] = $annotation
                ->get();
            }
          }
        }
      }
    }
  }

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