Skip to content

Commit f8e837b

Browse files
committed
Merge branch 'master' into 2.1
2 parents b7101ae + 408d961 commit f8e837b

File tree

4 files changed

+79
-3
lines changed

4 files changed

+79
-3
lines changed

src/Aop/ProceedingJoinPoint.php

+7
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,11 @@ public function getReflectMethod(): \ReflectionMethod
108108
$this->methodName
109109
);
110110
}
111+
112+
public function getInstance(): ?object
113+
{
114+
$ref = new \ReflectionFunction($this->originalMethod);
115+
116+
return $ref->getClosureThis();
117+
}
111118
}

tests/ProxyTraitTest.php

+24
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Hyperf\Di\Annotation\AnnotationCollector;
1515
use Hyperf\Di\Annotation\AspectCollector;
1616
use Hyperf\Utils\ApplicationContext;
17+
use HyperfTest\Di\Stub\Aspect\GetNameAspect;
1718
use HyperfTest\Di\Stub\Aspect\IncrAspect;
1819
use HyperfTest\Di\Stub\Aspect\IncrAspectAnnotation;
1920
use HyperfTest\Di\Stub\ProxyTraitObject;
@@ -47,6 +48,29 @@ public function testGetParamsMap()
4748
$this->assertEquals(['id', 'str', 'num'], $obj->get3(1, 'hy')['order']);
4849
}
4950

51+
public function testProceedingJoinPointGetInstance()
52+
{
53+
$aspect = [];
54+
ApplicationContext::setContainer(value(function () use (&$aspect) {
55+
$container = Mockery::mock(ContainerInterface::class);
56+
$container->shouldReceive('get')->with(Mockery::any())->andReturnUsing(function ($class) use (&$aspect) {
57+
$aspect[] = $class;
58+
return new $class();
59+
});
60+
return $container;
61+
}));
62+
63+
$obj = new ProxyTraitObject();
64+
$this->assertSame('HyperfCloud', $obj->getName2());
65+
66+
AspectCollector::set('classes', [
67+
GetNameAspect::class => [ProxyTraitObject::class],
68+
]);
69+
70+
$obj = new ProxyTraitObject();
71+
$this->assertSame('Hyperf', $obj->getName());
72+
}
73+
5074
public function testHandleAround()
5175
{
5276
$aspect = [];

tests/Stub/Aspect/GetNameAspect.php

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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\Aop\AbstractAspect;
15+
use Hyperf\Di\Aop\ProceedingJoinPoint;
16+
17+
class GetNameAspect extends AbstractAspect
18+
{
19+
public function process(ProceedingJoinPoint $proceedingJoinPoint)
20+
{
21+
return $proceedingJoinPoint->getInstance()->name;
22+
}
23+
}

tests/Stub/ProxyTraitObject.php

+25-3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@ class ProxyTraitObject
1717
{
1818
use ProxyTrait;
1919

20+
/**
21+
* @var string
22+
*/
23+
public $name;
24+
25+
public function __construct(string $name = 'Hyperf')
26+
{
27+
$this->name = $name;
28+
}
29+
2030
public function get(?int $id, string $str = '')
2131
{
2232
return $this->__getParamsMap(static::class, 'get', func_get_args());
@@ -34,10 +44,22 @@ public function get3(?int $id = 1, string $str = '', float $num = 1.0)
3444

3545
public function incr()
3646
{
37-
$__function__ = __FUNCTION__;
38-
$__method__ = __METHOD__;
39-
return self::__proxyCall(ProxyTraitObject::class, __FUNCTION__, self::__getParamsMap(ProxyTraitObject::class, __FUNCTION__, func_get_args()), function () use ($__function__, $__method__) {
47+
return self::__proxyCall(ProxyTraitObject::class, __FUNCTION__, self::__getParamsMap(ProxyTraitObject::class, __FUNCTION__, func_get_args()), function () {
4048
return 1;
4149
});
4250
}
51+
52+
public function getName()
53+
{
54+
return self::__proxyCall(ProxyTraitObject::class, __FUNCTION__, self::__getParamsMap(ProxyTraitObject::class, __FUNCTION__, func_get_args()), function () {
55+
return 'HyperfCloud';
56+
});
57+
}
58+
59+
public function getName2()
60+
{
61+
return self::__proxyCall(ProxyTraitObject::class, __FUNCTION__, self::__getParamsMap(ProxyTraitObject::class, __FUNCTION__, func_get_args()), function () {
62+
return 'HyperfCloud';
63+
});
64+
}
4365
}

0 commit comments

Comments
 (0)