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 ;
27
26
use Symfony \Component \ExpressionLanguage \ExpressionLanguage ;
27
+ use Symfony \Component \VarDumper \Server \DumpServer ;
28
28
29
29
/**
30
30
* @author Grégoire Pineau <lyrixx@lyrixx.info>
@@ -34,9 +34,16 @@ class ServerLogCommand extends Command
34
34
{
35
35
private const BG_COLOR = ['black ' , 'blue ' , 'cyan ' , 'green ' , 'magenta ' , 'red ' , 'white ' , 'yellow ' ];
36
36
37
- private ExpressionLanguage $ el ;
37
+ private DumpServer $ server ;
38
38
private HandlerInterface $ handler ;
39
39
40
+ public function __construct (DumpServer $ server )
41
+ {
42
+ $ this ->server = $ server ;
43
+
44
+ parent ::__construct ();
45
+ }
46
+
40
47
public function isEnabled (): bool
41
48
{
42
49
if (!class_exists (ConsoleFormatter::class)) {
@@ -78,12 +85,13 @@ protected function configure(): void
78
85
79
86
protected function execute (InputInterface $ input , OutputInterface $ output ): int
80
87
{
88
+ $ el = null ;
81
89
$ filter = $ input ->getOption ('filter ' );
82
90
if ($ filter ) {
83
91
if (!class_exists (ExpressionLanguage::class)) {
84
92
throw new LogicException ('Package "symfony/expression-language" is required to use the "filter" option. Try running "composer require symfony/expression-language". ' );
85
93
}
86
- $ this -> el = new ExpressionLanguage ();
94
+ $ el = new ExpressionLanguage ();
87
95
}
88
96
89
97
$ this ->handler = new ConsoleHandler ($ output , true , [
@@ -101,51 +109,27 @@ protected function execute(InputInterface $input, OutputInterface $output): int
101
109
$ host = 'tcp:// ' .$ host ;
102
110
}
103
111
104
- if (! $ socket = stream_socket_server ( $ host , $ errno , $ errstr )) {
105
- throw new RuntimeException ( sprintf ( ' Server start failed on "%s": ' , $ host ). $ errstr . ' ' . $ errno );
106
- }
112
+ $ this -> server -> listen (
113
+ function ( int $ clientId , string $ message ) use ( $ el , $ filter , $ output ) {
114
+ $ record = unserialize ( base64_decode ( $ message ));
107
115
108
- foreach ($ this ->getLogs ($ socket ) as $ clientId => $ message ) {
109
- $ record = unserialize (base64_decode ($ message ));
110
-
111
- // Impossible to decode the message, give up.
112
- if (false === $ record ) {
113
- continue ;
114
- }
116
+ // Impossible to decode the message, give up.
117
+ if (false === $ record ) {
118
+ return ;
119
+ }
115
120
116
- if ($ filter && !$ this -> el ->evaluate ($ filter , $ record )) {
117
- continue ;
118
- }
121
+ if ($ filter && !$ el ->evaluate ($ filter , $ record )) {
122
+ return ;
123
+ }
119
124
120
- $ this ->displayLog ($ output , $ clientId , $ record );
121
- }
125
+ $ this ->displayLog ($ output , $ clientId , $ record );
126
+ },
127
+ $ input ,
128
+ );
122
129
123
130
return 0 ;
124
131
}
125
132
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
- }
147
- }
148
-
149
133
private function displayLog (OutputInterface $ output , int $ clientId , array $ record ): void
150
134
{
151
135
if (isset ($ record ['log_id ' ])) {
0 commit comments