1414use FriendsOfHyperf \IpcBroadcaster \IpcMessage ;
1515use Hyperf \Context \ApplicationContext ;
1616use Hyperf \Contract \StdoutLoggerInterface ;
17- use Psr \Container \ContainerInterface ;
1817use Throwable ;
1918
2019class PipeMessage extends IpcMessage
@@ -28,50 +27,35 @@ public function __construct(
2827 public function handle (): void
2928 {
3029 try {
31- if (! $ sender = $ this ->getSender ( )) {
30+ if (! $ sender = $ this ->get (Sender::class )) {
3231 return ;
3332 }
3433
3534 $ params = $ sender ->getFdAndMethodFromProxyMethod ($ this ->method , $ this ->arguments );
3635
3736 $ sender ->proxy (...$ params );
3837 } catch (Throwable $ exception ) {
39- $ this ->getLogger ( )?->warning((string ) $ exception );
38+ $ this ->get (StdoutLoggerInterface::class )?->warning((string ) $ exception );
4039 }
4140 }
4241
43- private function getLogger (): ?StdoutLoggerInterface
42+ /**
43+ * @template T
44+ * @param class-string<T> $class
45+ * @return T|null
46+ */
47+ private function get (string $ class )
4448 {
45- if (! $ container = $ this ->getContainer ()) {
46- return null ;
47- }
48-
49- if (! $ container ->has (StdoutLoggerInterface::class)) {
50- return null ;
51- }
52-
53- return $ container ->get (StdoutLoggerInterface::class);
54- }
55-
56- private function getSender (): ?Sender
57- {
58- if (! $ container = $ this ->getContainer ()) {
59- return null ;
60- }
61-
62- if (! $ container ->has (Sender::class)) {
49+ if (! ApplicationContext::hasContainer ()) {
6350 return null ;
6451 }
6552
66- return $ container ->get (Sender::class);
67- }
53+ $ container = ApplicationContext::getContainer ();
6854
69- private function getContainer (): ?ContainerInterface
70- {
71- if (! ApplicationContext::hasContainer ()) {
55+ if (! $ container ->has ($ class )) {
7256 return null ;
7357 }
7458
75- return ApplicationContext:: getContainer ( );
59+ return $ container -> get ( $ class );
7660 }
7761}
0 commit comments