66use Monolog \Logger ;
77use Psr \Http \Message \ResponseInterface ;
88use Psr \Http \Message \ServerRequestInterface ;
9+ use Psr \Log \LoggerInterface ;
910use React \EventLoop \Loop ;
1011use React \EventLoop \LoopInterface ;
1112use React \Http \HttpServer ;
@@ -36,7 +37,7 @@ class Server {
3637 public function __construct (
3738 private PersistentState $ state ,
3839 private string $ hostAddr ,
39- private Logger | false $ logger = false
40+ private ? LoggerInterface $ logger = null
4041 ) {
4142 $ this ->endpoints ['/ ' ] = new VerifiedEndpoint ($ state );
4243 $ this ->endpoints ['/verified ' ] = &$ this ->endpoints ['/ ' ];
@@ -57,7 +58,7 @@ public function logError($e, bool $fatal = false): void
5758 $ error = 'Error: ' . $ e ->getMessage () . PHP_EOL .
5859 'Line ' . $ e ->getLine () . ' in ' . $ e ->getFile () . PHP_EOL .
5960 $ e ->getTraceAsString ();
60- if ($ this ->logger ) $ this ->logger ->warning ($ error );
61+ if (isset ( $ this ->logger ) ) $ this ->logger ->warning ($ error );
6162 }
6263
6364 /**
@@ -138,7 +139,31 @@ public function getLoop(): ?LoopInterface
138139 {
139140 return $ this ->loop ?? null ;
140141 }
141-
142+
143+ /**
144+ * Retrieves the logger instance.
145+ *
146+ * @return LoggerInterface|null Returns the logger instance if available, or null if the logger is disabled.
147+ */
148+ public function getLogger (): ?LoggerInterface
149+ {
150+ return isset ($ this ->logger )
151+ ? $ this ->logger
152+ : null ;
153+ }
154+
155+ /**
156+ * Retrieves the logger instance.
157+ *
158+ * @return LoggerInterface|true|null Returns the logger instance if available, or null if the logger is disabled.
159+ */
160+ public function setLogger (LoggerInterface |true |null $ logger ): void
161+ {
162+ $ this ->logger = $ logger === true
163+ ? new Logger ('VerifierServer ' , [new \Monolog \Handler \StreamHandler ('php://stdout ' , Level::Info)])
164+ : $ logger ;
165+ }
166+
142167 /**
143168 * Retrieves the server instance.
144169 *
@@ -160,17 +185,19 @@ public function getState(): PersistentState
160185 }
161186
162187 /**
163- * Set the verbosity level of the server.
188+ * Converts an associative array into a request string format.
189+ * Useful for forging requests in tests or applications that require internal request generation.
164190 *
165- * @param Logger|bool $bool True to enable logging, false to disable it.
191+ * Each key-value pair in the array is transformed into a string
192+ * in the format "key: value" and concatenated with a newline character.
193+ *
194+ * @param array $formData The associative array to be converted.
195+ *
196+ * @return string The resulting request string.
166197 */
167- public function setLogger ( Logger | bool $ logger = false ): void
198+ public static function arrayToRequestString ( array $ formData ): string
168199 {
169- if ($ logger === true ) {
170- $ logger = new Logger ('Verifier Server ' );
171- $ logger ->pushHandler (new \Monolog \Handler \StreamHandler ('php://stdout ' , Level::Debug));
172- }
173- $ this ->logger = $ logger ;
200+ return implode (PHP_EOL , array_map (fn ($ key , $ value ) => $ key . ': ' . $ value , array_keys ($ formData ), $ formData ));
174201 }
175202
176203 /**
@@ -180,7 +207,7 @@ public function setLogger(Logger|bool $logger = false): void
180207 *
181208 * @return \Psr\Http\Message\ResponseInterface The generated HTTP response.
182209 */
183- public function handleReact (ServerRequestInterface $ client ): ResponseInterface
210+ private function handleReact (ServerRequestInterface $ client ): ResponseInterface
184211 {
185212 $ method = $ client ->getMethod ();
186213 $ uri = $ client ->getUri ()->getPath ();
@@ -216,7 +243,7 @@ public function handleReact(ServerRequestInterface $client): ResponseInterface
216243 *
217244 * @return null Always returns null after processing the request.
218245 */
219- public function handleResource ($ client ): null
246+ private function handleResource ($ client ): null
220247 {
221248 $ request = fread ($ client , 1024 );
222249 $ headers = [];
@@ -277,22 +304,6 @@ public function handleResource($client): null
277304 return null ;
278305 }
279306
280- /**
281- * Converts an associative array into a request string format.
282- * Useful for forging requests in tests or applications that require internal request generation.
283- *
284- * Each key-value pair in the array is transformed into a string
285- * in the format "key: value" and concatenated with a newline character.
286- *
287- * @param array $formData The associative array to be converted.
288- *
289- * @return string The resulting request string.
290- */
291- public static function arrayToRequestString (array $ formData ): string
292- {
293- return implode (PHP_EOL , array_map (fn ($ key , $ value ) => $ key . ': ' . $ value , array_keys ($ formData ), $ formData ));
294- }
295-
296307 /**
297308 * Destructor method that is automatically called when the object is destroyed.
298309 * It ensures that the server is properly stopped by calling the stop() method.
0 commit comments