Skip to content

Commit ac4813b

Browse files
Encapsulated the code for reading aspect meta properties. (#3944)
* Format code * Update CHANGELOG-2.2.md * Added test cases. Co-authored-by: 李铭昕 <[email protected]>
1 parent 4b99ecd commit ac4813b

File tree

7 files changed

+118
-34
lines changed

7 files changed

+118
-34
lines changed

src/Annotation/Aspect.php

+1-16
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
namespace Hyperf\Di\Annotation;
1313

1414
use Attribute;
15-
use Hyperf\Di\ReflectionManager;
16-
use ReflectionProperty;
1715

1816
/**
1917
* @Annotation
@@ -45,20 +43,7 @@ public function collectClass(string $className): void
4543

4644
protected function collect(string $className)
4745
{
48-
// Create the aspect instance without invoking their constructor.
49-
$reflectionClass = ReflectionManager::reflectClass($className);
50-
$properties = $reflectionClass->getProperties(ReflectionProperty::IS_PUBLIC);
51-
$instanceClasses = $instanceAnnotations = [];
52-
$instancePriority = null;
53-
foreach ($properties as $property) {
54-
if ($property->getName() === 'classes') {
55-
$instanceClasses = ReflectionManager::getPropertyDefaultValue($property);
56-
} elseif ($property->getName() === 'annotations') {
57-
$instanceAnnotations = ReflectionManager::getPropertyDefaultValue($property);
58-
} elseif ($property->getName() === 'priority') {
59-
$instancePriority = ReflectionManager::getPropertyDefaultValue($property);
60-
}
61-
}
46+
[$instanceClasses, $instanceAnnotations, $instancePriority] = AspectLoader::load($className);
6247

6348
// Classes
6449
$classes = $this->classes;

src/Annotation/AspectLoader.php

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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 Hyperf\Di\Annotation;
13+
14+
use Hyperf\Di\ReflectionManager;
15+
use ReflectionProperty;
16+
17+
class AspectLoader
18+
{
19+
/**
20+
* Load classes annotations and priority from aspect without invoking their constructor.
21+
*/
22+
public static function load(string $className): array
23+
{
24+
$reflectionClass = ReflectionManager::reflectClass($className);
25+
$properties = $reflectionClass->getProperties(ReflectionProperty::IS_PUBLIC);
26+
$instanceClasses = $instanceAnnotations = [];
27+
$instancePriority = null;
28+
foreach ($properties as $property) {
29+
if ($property->getName() === 'classes') {
30+
$instanceClasses = ReflectionManager::getPropertyDefaultValue($property);
31+
} elseif ($property->getName() === 'annotations') {
32+
$instanceAnnotations = ReflectionManager::getPropertyDefaultValue($property);
33+
} elseif ($property->getName() === 'priority') {
34+
$instancePriority = ReflectionManager::getPropertyDefaultValue($property);
35+
}
36+
}
37+
38+
return [$instanceClasses, $instanceAnnotations, $instancePriority];
39+
}
40+
}

src/Annotation/Scanner.php

+1-15
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
use Hyperf\Di\ReflectionManager;
2121
use Hyperf\Utils\Filesystem\Filesystem;
2222
use ReflectionClass;
23-
use ReflectionProperty;
2423

2524
class Scanner
2625
{
@@ -302,20 +301,7 @@ protected function loadAspects(int $lastCacheModified): void
302301
continue;
303302
}
304303

305-
// Create the aspect instance without invoking their constructor.
306-
$reflectionClass = ReflectionManager::reflectClass($aspect);
307-
$properties = $reflectionClass->getProperties(ReflectionProperty::IS_PUBLIC);
308-
$instanceClasses = $instanceAnnotations = [];
309-
$instancePriority = null;
310-
foreach ($properties as $property) {
311-
if ($property->getName() === 'classes') {
312-
$instanceClasses = ReflectionManager::getPropertyDefaultValue($property);
313-
} elseif ($property->getName() === 'annotations') {
314-
$instanceAnnotations = ReflectionManager::getPropertyDefaultValue($property);
315-
} elseif ($property->getName() === 'priority') {
316-
$instancePriority = ReflectionManager::getPropertyDefaultValue($property);
317-
}
318-
}
304+
[$instanceClasses, $instanceAnnotations, $instancePriority] = AspectLoader::load($aspect);
319305

320306
$classes = $instanceClasses ?: [];
321307
// Annotations

src/Definition/DefinitionSource.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ private function getParametersDefinition(ReflectionFunctionAbstract $constructor
7575
}
7676

7777
$parameterType = $parameter->getType();
78-
if ($parameterType && $parameterType instanceof \ReflectionNamedType && ! $parameterType->isBuiltin()) {
78+
if ($parameterType instanceof \ReflectionNamedType && ! $parameterType->isBuiltin()) {
7979
$parameters[$index] = new Reference($parameterType->getName());
8080
}
8181
}

src/MetadataCollector.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,7 @@ public static function serialize(): string
7171
*/
7272
public static function deserialize(string $metadata): bool
7373
{
74-
$data = unserialize($metadata);
75-
static::$container = $data;
74+
static::$container = unserialize($metadata);
7675
return true;
7776
}
7877

tests/Annotation/AspectLoaderTest.php

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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\Annotation;
13+
14+
use Hyperf\Di\Annotation\AspectLoader;
15+
use Hyperf\Di\Annotation\Inject;
16+
use HyperfTest\Di\Stub\Aspect\Debug1Aspect;
17+
use HyperfTest\Di\Stub\Aspect\DebugLoaderAspect;
18+
use PHPUnit\Framework\TestCase;
19+
20+
/**
21+
* @internal
22+
* @coversNothing
23+
*/
24+
class AspectLoaderTest extends TestCase
25+
{
26+
public function testLoad()
27+
{
28+
[$classes, $annotations, $priority] = AspectLoader::load(Debug1Aspect::class);
29+
30+
$this->assertSame(['Debug1AspectFoo'], $classes);
31+
$this->assertSame([], $annotations);
32+
$this->assertSame(null, $priority);
33+
34+
[$classes, $annotations, $priority] = AspectLoader::load(DebugLoaderAspect::class);
35+
36+
$this->assertSame(['Debug1AspectFoo'], $classes);
37+
$this->assertSame([Inject::class], $annotations);
38+
$this->assertSame(100, $priority);
39+
}
40+
}
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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\Aspect;
13+
14+
use Hyperf\Di\Annotation\Inject;
15+
use Hyperf\Di\Aop\AbstractAspect;
16+
use Hyperf\Di\Aop\ProceedingJoinPoint;
17+
18+
class DebugLoaderAspect extends AbstractAspect
19+
{
20+
public $classes = [
21+
'Debug1AspectFoo',
22+
];
23+
24+
public $annotations = [
25+
Inject::class,
26+
];
27+
28+
public $priority = 100;
29+
30+
public function process(ProceedingJoinPoint $proceedingJoinPoint)
31+
{
32+
return $proceedingJoinPoint->process();
33+
}
34+
}

0 commit comments

Comments
 (0)