Skip to content

Commit cf7acdc

Browse files
committed
Optimized codes
1 parent 786eb02 commit cf7acdc

File tree

2 files changed

+13
-29
lines changed

2 files changed

+13
-29
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"pull-request": "https://github.com/friendsofhyperf/components/pulls"
2323
},
2424
"require": {
25-
"friendsofhyperf/icp-broadcaster": "~3.1.1",
25+
"friendsofhyperf/icp-broadcaster": "~3.1.2",
2626
"hyperf/event": "~3.1.0"
2727
},
2828
"minimum-stability": "dev",

src/PipeMessage.php

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use FriendsOfHyperf\IpcBroadcaster\IpcMessage;
1515
use Hyperf\Context\ApplicationContext;
1616
use Hyperf\Contract\StdoutLoggerInterface;
17-
use Psr\Container\ContainerInterface;
1817
use Throwable;
1918

2019
class 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

Comments
 (0)