function AttributeClassDiscovery::parseClass

Same name and namespace in other branches
  1. 10 core/lib/Drupal/Component/Plugin/Discovery/AttributeClassDiscovery.php \Drupal\Component\Plugin\Discovery\AttributeClassDiscovery::parseClass()

Parses attributes from a class.

Parameters

class-string $class: The class to parse.

\SplFileInfo $fileinfo: The SPL file information for the class.

Return value

array An array with the keys 'id' and 'content'. The 'id' is the plugin ID and 'content' is the plugin definition.

Throws

\ReflectionException

2 calls to AttributeClassDiscovery::parseClass()
AttributeClassDiscovery::getDefinitions in core/lib/Drupal/Component/Plugin/Discovery/AttributeClassDiscovery.php
Gets the definition of all plugins for this type.
AttributeDiscoveryWithAnnotations::parseClass in core/lib/Drupal/Core/Plugin/Discovery/AttributeDiscoveryWithAnnotations.php
Parses attributes from a class.
1 method overrides AttributeClassDiscovery::parseClass()
AttributeDiscoveryWithAnnotations::parseClass in core/lib/Drupal/Core/Plugin/Discovery/AttributeDiscoveryWithAnnotations.php
Parses attributes from a class.

File

core/lib/Drupal/Component/Plugin/Discovery/AttributeClassDiscovery.php, line 122

Class

AttributeClassDiscovery
Defines a discovery mechanism to find plugins with attributes.

Namespace

Drupal\Component\Plugin\Discovery

Code

protected function parseClass(string $class, \SplFileInfo $fileinfo) : array {
    // @todo Consider performance improvements over using reflection.
    // @see https://www.drupal.org/project/drupal/issues/3395260.
    $reflection_class = new \ReflectionClass($class);
    $id = $content = NULL;
    if ($attributes = $reflection_class->getAttributes($this->pluginDefinitionAttributeName, \ReflectionAttribute::IS_INSTANCEOF)) {
        
        /** @var \Drupal\Component\Plugin\Attribute\AttributeInterface $attribute */
        $attribute = $attributes[0]->newInstance();
        $this->prepareAttributeDefinition($attribute, $class);
        $id = $attribute->getId();
        $content = $attribute->get();
    }
    return [
        'id' => $id,
        'content' => $content,
    ];
}

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