Skip to content

Commit 108ad90

Browse files
authored
Support ignoreAnnotations for Annotation Reader. (#4880)
1 parent f63246d commit 108ad90

File tree

4 files changed

+46
-7
lines changed

4 files changed

+46
-7
lines changed

src/Annotation/AnnotationReader.php

+7
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
*/
2222
class AnnotationReader
2323
{
24+
public function __construct(protected array $ignoreAnnotations = [])
25+
{
26+
}
27+
2428
public function getClassAnnotations(ReflectionClass $class)
2529
{
2630
return $this->getAttributes($class);
@@ -83,6 +87,9 @@ public function getAttributes(\Reflector $reflection): array
8387
}
8488
$attributes = $reflection->getAttributes();
8589
foreach ($attributes as $attribute) {
90+
if (in_array($attribute->getName(), $this->ignoreAnnotations, true)) {
91+
continue;
92+
}
8693
if (! class_exists($attribute->getName())) {
8794
$className = $methodName = $propertyName = '';
8895
if ($reflection instanceof ReflectionClass) {

src/Annotation/Scanner.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public function scan(array $classMap = [], string $proxyDir = ''): array
9898

9999
$this->deserializeCachedScanData($collectors);
100100

101-
$annotationReader = new AnnotationReader();
101+
$annotationReader = new AnnotationReader($this->scanConfig->getIgnoreAnnotations());
102102

103103
$paths = $this->normalizeDir($paths);
104104

tests/AnnotationReaderTest.php

+18-6
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
namespace HyperfTest\Di;
1313

1414
use Hyperf\Di\Annotation\AnnotationReader;
15+
use HyperfTest\Di\Stub\FooWithNotExistAnnotation;
16+
use HyperfTest\Di\Stub\IgnoreDemoAnnotation;
1517
use HyperfTest\Di\Stub\NotFoundAttributeTarget;
1618
use PHPUnit\Framework\TestCase;
1719
use ReflectionClass;
@@ -41,9 +43,6 @@ public function testGetNotFoundAttributesOfClass()
4143
}
4244
}
4345

44-
/**
45-
* @requires PHP 8.0
46-
*/
4746
public function testGetNotFoundAttributesOfMethod()
4847
{
4948
$reflectionClass = new ReflectionClass(NotFoundAttributeTarget::class);
@@ -64,9 +63,6 @@ public function testGetNotFoundAttributesOfMethod()
6463
}
6564
}
6665

67-
/**
68-
* @requires PHP 8.0
69-
*/
7066
public function testGetNotFoundAttributesOfProperty()
7167
{
7268
$reflectionClass = new ReflectionClass(NotFoundAttributeTarget::class);
@@ -86,4 +82,20 @@ public function testGetNotFoundAttributesOfProperty()
8682
$this->assertSame($exceptionMessage, $actual);
8783
}
8884
}
85+
86+
public function testIgnoreAnnotations()
87+
{
88+
$reader = new AnnotationReader(['NotExistAnnotation']);
89+
90+
$res = $reader->getClassAnnotations(new \ReflectionClass(FooWithNotExistAnnotation::class));
91+
92+
$this->assertSame(1, count($res));
93+
$this->assertInstanceOf(IgnoreDemoAnnotation::class, $res[0]);
94+
95+
$reader = new AnnotationReader(['NotExistAnnotation', IgnoreDemoAnnotation::class]);
96+
97+
$res = $reader->getClassAnnotations(new \ReflectionClass(FooWithNotExistAnnotation::class));
98+
99+
$this->assertSame([], $res);
100+
}
89101
}
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* This file is part of Hyperf.
6+
*
7+
* @link https://www.hyperf.io
8+
* @document https://hyperf.wiki
9+
* @contact [email protected]
10+
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
11+
*/
12+
namespace HyperfTest\Di\Stub;
13+
14+
use NotExistAnnotation;
15+
16+
#[NotExistAnnotation]
17+
#[IgnoreDemoAnnotation]
18+
class FooWithNotExistAnnotation
19+
{
20+
}

0 commit comments

Comments
 (0)