public function AnnotatedClassDiscovery::getDefinitions

Implements Drupal\Component\Plugin\Discovery\DiscoveryInterface::getDefinitions().

Overrides DiscoveryInterface::getDefinitions

2 calls to AnnotatedClassDiscovery::getDefinitions()
1 method overrides AnnotatedClassDiscovery::getDefinitions()

File

drupal/core/lib/Drupal/Component/Plugin/Discovery/AnnotatedClassDiscovery.php, line 76
Contains Drupal\Component\Plugin\Discovery\AnnotatedClassDiscovery.

Class

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

Namespace

Drupal\Component\Plugin\Discovery

Code

public function getDefinitions() {
  $definitions = array();
  $reader = new AnnotationReader();

  // Prevent @endlink from being parsed as an annotation.
  $reader
    ->addGlobalIgnoredName('endlink');

  // Register the namespaces of classes that can be used for annotations.
  AnnotationRegistry::registerAutoloadNamespaces($this
    ->getAnnotationNamespaces());

  // Search for classes within all PSR-0 namespace locations.
  foreach ($this
    ->getPluginNamespaces() as $namespace => $dirs) {
    foreach ($dirs as $dir) {
      $dir .= DIRECTORY_SEPARATOR . str_replace('\\', DIRECTORY_SEPARATOR, $namespace);
      if (file_exists($dir)) {
        foreach (new DirectoryIterator($dir) as $fileinfo) {

          // @todo Once core requires 5.3.6, use $fileinfo->getExtension().
          if (pathinfo($fileinfo
            ->getFilename(), PATHINFO_EXTENSION) == 'php') {
            $class = $namespace . '\\' . $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);
            if ($annotation = $reader
              ->getClassAnnotation($parser
              ->getReflectionClass(), $this->pluginDefinitionAnnotationName)) {

              // AnnotationInterface::get() returns the array definition
              // instead of requiring us to work with the annotation object.
              $definition = $annotation
                ->get();
              $definition['class'] = $class;
              $definitions[$definition['id']] = $definition;
            }
          }
        }
      }
    }
  }
  return $definitions;
}