Skip to content

Commit 67efe78

Browse files
committed
refactor: simplify UdpSocketAspect by removing unnecessary properties and methods
1 parent d8c10d7 commit 67efe78

File tree

1 file changed

+3
-40
lines changed

1 file changed

+3
-40
lines changed

src/monolog-hook/src/Aspect/UdpSocketAspect.php

Lines changed: 3 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -11,47 +11,10 @@
1111

1212
namespace FriendsOfHyperf\MonologHook\Aspect;
1313

14-
use Hyperf\Coroutine\Coroutine;
15-
use Hyperf\Di\Aop\AbstractAspect;
16-
use Hyperf\Di\Aop\ProceedingJoinPoint;
17-
use Monolog\Handler\SyslogUdp\UdpSocket;
18-
use Socket;
19-
use WeakMap;
14+
class_alias(\Hyperf\Logger\Aspect\UdpSocketAspect::class, UdpSocketAspect::class);
2015

21-
/**
22-
* @property null|Socket $socket
23-
*/
24-
class UdpSocketAspect extends AbstractAspect
25-
{
26-
public array $classes = [
27-
UdpSocket::class . '::getSocket',
28-
];
29-
30-
public static WeakMap $coSockets;
31-
32-
public function __construct()
33-
{
34-
self::$coSockets = new WeakMap();
35-
}
36-
37-
public function process(ProceedingJoinPoint $proceedingJoinPoint)
16+
if (! class_exists(UdpSocketAspect::class)) {
17+
class UdpSocketAspect extends \Hyperf\Logger\Aspect\UdpSocketAspect
3818
{
39-
if (! Coroutine::inCoroutine()) {
40-
return $proceedingJoinPoint->process();
41-
}
42-
43-
$instance = $proceedingJoinPoint->getInstance();
44-
45-
if (isset(self::$coSockets[$instance]) && self::$coSockets[$instance] instanceof Socket) {
46-
return self::$coSockets[$instance];
47-
}
48-
49-
return self::$coSockets[$instance] = (function () use ($proceedingJoinPoint) {
50-
$nonCoSocket = $this->socket; // Save the socket of non-coroutine.
51-
$this->socket = null; // Unset the socket of non-coroutine.
52-
$coSocket = $proceedingJoinPoint->process(); // ReCreate the socket in coroutine.
53-
$this->socket = $nonCoSocket; // Restore the socket of non-coroutine.
54-
return $coSocket;
55-
})->call($instance);
5619
}
5720
}

0 commit comments

Comments
 (0)