|
11 | 11 |
|
12 | 12 | namespace FriendsOfHyperf\MonologHook\Aspect; |
13 | 13 |
|
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); |
20 | 15 |
|
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 |
38 | 18 | { |
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); |
56 | 19 | } |
57 | 20 | } |
0 commit comments