20
20
use Symfony \Component \Console \Attribute \AsCommand ;
21
21
use Symfony \Component \Console \Command \Command ;
22
22
use Symfony \Component \Console \Exception \LogicException ;
23
- use Symfony \Component \Console \Exception \RuntimeException ;
24
23
use Symfony \Component \Console \Input \InputInterface ;
25
24
use Symfony \Component \Console \Input \InputOption ;
26
25
use Symfony \Component \Console \Output \OutputInterface ;
@@ -34,7 +33,6 @@ class ServerLogCommand extends Command
34
33
{
35
34
private const BG_COLOR = ['black ' , 'blue ' , 'cyan ' , 'green ' , 'magenta ' , 'red ' , 'white ' , 'yellow ' ];
36
35
37
- private ExpressionLanguage $ el ;
38
36
private HandlerInterface $ handler ;
39
37
40
38
public function isEnabled (): bool
@@ -78,12 +76,13 @@ protected function configure(): void
78
76
79
77
protected function execute (InputInterface $ input , OutputInterface $ output ): int
80
78
{
79
+ $ el = null ;
81
80
$ filter = $ input ->getOption ('filter ' );
82
81
if ($ filter ) {
83
82
if (!class_exists (ExpressionLanguage::class)) {
84
83
throw new LogicException ('Package "symfony/expression-language" is required to use the "filter" option. Try running "composer require symfony/expression-language". ' );
85
84
}
86
- $ this -> el = new ExpressionLanguage ();
85
+ $ el = new ExpressionLanguage ();
87
86
}
88
87
89
88
$ this ->handler = new ConsoleHandler ($ output , true , [
@@ -101,49 +100,28 @@ protected function execute(InputInterface $input, OutputInterface $output): int
101
100
$ host = 'tcp:// ' .$ host ;
102
101
}
103
102
104
- if (!$ socket = stream_socket_server ($ host , $ errno , $ errstr )) {
105
- throw new RuntimeException (sprintf ('Server start failed on "%s": ' , $ host ).$ errstr .' ' .$ errno );
106
- }
107
-
108
- foreach ($ this ->getLogs ($ socket ) as $ clientId => $ message ) {
109
- $ record = unserialize (base64_decode ($ message ));
103
+ $ streamHelper = $ this ->getHelper ('stream ' );
104
+ $ streamHelper ->listen (
105
+ $ input ,
106
+ $ output ,
107
+ $ host ,
108
+ function (int $ clientId , string $ message ) use ($ el , $ filter , $ output ) {
109
+ $ record = unserialize (base64_decode ($ message ));
110
+
111
+ // Impossible to decode the message, give up.
112
+ if (false === $ record ) {
113
+ return ;
114
+ }
110
115
111
- // Impossible to decode the message, give up.
112
- if (false === $ record ) {
113
- continue ;
114
- }
116
+ if ($ filter && !$ el ->evaluate ($ filter , $ record )) {
117
+ return ;
118
+ }
115
119
116
- if ($ filter && !$ this ->el ->evaluate ($ filter , $ record )) {
117
- continue ;
120
+ $ this ->displayLog ($ output , $ clientId , $ record );
118
121
}
122
+ );
119
123
120
- $ this ->displayLog ($ output , $ clientId , $ record );
121
- }
122
-
123
- return 0 ;
124
- }
125
-
126
- private function getLogs ($ socket ): iterable
127
- {
128
- $ sockets = [(int ) $ socket => $ socket ];
129
- $ write = [];
130
-
131
- while (true ) {
132
- $ read = $ sockets ;
133
- stream_select ($ read , $ write , $ write , null );
134
-
135
- foreach ($ read as $ stream ) {
136
- if ($ socket === $ stream ) {
137
- $ stream = stream_socket_accept ($ socket );
138
- $ sockets [(int ) $ stream ] = $ stream ;
139
- } elseif (feof ($ stream )) {
140
- unset($ sockets [(int ) $ stream ]);
141
- fclose ($ stream );
142
- } else {
143
- yield (int ) $ stream => fgets ($ stream );
144
- }
145
- }
146
- }
124
+ return Command::SUCCESS ;
147
125
}
148
126
149
127
private function displayLog (OutputInterface $ output , int $ clientId , array $ record ): void
0 commit comments