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', 'content', and 'dependencies'. The 'id' is the plugin ID, 'content' is the plugin definition, and 'dependencies' is a list of class, interface or trait names in the plugin class hierarchy.

Throws

\ReflectionException

\Error

3 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.
AttributeDiscoveryWithAnnotationsAutomatedProviders::parseClass in core/modules/migrate/src/Plugin/Discovery/AttributeDiscoveryWithAnnotationsAutomatedProviders.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 233

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);
    if ($dependencies = $this->getClassDependencies($reflection_class)) {
      // Include the dependencies in the plugin definition content in case
      // plugins need to know about them.
      $attribute->setDependencies($dependencies);
    }
    $id = $attribute->getId();
    $content = $attribute->get();
  }
  return [
    'id' => $id,
    'content' => $content,
    'dependencies' => $dependencies ?? NULL,
  ];
}

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