Skip to content

Commit 2761e03

Browse files
author
github-ci
committed
update
1 parent aef9e0c commit 2761e03

33 files changed

+1534
-39
lines changed

src/Aspect/ExceptionAspect.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class ExceptionAspect
1313
#[AfterThrowing(classAttribute: CatchException::class)]
1414
public function catchException(JoinPoint $joinPoint): void
1515
{
16-
if ($joinPoint->getException()) {
16+
if ($joinPoint->getException() !== null) {
1717
//dump($joinPoint->getException());
1818
}
1919
}

src/Aspect/StopwatchAspect.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ class StopwatchAspect
1515
{
1616
public function __construct(
1717
private readonly ?SComponent $stopwatch = null,
18-
)
19-
{
18+
) {
2019
$this->eventMap = new \WeakMap();
2120
}
2221

@@ -25,7 +24,7 @@ public function __construct(
2524
#[Before(methodAttribute: Stopwatch::class)]
2625
public function startEvent(JoinPoint $joinPoint): void
2726
{
28-
if (!$this->stopwatch) {
27+
if ($this->stopwatch === null) {
2928
return;
3029
}
3130
$event = $this->stopwatch->start($joinPoint->getMethod(), $joinPoint->getInternalServiceId());
@@ -35,7 +34,7 @@ public function startEvent(JoinPoint $joinPoint): void
3534
#[After(methodAttribute: Stopwatch::class)]
3635
public function stopEvent(JoinPoint $joinPoint): void
3736
{
38-
if (!$this->stopwatch) {
37+
if ($this->stopwatch === null) {
3938
return;
4039
}
4140
if (!$this->eventMap->offsetExists($joinPoint)) {

src/Attribute/Advice.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,21 @@ public function __construct(
1212
public ?array $serviceIds = null,
1313
public ?array $serviceTags = null,
1414
public ?array $parentClasses = null,
15-
)
16-
{
15+
) {
1716
if ($this->classAttribute !== null) {
18-
$this->statement = static::getClassAttributeStatement($this->classAttribute);
17+
$this->statement = self::getClassAttributeStatement($this->classAttribute);
1918
}
2019
if ($this->methodAttribute !== null) {
21-
$this->statement = static::getMethodAttributeStatement($this->methodAttribute);
20+
$this->statement = self::getMethodAttributeStatement($this->methodAttribute);
2221
}
2322
if ($this->serviceIds !== null) {
24-
$this->statement = static::getServiceIdStatement($this->serviceIds);
23+
$this->statement = self::getServiceIdStatement($this->serviceIds);
2524
}
2625
if ($this->serviceTags !== null) {
27-
$this->statement = static::getServiceTagStatement($this->serviceTags);
26+
$this->statement = self::getServiceTagStatement($this->serviceTags);
2827
}
2928
if ($this->parentClasses !== null) {
30-
$this->statement = static::getParentClassStatement($this->parentClasses);
29+
$this->statement = self::getParentClassStatement($this->parentClasses);
3130
}
3231
}
3332

src/DependencyInjection/Compiler/AopAttributeCompilerPass.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@ public function process(ContainerBuilder $container): void
4242
$statements = [];
4343
foreach (array_keys($container->findTaggedServiceIds(Aspect::TAG_NAME)) as $serviceId) {
4444
$reflectionClass = $this->getReflectionClass($container, $serviceId);
45-
if (!$reflectionClass) {
45+
if ($reflectionClass === null) {
4646
continue;
4747
}
4848
foreach ($reflectionClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) {
4949
foreach ($method->getAttributes() as $attribute) {
50-
/** @var \ReflectionAttribute $attribute */
50+
/** @var \ReflectionAttribute<object> $attribute */
5151
$name = $attribute->getName();
5252
if (is_subclass_of($name, Advice::class)) {
5353
$instance = $attribute->newInstance();
@@ -77,12 +77,13 @@ public function process(ContainerBuilder $container): void
7777
// 这里为了快速实现,我们使用了表达式组件,跟Spring的实现有差异
7878
$expressionLanguage = new ExpressionLanguage(null, $container->getExpressionLanguageProviders());
7979
$expressionLanguage->addFunction(ExpressionFunction::fromPhp('count'));
80+
$expressionLanguage->addFunction(ExpressionFunction::fromPhp('in_array'));
8081
foreach ($container->getServiceIds() as $serviceId) {
8182
if ($this->isInternalService($serviceId)) {
8283
continue;
8384
}
8485
$reflectionClass = $this->getReflectionClass($container, $serviceId);
85-
if (!$reflectionClass) {
86+
if ($reflectionClass === null) {
8687
continue;
8788
}
8889
// 为了减少循环依赖问题,这里我们暂时忽略Aspect
@@ -105,13 +106,14 @@ public function process(ContainerBuilder $container): void
105106
// 满足条件,我们开始拦截方法
106107
if (
107108
$statement !== $fullName
108-
&& !$expressionLanguage->evaluate($statement, [
109+
&& !(bool) $expressionLanguage->evaluate($statement, [
109110
'class' => $reflectionClass,
110111
'method' => $method,
111112
'serviceId' => $serviceId,
112113
'serviceTags' => $serviceTags,
113114
'parentClasses' => self::getParentClasses($reflectionClass),
114-
])) {
115+
])
116+
) {
115117
continue;
116118
}
117119

@@ -219,6 +221,8 @@ private function updateInternalServiceId(ContainerBuilder $container, string $se
219221

220222
if (!$container->hasDefinition($internalId)) {
221223
$definition = $container->getDefinition($serviceId);
224+
// 保存原始的 public 状态
225+
$originalPublic = $definition->isPublic();
222226
if (!$definition->isAbstract() && $serviceId !== 'session.abstract_handler') {
223227
// 声明为public,方便后面动态调用,目前主要在 ServiceCallHandler 中调用
224228
$definition->setPublic(true);
@@ -237,10 +241,10 @@ private function updateInternalServiceId(ContainerBuilder $container, string $se
237241
->setArguments([
238242
new Reference($internalId),
239243
])
240-
->setPublic($definition->isPublic())
244+
->setPublic($originalPublic)
241245
->setTags($existTags) // 这里重新补上原始服务的tag
242246
//->addTag('aop-proxy')
243-
;
247+
;
244248
// 覆盖旧的服务名
245249
$container->setDefinition($serviceId, $aopNewService);
246250
}

src/Model/ProcessContext.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44

55
class ProcessContext
66
{
7-
public function __construct(private readonly int $pid)
8-
{
9-
}
7+
public function __construct(private readonly int $pid) {}
108

119
public function getPid(): int
1210
{
@@ -17,9 +15,9 @@ public function getPid(): int
1715

1816
public static function instance(int $pid): ProcessContext
1917
{
20-
if (!isset(static::$instances[$pid])) {
21-
static::$instances[$pid] = new self($pid);
18+
if (!isset(self::$instances[$pid])) {
19+
self::$instances[$pid] = new self($pid);
2220
}
23-
return static::$instances[$pid];
21+
return self::$instances[$pid];
2422
}
2523
}

src/Service/AopInterceptor.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,14 @@ private function createJoinPoint(object $proxy, object $instance, string $method
9393
/**
9494
* 任何类型的注解,最终我们都是丢到这里去处理
9595
*
96-
* @return mixed
97-
* @var object $proxy the proxy that intercepted the method call
98-
* @var object $instance the wrapped instance within the proxy
99-
* @var string $method name of the called method
100-
* @var array $params sorted array of parameters passed to the intercepted
96+
* @param object $proxy the proxy that intercepted the method call
97+
* @param object $instance the wrapped instance within the proxy
98+
* @param string $method name of the called method
99+
* @param array $params sorted array of parameters passed to the intercepted
101100
* method, indexed by parameter name
102-
* @var bool $returnEarly flag to tell the interceptor proxy to return early, returning
101+
* @param bool $returnEarly flag to tell the interceptor proxy to return early, returning
103102
* the interceptor's return value instead of executing the method logic
103+
* @return mixed
104104
*/
105105
public function __invoke(object $proxy, object $instance, string $method, array $params, bool &$returnEarly): mixed
106106
{
@@ -183,7 +183,5 @@ public function __invoke(object $proxy, object $instance, string $method, array
183183
*/
184184
}
185185
}
186-
187-
return $returnValue;
188186
}
189187
}

tests/AopBundleTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class AopBundleTest extends TestCase
1414
public function testBundleBuild(): void
1515
{
1616
$bundle = new AopBundle();
17-
/** @var ContainerBuilder|MockObject $containerBuilder */
17+
/** @var MockObject&ContainerBuilder $containerBuilder */
1818
$containerBuilder = $this->createMock(ContainerBuilder::class);
1919

2020
// 验证是否添加了正确的编译器
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
namespace Tourze\Symfony\Aop\Tests\Aspect;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Tourze\Symfony\Aop\Aspect\ExceptionAspect;
7+
use Tourze\Symfony\Aop\Model\JoinPoint;
8+
9+
class ExceptionAspectTest extends TestCase
10+
{
11+
private ExceptionAspect $aspect;
12+
13+
public function testCatchExceptionWithException(): void
14+
{
15+
$exception = new \RuntimeException('Test exception');
16+
$joinPoint = $this->createMock(JoinPoint::class);
17+
$joinPoint->method('getException')->willReturn($exception);
18+
19+
// The method should handle the exception without throwing
20+
$this->aspect->catchException($joinPoint);
21+
22+
// No assertions needed as the method just handles the exception
23+
$this->assertTrue(true);
24+
}
25+
26+
public function testCatchExceptionWithoutException(): void
27+
{
28+
$joinPoint = $this->createMock(JoinPoint::class);
29+
$joinPoint->method('getException')->willReturn(null);
30+
31+
// The method should handle null exception gracefully
32+
$this->aspect->catchException($joinPoint);
33+
34+
// No assertions needed as the method handles null case
35+
$this->assertTrue(true);
36+
}
37+
38+
public function testCatchExceptionWithDifferentExceptionTypes(): void
39+
{
40+
$exceptions = [
41+
new \Exception('General exception'),
42+
new \RuntimeException('Runtime exception'),
43+
new \InvalidArgumentException('Invalid argument'),
44+
new \LogicException('Logic exception'),
45+
new \TypeError('Type error'),
46+
];
47+
48+
foreach ($exceptions as $exception) {
49+
$joinPoint = $this->createMock(JoinPoint::class);
50+
$joinPoint->method('getException')->willReturn($exception);
51+
52+
// Each exception type should be handled without throwing
53+
$this->aspect->catchException($joinPoint);
54+
}
55+
56+
// All exceptions handled successfully
57+
$this->assertTrue(true);
58+
}
59+
60+
public function testCatchExceptionMultipleCalls(): void
61+
{
62+
$joinPoint1 = $this->createMock(JoinPoint::class);
63+
$joinPoint1->method('getException')->willReturn(new \Exception('Exception 1'));
64+
65+
$joinPoint2 = $this->createMock(JoinPoint::class);
66+
$joinPoint2->method('getException')->willReturn(null);
67+
68+
$joinPoint3 = $this->createMock(JoinPoint::class);
69+
$joinPoint3->method('getException')->willReturn(new \Exception('Exception 3'));
70+
71+
// Multiple calls should all be handled properly
72+
$this->aspect->catchException($joinPoint1);
73+
$this->aspect->catchException($joinPoint2);
74+
$this->aspect->catchException($joinPoint3);
75+
76+
// All calls handled successfully
77+
$this->assertTrue(true);
78+
}
79+
80+
protected function setUp(): void
81+
{
82+
parent::setUp();
83+
$this->aspect = new ExceptionAspect();
84+
}
85+
}

0 commit comments

Comments
 (0)