function DocParser::collectAnnotationMetadata

Same name in other branches
  1. 9 core/lib/Drupal/Component/Annotation/Doctrine/DocParser.php \Drupal\Component\Annotation\Doctrine\DocParser::collectAnnotationMetadata()
  2. 8.9.x core/lib/Drupal/Component/Annotation/Doctrine/DocParser.php \Drupal\Component\Annotation\Doctrine\DocParser::collectAnnotationMetadata()
  3. 11.x core/lib/Drupal/Component/Annotation/Doctrine/DocParser.php \Drupal\Component\Annotation\Doctrine\DocParser::collectAnnotationMetadata()

Collects parsing metadata for a given annotation class

Parameters

string $name The annotation name:

Return value

void

1 call to DocParser::collectAnnotationMetadata()
DocParser::Annotation in core/lib/Drupal/Component/Annotation/Doctrine/DocParser.php
Annotation ::= "@" AnnotationName MethodCall AnnotationName ::= QualifiedName | SimpleName QualifiedName ::= NameSpacePart "\" {NameSpacePart "\"}* SimpleName NameSpacePart ::= identifier | null | false |…

File

core/lib/Drupal/Component/Annotation/Doctrine/DocParser.php, line 461

Class

DocParser
A parser for docblock annotations.

Namespace

Drupal\Component\Annotation\Doctrine

Code

private function collectAnnotationMetadata($name) {
    if (self::$metadataParser === null) {
        self::$metadataParser = new self();
        self::$metadataParser->setIgnoreNotImportedAnnotations(true);
        self::$metadataParser->setIgnoredAnnotationNames($this->ignoredAnnotationNames);
        self::$metadataParser->setImports(array(
            'enum' => 'Doctrine\\Common\\Annotations\\Annotation\\Enum',
            'target' => 'Doctrine\\Common\\Annotations\\Annotation\\Target',
            'attribute' => 'Doctrine\\Common\\Annotations\\Annotation\\Attribute',
            'attributes' => 'Doctrine\\Common\\Annotations\\Annotation\\Attributes',
        ));
    }
    $class = new \ReflectionClass($name);
    $docComment = $class->getDocComment();
    // Sets default values for annotation metadata
    $metadata = array(
        'default_property' => null,
        'has_constructor' => null !== ($constructor = $class->getConstructor()) && $constructor->getNumberOfParameters() > 0,
        'properties' => array(),
        'property_types' => array(),
        'attribute_types' => array(),
        'targets_literal' => null,
        'targets' => Target::TARGET_ALL,
        'is_annotation' => str_contains($docComment, '@Annotation'),
    );
    // verify that the class is really meant to be an annotation
    if ($metadata['is_annotation']) {
        self::$metadataParser->setTarget(Target::TARGET_CLASS);
        foreach (self::$metadataParser->parse($docComment, 'class @' . $name) as $annotation) {
            if ($annotation instanceof Target) {
                $metadata['targets'] = $annotation->targets;
                $metadata['targets_literal'] = $annotation->literal;
                continue;
            }
            if ($annotation instanceof Attributes) {
                foreach ($annotation->value as $attribute) {
                    $this->collectAttributeTypeMetadata($metadata, $attribute);
                }
            }
        }
        // if not has a constructor will inject values into public properties
        if (false === $metadata['has_constructor']) {
            // collect all public properties
            foreach ($class->getProperties(\ReflectionProperty::IS_PUBLIC) as $property) {
                $metadata['properties'][$property->name] = $property->name;
                if (false === ($propertyComment = $property->getDocComment())) {
                    continue;
                }
                $attribute = new Attribute();
                $attribute->required = str_contains($propertyComment, '@Required');
                $attribute->name = $property->name;
                $attribute->type = str_contains($propertyComment, '@var') && preg_match('/@var\\s+([^\\s]+)/', $propertyComment, $matches) ? $matches[1] : 'mixed';
                $this->collectAttributeTypeMetadata($metadata, $attribute);
                // checks if the property has @Enum
                if (str_contains($propertyComment, '@Enum')) {
                    $context = 'property ' . $class->name . "::\$" . $property->name;
                    self::$metadataParser->setTarget(Target::TARGET_PROPERTY);
                    foreach (self::$metadataParser->parse($propertyComment, $context) as $annotation) {
                        if (!$annotation instanceof Enum) {
                            continue;
                        }
                        $metadata['enum'][$property->name]['value'] = $annotation->value;
                        $metadata['enum'][$property->name]['literal'] = !empty($annotation->literal) ? $annotation->literal : $annotation->value;
                    }
                }
            }
            // choose the first property as default property
            $metadata['default_property'] = reset($metadata['properties']);
        }
    }
    self::$annotationMetadata[$name] = $metadata;
}

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