Skip to content

Commit

Permalink
[feature/interface-attributes] Added read attributes on interface
Browse files Browse the repository at this point in the history
  • Loading branch information
Alxey Shapilov committed Nov 1, 2023
1 parent aa45e59 commit 973dec9
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/Internal/AttributeReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ public function getClassMetadata(\ReflectionClass $class, string $name = null):
yield $this->instantiator->instantiate($attribute, $arguments, $class);
}

foreach ($class->getInterfaces() as $interface) {
yield from $this->getClassMetadata($interface, $name);
}

foreach ($class->getTraits() as $trait) {
yield from $this->getClassMetadata($trait, $name);
}
Expand Down
4 changes: 4 additions & 0 deletions src/Internal/DoctrineAnnotationReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ public function getClassMetadata(\ReflectionClass $class, string $name = null):

yield from $this->filter($name, $result);

foreach ($class->getInterfaces() as $interface) {
yield from $this->getClassMetadata($interface, $name);
}

foreach ($class->getTraits() as $trait) {
yield from $this->getClassMetadata($trait, $name);
}
Expand Down
12 changes: 12 additions & 0 deletions tests/Reader/AttributeReaderTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
use Spiral\Attributes\AttributeReader;
use Spiral\Attributes\Exception\SemanticAttributeException;
use Spiral\Attributes\ReaderInterface;
use Spiral\Tests\Attributes\Reader\Fixture\AnnotatedClass;
use Spiral\Tests\Attributes\Reader\Fixture\ClassWithAnnotatedInterface;
use Spiral\Tests\Attributes\Reader\Fixture\ClassWithAnnotatedTrait;
use Spiral\Tests\Attributes\Reader\Fixture\UndefinedMeta;

/**
Expand All @@ -27,6 +30,15 @@ protected function getReader(): ReaderInterface
return new AttributeReader();
}

public function testClassMetadataCount(): void
{
parent::testClassMetadataCount();

$this->assertCount($this->classMetadataCount,
$this->getClassMetadata(ClassWithAnnotatedInterface::class)
);
}

public function testUndefinedClassMeta(): void
{
$this->expectException(SemanticAttributeException::class);
Expand Down
10 changes: 10 additions & 0 deletions tests/Reader/DoctrineReaderTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Spiral\Attributes\AnnotationReader;
use Spiral\Attributes\ReaderInterface;
use Spiral\Tests\Attributes\Reader\Fixture\ClassWithAnnotatedInterface;

/**
* Doctrine reader does not support:
Expand All @@ -35,4 +36,13 @@ protected function getReader(): ReaderInterface
{
return new AnnotationReader();
}

public function testClassMetadataCount(): void
{
parent::testClassMetadataCount();

$this->assertCount($this->classMetadataCount,
$this->getClassMetadata(ClassWithAnnotatedInterface::class)
);
}
}
20 changes: 20 additions & 0 deletions tests/Reader/Fixture/AnnotatedInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

/**
* This file is part of Spiral Framework package.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Spiral\Tests\Attributes\Reader\Fixture;

use Spiral\Tests\Attributes\Reader\Fixture\Annotation\ClassAnnotation;

/** @ClassAnnotation(field="value") */
#[ClassAnnotation(field: 'value')]
interface AnnotatedInterface
{
}
16 changes: 16 additions & 0 deletions tests/Reader/Fixture/ClassWithAnnotatedInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

/**
* This file is part of Spiral Framework package.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Spiral\Tests\Attributes\Reader\Fixture;

class ClassWithAnnotatedInterface implements AnnotatedInterface
{
}
2 changes: 0 additions & 2 deletions tests/Reader/ReaderTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

namespace Spiral\Tests\Attributes\Reader;

use Spiral\Attributes\Exception\SemanticAttributeException;
use Spiral\Tests\Attributes\Concerns\InteractWithMetadata;
use Spiral\Tests\Attributes\Reader\Fixture\AnnotatedClass;
use Spiral\Tests\Attributes\Reader\Fixture\Annotation\ClassAnnotation;
Expand All @@ -22,7 +21,6 @@
use Spiral\Tests\Attributes\Reader\Fixture\Annotation\MethodParameterAnnotation;
use Spiral\Tests\Attributes\Reader\Fixture\Annotation\PropertyAnnotation;
use Spiral\Tests\Attributes\Reader\Fixture\ClassWithAnnotatedTrait;
use Spiral\Tests\Attributes\Reader\Fixture\UndefinedMeta;
use Spiral\Tests\Attributes\TestCase;

/**
Expand Down

0 comments on commit 973dec9

Please sign in to comment.