Skip to content

Commit b473a22

Browse files
authored
Optimized AnnotationJob to support call protected / private methods. (#7604)
1 parent 0a0d4b1 commit b473a22

File tree

3 files changed

+60
-12
lines changed

3 files changed

+60
-12
lines changed

src/AnnotationJob.php

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,10 @@
1818

1919
class AnnotationJob extends Job
2020
{
21-
public string $class;
22-
23-
public string $method;
24-
2521
public array $params = [];
2622

27-
public function __construct(string $class, string $method, array $params, int $maxAttempts = 0)
23+
public function __construct(public string $class, public string $method, array $params, public int $maxAttempts = 0)
2824
{
29-
$this->class = $class;
30-
$this->method = $method;
31-
$this->maxAttempts = $maxAttempts;
3225
foreach ($params as $key => $value) {
3326
if ($value instanceof CompressInterface) {
3427
$value = $value->compress();
@@ -40,10 +33,9 @@ public function __construct(string $class, string $method, array $params, int $m
4033
public function handle()
4134
{
4235
$container = ApplicationContext::getContainer();
43-
44-
$class = $container->get($this->class);
45-
36+
$instance = $container->get($this->class);
4637
$params = [];
38+
4739
foreach ($this->params as $key => $value) {
4840
if ($value instanceof UnCompressInterface) {
4941
$value = $value->uncompress();
@@ -53,6 +45,8 @@ public function handle()
5345

5446
$container->get(Environment::class)->setAsyncQueue(true);
5547

56-
$class->{$this->method}(...$params);
48+
$method = $this->method;
49+
50+
(fn () => $this->{$method}(...$params))->call($instance);
5751
}
5852
}

tests/AsyncQueueAspectTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,21 @@
2020
use Hyperf\AsyncQueue\Environment;
2121
use Hyperf\Context\ApplicationContext;
2222
use Hyperf\Context\Context;
23+
use Hyperf\Coroutine\Waiter;
2324
use Hyperf\Di\Annotation\AnnotationCollector;
2425
use Hyperf\Di\Annotation\Aspect;
2526
use Hyperf\Di\Aop\Ast;
2627
use Hyperf\Di\ReflectionManager;
2728
use HyperfTest\AsyncQueue\Stub\FooProxy;
29+
use HyperfTest\AsyncQueue\Stub\FooService;
2830
use Mockery;
2931
use PHPUnit\Framework\Attributes\CoversNothing;
3032
use PHPUnit\Framework\Attributes\Group;
3133
use PHPUnit\Framework\TestCase;
3234
use Psr\Container\ContainerInterface;
3335

36+
use function Hyperf\Coroutine\wait;
37+
3438
/**
3539
* @internal
3640
* @coversNothing
@@ -84,6 +88,28 @@ public function testAsyncMessageVariadic()
8488
$this->assertSame([$id, $uuid, $data], Context::get(FooProxy::class));
8589
}
8690

91+
public function testAnnotationJob()
92+
{
93+
$container = Mockery::mock(ContainerInterface::class);
94+
ApplicationContext::setContainer($container);
95+
$container->shouldReceive('get')->with(Waiter::class)->andReturn(new Waiter());
96+
97+
wait(function () use ($container) {
98+
$container->shouldReceive('get')->with(FooService::class)->andReturn(new FooService());
99+
$container->shouldReceive('get')->with(Environment::class)->andReturn(new Environment());
100+
101+
$job = new AnnotationJob(FooService::class, 'test', []);
102+
$job->handle();
103+
104+
$this->assertSame(1, Context::get(FooService::class . '::test'));
105+
106+
$job = new AnnotationJob(FooService::class, 'foo', []);
107+
$job->handle();
108+
109+
$this->assertSame(1, Context::get(FooService::class . '::foo'));
110+
});
111+
}
112+
87113
protected function getContainer()
88114
{
89115
$container = Mockery::mock(ContainerInterface::class);

tests/Stub/FooService.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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 group@hyperf.io
10+
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
11+
*/
12+
13+
namespace HyperfTest\AsyncQueue\Stub;
14+
15+
use Hyperf\Context\Context;
16+
17+
class FooService
18+
{
19+
public function test()
20+
{
21+
Context::set(self::class . '::test', 1);
22+
}
23+
24+
protected function foo()
25+
{
26+
Context::set(self::class . '::foo', 1);
27+
}
28+
}

0 commit comments

Comments
 (0)