Skip to content

Commit 2c37dc3

Browse files
committed
Configuration class
1 parent b50bca2 commit 2c37dc3

19 files changed

Lines changed: 211 additions & 161 deletions

composer.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,13 @@
3535
},
3636
"require-dev": {
3737
"guzzlehttp/psr7": "^2.0",
38-
"php-coveralls/php-coveralls": "^2.0",
3938
"phpstan/phpstan": "^2.0",
4039
"phpunit/phpunit": "^10.0 | ^11.0 | ^12.0",
4140
"phrity/logger-console": "^1.0",
4241
"phrity/net-mock": "dev-ws4 as 2.4.0",
4342
"phrity/util-errorhandler": "^1.1",
4443
"robiningelbrecht/phpunit-coverage-tools": "^1.9",
45-
"squizlabs/php_codesniffer": "^3.5"
44+
"squizlabs/php_codesniffer": "^4.0"
4645
},
4746
"suggest": {
4847
"ext-zlib": "Required for per-message deflate compression",

docs/Migrate_3_4.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,21 @@ Version 4.x has few changes compared to previous version.
1414
* Client `setContext()` - no longer accepts array input
1515
* Server `onConnect()` - use `onHandshake()` instead
1616
* Server `setContext()` - no longer accepts array input
17-
* FrameHandler `setLogger()` - removed
17+
18+
v4 uses the Configuration class to propagate configurations throughout internal classes.
19+
20+
This affects the following classes;
21+
* Connection
22+
* FrameHandler
23+
* MessageHandler
24+
* MiddlewareHandler
25+
* All middlewares
26+
27+
He following methods (when applicable) are removed;
28+
* `setLogger()`
29+
30+
Instead these classes get the method;
31+
* `setConfiguration(WebSocket\Configuration $configuration)`
1832

1933
## Extending
2034

src/Client.php

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
use WebSocket\Message\Message;
4444
use WebSocket\Middleware\MiddlewareInterface;
4545
use WebSocket\Trait\{
46+
ConfigurationTrait,
4647
ListenerTrait,
4748
LoggerAwareTrait,
4849
SendMethodsTrait,
@@ -55,6 +56,7 @@
5556
*/
5657
class Client implements LoggerAwareInterface, Stringable
5758
{
59+
use ConfigurationTrait;
5860
/** @use ListenerTrait<Client> */
5961
use ListenerTrait;
6062
use LoggerAwareTrait;
@@ -91,7 +93,7 @@ class Client implements LoggerAwareInterface, Stringable
9193
/**
9294
* @param UriInterface|string $uri A ws/wss-URI
9395
*/
94-
public function __construct(UriInterface|string $uri)
96+
public function __construct(UriInterface|string $uri, Configuration|null $configuration = null)
9597
{
9698
$this->socketUri = $this->parseUri($uri);
9799
$this->initLogger();
@@ -102,6 +104,7 @@ public function __construct(UriInterface|string $uri)
102104
= $this->serverRequestFactory
103105
= $this->uriFactory
104106
= new DefaultHttpFactory();
107+
$this->initConfiguration($configuration);
105108
}
106109

107110
/**
@@ -133,10 +136,7 @@ public function setStreamFactory(StreamFactory $streamFactory): self
133136
*/
134137
public function setLogger(LoggerInterface $logger): void
135138
{
136-
$this->logger = $logger;
137-
if ($this->connection) {
138-
$this->connection->setLogger($this->logger);
139-
}
139+
$this->getConfiguration()->setLogger($logger);
140140
}
141141

142142
/**
@@ -317,12 +317,12 @@ public function start(int|float|null $timeout = null): void
317317
{
318318
// Check if running
319319
if ($this->running) {
320-
$this->logger->warning("[client] Client is already running");
320+
$this->configuration->getLogger()->warning("[client] Client is already running");
321321
return;
322322
}
323323
$this->running = true;
324324
$reconnect = false;
325-
$this->logger->info("[client] Client is running");
325+
$this->configuration->getLogger()->info("[client] Client is running");
326326

327327
$connection = $this->connection();
328328

@@ -340,12 +340,12 @@ public function start(int|float|null $timeout = null): void
340340
$this->dispatch($message->getOpcode(), [$this, $connection, $message]);
341341
} catch (MessageLevelInterface $e) {
342342
// Error, but keep connection open
343-
$this->logger->error("[client] {$e->getMessage()}", ['exception' => $e]);
343+
$this->configuration->getLogger()->error("[client] {$e->getMessage()}", ['exception' => $e]);
344344
$this->dispatch('error', [$this, $connection, $e]);
345345
} catch (ConnectionLevelInterface $e) {
346346
// Error, disconnect connection
347347
$this->disconnect();
348-
$this->logger->error("[client] {$e->getMessage()}", ['exception' => $e]);
348+
$this->configuration->getLogger()->error("[client] {$e->getMessage()}", ['exception' => $e]);
349349
$this->dispatch('error', [$this, $connection, $e]);
350350
}
351351
}
@@ -357,7 +357,7 @@ public function start(int|float|null $timeout = null): void
357357
} catch (CloseException $e) {
358358
// Close connection
359359
$connection->close($e->getCloseStatus(), $e->getMessage());
360-
$this->logger->error("[server] {$e->getMessage()}", ['exception' => $e]);
360+
$this->configuration->getLogger()->error("[server] {$e->getMessage()}", ['exception' => $e]);
361361
$this->dispatch('error', [$this, $connection, $e]);
362362
} catch (ReconnectException $e) {
363363
// Reconnect connection
@@ -366,21 +366,21 @@ public function start(int|float|null $timeout = null): void
366366
$this->socketUri = $uri;
367367
}
368368
$connection->close();
369-
$this->logger->error("[server] {$e->getMessage()}", ['exception' => $e]);
369+
$this->configuration->getLogger()->error("[server] {$e->getMessage()}", ['exception' => $e]);
370370
$this->dispatch('error', [$this, $connection, $e]);
371371
} catch (ExceptionInterface $e) {
372372
$this->disconnect();
373373
$this->running = false;
374374

375375
// Low-level error
376-
$this->logger->error("[client] {$e->getMessage()}", ['exception' => $e]);
376+
$this->configuration->getLogger()->error("[client] {$e->getMessage()}", ['exception' => $e]);
377377
$this->dispatch('error', [$this, null, $e]);
378378
} catch (Throwable $e) {
379379
$this->disconnect();
380380
$this->running = false;
381381

382382
// Crash it
383-
$this->logger->error("[client] {$e->getMessage()}", ['exception' => $e]);
383+
$this->configuration->getLogger()->error("[client] {$e->getMessage()}", ['exception' => $e]);
384384
throw $e;
385385
}
386386
gc_collect_cycles(); // Collect garbage
@@ -398,7 +398,7 @@ public function start(int|float|null $timeout = null): void
398398
public function stop(): void
399399
{
400400
$this->running = false;
401-
$this->logger->info("[client] Client is stopped");
401+
$this->configuration->getLogger()->info("[client] Client is stopped");
402402
}
403403

404404
/**
@@ -468,7 +468,7 @@ public function connect(): void
468468
$stream = $client->connect();
469469
} catch (Throwable $e) {
470470
$error = "Could not open socket to \"{$hostUri}\": {$e->getMessage()}";
471-
$this->logger->error("[client] {$error}", ['exception' => $e]);
471+
$this->configuration->getLogger()->error("[client] {$error}", ['exception' => $e]);
472472
throw new ClientException($error);
473473
}
474474
$name = $stream->getRemoteName();
@@ -484,14 +484,14 @@ public function connect(): void
484484
);
485485
$this->connection->setFrameSize($this->frameSize);
486486
$this->connection->setTimeout($this->timeout);
487-
$this->connection->setLogger($this->logger);
487+
// $this->connection->setLogger($this->configuration->getLogger());
488488
foreach ($this->middlewares as $middleware) {
489489
$this->connection->addMiddleware($middleware);
490490
}
491491

492492
if (!$this->isConnected()) {
493493
$error = "Invalid stream on \"{$hostUri}\".";
494-
$this->logger->error("[client] {$error}");
494+
$this->configuration->getLogger()->error("[client] {$error}");
495495
throw new ClientException($error);
496496
}
497497
try {
@@ -500,14 +500,14 @@ public function connect(): void
500500
$response = $this->performHandshake($this->socketUri, $this->connection);
501501
}
502502
} catch (ReconnectException $e) {
503-
$this->logger->info("[client] {$e->getMessage()}", ['exception' => $e]);
503+
$this->configuration->getLogger()->info("[client] {$e->getMessage()}", ['exception' => $e]);
504504
if ($uri = $e->getUri()) {
505505
$this->socketUri = $uri;
506506
}
507507
$this->connect();
508508
return;
509509
}
510-
$this->logger->info("[client] Client connected to {$this->socketUri}");
510+
$this->configuration->getLogger()->info("[client] Client connected to {$this->socketUri}");
511511
$this->dispatch('handshake', [
512512
$this,
513513
$this->connection,
@@ -524,7 +524,7 @@ public function disconnect(): void
524524
{
525525
if ($this->connection && $this->isConnected()) {
526526
$this->connection->disconnect();
527-
$this->logger->info('[client] Client disconnected');
527+
$this->configuration->getLogger()->info('[client] Client disconnected');
528528
$this->dispatch('disconnect', [$this, $this->connection]);
529529
}
530530
}
@@ -616,11 +616,11 @@ protected function performHandshake(Uri $uri, Connection $connection): ResponseI
616616
throw new HandshakeException("Server sent bad upgrade response.", $response);
617617
}
618618
} catch (HandshakeException $e) {
619-
$this->logger->error("[client] {$e->getMessage()}", ['exception' => $e]);
619+
$this->configuration->getLogger()->error("[client] {$e->getMessage()}", ['exception' => $e]);
620620
throw $e;
621621
}
622622

623-
$this->logger->debug("[client] Handshake on {$uri->getPath()}");
623+
$this->configuration->getLogger()->debug("[client] Handshake on {$uri->getPath()}");
624624
$connection->setHandshakeRequest($request);
625625
$connection->setHandshakeResponse($response);
626626

src/Configuration.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
/**
4+
* Copyright (C) 2014-2025 Textalk and contributors.
5+
* This file is part of Websocket PHP and is free software under the ISC License.
6+
*/
7+
8+
namespace WebSocket;
9+
10+
use Psr\Log\{
11+
LoggerAwareInterface,
12+
LoggerInterface,
13+
NullLogger,
14+
};
15+
use Stringable;
16+
use WebSocket\Trait\StringableTrait;
17+
18+
/**
19+
* WebSocket\Connection class.
20+
* A client/server connection, wrapping socket stream.
21+
*/
22+
class Configuration implements LoggerAwareInterface, Stringable
23+
{
24+
use StringableTrait;
25+
26+
private LoggerInterface $logger;
27+
28+
29+
/* ---------- Magic methods ------------------------------------------------------------------------------------ */
30+
31+
public function __construct(
32+
LoggerInterface|null $logger = null,
33+
) {
34+
$this->logger = $logger ?? new NullLogger();
35+
}
36+
37+
public function __toString(): string
38+
{
39+
return $this->stringable('');
40+
}
41+
42+
43+
/* ---------- Logger methods ----------------------------------------------------------------------------------- */
44+
45+
public function getLogger(): LoggerInterface
46+
{
47+
return $this->logger;
48+
}
49+
50+
public function setLogger(LoggerInterface $logger): void
51+
{
52+
$this->logger = $logger;
53+
}
54+
}

0 commit comments

Comments
 (0)