function AttributeDiscoveryWithAnnotations::parseClass

Same name in other branches
  1. 11.x core/lib/Drupal/Core/Plugin/Discovery/AttributeDiscoveryWithAnnotations.php \Drupal\Core\Plugin\Discovery\AttributeDiscoveryWithAnnotations::parseClass()

Overrides AttributeClassDiscovery::parseClass

File

core/lib/Drupal/Core/Plugin/Discovery/AttributeDiscoveryWithAnnotations.php, line 80

Class

AttributeDiscoveryWithAnnotations
Enables both attribute and annotation discovery for plugin definitions.

Namespace

Drupal\Core\Plugin\Discovery

Code

protected function parseClass(string $class, \SplFileInfo $fileinfo) : array {
    // 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);
    $reflection_class = $parser->getReflectionClass();
    // @todo Handle deprecating definitions discovery via annotations in
    // https://www.drupal.org/project/drupal/issues/3265945.
    
    /** @var \Drupal\Component\Annotation\AnnotationInterface $annotation */
    if ($annotation = $this->getAnnotationReader()
        ->getClassAnnotation($reflection_class, $this->pluginDefinitionAnnotationName)) {
        $this->prepareAnnotationDefinition($annotation, $class);
        return [
            'id' => $annotation->getId(),
            'content' => $annotation->get(),
        ];
    }
    // Annotations use static reflection and are able to analyze a class that
    // extends classes or uses traits that do not exist. Attribute discovery
    // will trigger a fatal error with such classes, so only call it if the
    // class has a class attribute.
    if ($reflection_class->hasClassAttribute($this->pluginDefinitionAttributeName)) {
        return parent::parseClass($class, $fileinfo);
    }
    return [
        'id' => NULL,
        'content' => NULL,
    ];
}

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