Skip to content

Commit 62a904e

Browse files
committed
Configuration class
Merge branch 'v4.0-main' into v4-wotk
2 parents 2c37dc3 + d1670cf commit 62a904e

44 files changed

Lines changed: 860 additions & 862 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Makefile

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ cs: composer.lock
1414
stan: composer.lock
1515
./vendor/bin/phpstan analyse --memory-limit 256M
1616

17-
coverage: composer.lock
17+
coverage: composer.lock clean-coverage
1818
./vendor/bin/phpunit --coverage-clover coverage/clover.xml --coverage-html=coverage -d --min-coverage=100
1919

2020
composer.phar:
@@ -25,7 +25,10 @@ composer.lock: composer.phar
2525

2626
vendor/bin/phpunit: install
2727

28-
clean:
29-
rm composer.lock
30-
rm -r vendor
31-
rm -r coverage
28+
clean: clean-coverage
29+
rm -f composer.lock
30+
rm -rf vendor
31+
rm -rf coverage
32+
33+
clean-coverage:
34+
rm -rf coverage

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
},
2929
"require": {
3030
"php": "^8.1",
31+
"phrity/http": "^1.0",
3132
"phrity/net-uri": "^2.1",
3233
"phrity/net-stream": "dev-ws4 as 2.4.0",
3334
"psr/http-message": "^1.1 | ^2.0",
@@ -45,7 +46,7 @@
4546
},
4647
"suggest": {
4748
"ext-zlib": "Required for per-message deflate compression",
48-
"psr/http-message-implementation": "Use a complete http-message implementation",
49+
"psr/http-factory-implementation": "Use a complete http-factory implementation",
4950
"psr/log-implementation": "Use a logger implementation"
5051
}
5152
}

docs/Changelog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
99
### `4.0.0`
1010

11+
* Shared stream observer, sharable across multiple cients and servers (@sirn-se)
12+
* Configuration class for various settings (@sirn-se)
13+
* Many class local configuration setters removed (@sirn-se)
1114
* Remove deprecated code (@sirn-se)
1215

1316
## `v3.6`

docs/Client.md

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -201,13 +201,22 @@ $client->getContext(); // => currently used Phrity\Net\Context
201201
### HTTP factories
202202

203203
By default the Client uses a minimal [PSR-7 HTTP message](https://www.php-fig.org/psr/psr-7/) implementation.
204-
To use complete implementations such as `nyholm/psr7` or `guzzlehttp/psr7`, add their [PSR-17 HTTP factories](https://www.php-fig.org/psr/psr-17/) during setup.
204+
Other (more complete) implementations can be used by setting [PSR-17 HTTP factories](https://www.php-fig.org/psr/psr-17/) on the CLient.
205+
206+
Set a configured HttpFactory class on the Client.
205207
```php
206-
$httpFactory = new Nyholm\Psr7\Factory\Psr17Factory(); // Or any other PSR-17 factory
207-
$client
208-
->setResponseFactory($httpFactory)
209-
->setRequestFactory($httpFactory)
210-
->setUriFactory($httpFactory);
208+
$factory = new Phrity\Http\HttpFactory(
209+
requestFactory: $myRequestFactory,
210+
responseFactory: $myResponseFactory,
211+
uriFactory: $myUriFactory,
212+
);
213+
$client->setHttpFactory($factory);
214+
215+
```
216+
Or if you use factories that support multiple interfaces, available in libraries such as `nyholm/psr7` or `guzzlehttp/psr7`.
217+
```php
218+
$psrFactory = new Nyholm\Psr7\Factory\Psr17Factory(); // Or any other PSR-17 factory
219+
$client->setHttpFactory(Phrity\Http\HttpFactory::create($psrFactory));
211220
```
212221

213222
### Handshake headers

docs/Migrate_3_4.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ Version 4.x has few changes compared to previous version.
1515
* Server `onConnect()` - use `onHandshake()` instead
1616
* Server `setContext()` - no longer accepts array input
1717

18+
## Configuration management
19+
1820
v4 uses the Configuration class to propagate configurations throughout internal classes.
1921

2022
This affects the following classes;
@@ -26,9 +28,23 @@ This affects the following classes;
2628

2729
He following methods (when applicable) are removed;
2830
* `setLogger()`
31+
* `getTimeout()`
32+
* `setTimeout()`
33+
* `getFrameSize()`
34+
* `setFrameSize()`
2935

30-
Instead these classes get the method;
36+
Instead these classes can be configured using;
37+
* `__construct(..., WebSocket\Configuration|null $configuration = null)`
3138
* `setConfiguration(WebSocket\Configuration $configuration)`
3239

40+
If you need to set configuration on internal classes, best way is to clone the original;
41+
```php
42+
$clonedConfiguration = clone $source->getConfiguration();
43+
$clonedConfiguration->setLogger(...);
44+
$clonedConfiguration->setTimeout(...);
45+
$clonedConfiguration->setFrameSize(...);
46+
$source->setConfiguration($clonedConfiguration);
47+
```
48+
3349
## Extending
3450

docs/Server.md

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -192,13 +192,22 @@ $server->getContext(); // => currently used Phrity\Net\Context
192192
### HTTP factories
193193

194194
By default the Server uses a minimal [PSR-7 HTTP message](https://www.php-fig.org/psr/psr-7/) implementation.
195-
To use complete implementations such as `nyholm/psr7` or `guzzlehttp/psr7`, add their [PSR-17 HTTP factories](https://www.php-fig.org/psr/psr-17/) during setup.
195+
Other (more complete) implementations can be used by setting [PSR-17 HTTP factories](https://www.php-fig.org/psr/psr-17/) on the Server.
196+
197+
Set a configured HttpFactory class on the Client.
196198
```php
197-
$httpFactory = new Nyholm\Psr7\Factory\Psr17Factory(); // Or any other PSR-17 factory
198-
$server
199-
->setResponseFactory($httpFactory)
200-
->setServerRequestFactory($httpFactory)
201-
->setUriFactory($httpFactory);
199+
$factory = new Phrity\Http\HttpFactory(
200+
serverRequestFactory: $myServerRequestFactory,
201+
responseFactory: $myResponseFactory,
202+
uriFactory: $myUriFactory,
203+
);
204+
$server->setHttpFactory($factory);
205+
```
206+
207+
Or if you use factories that support multiple interfaces, available in libraries such as `nyholm/psr7` or `guzzlehttp/psr7`.
208+
```php
209+
$psrFactory = new Nyholm\Psr7\Factory\Psr17Factory(); // Or any other PSR-17 factory
210+
$server->setHttpFactory(Phrity\Http\HttpFactory::create($psrFactory));
202211
```
203212

204213
### Max connections

examples/delegating_server.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
* ssl: bool,
5555
* timeout: int<0, max>|float,
5656
* framesize: int<1, max>,
57-
* connections: int<0, max>|null,
57+
* connections: int<1, max>|null,
5858
* deflate: bool,
5959
* } $options
6060
*/
@@ -102,7 +102,7 @@
102102
echo "# Set frame size: {$options['framesize']}\n";
103103
}
104104
if (isset($options['connections'])) {
105-
$server->setMaxConnections($options['connections']);
105+
$server->getConfiguration()->setMaxConnections($options['connections']);
106106
echo "# Set max connections: {$options['connections']}\n";
107107
}
108108
if (isset($options['deflate'])) {

examples/echoserver.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
* ssl: bool,
5454
* timeout: int<0, max>|float,
5555
* framesize: int<1, max>,
56-
* connections: int<0, max>|null,
56+
* connections: int<1, max>|null,
5757
* deflate: bool,
5858
* } $options
5959
*/
@@ -83,7 +83,7 @@
8383
echo "# Set frame size: {$options['framesize']}\n";
8484
}
8585
if (isset($options['connections'])) {
86-
$server->setMaxConnections($options['connections']);
86+
$server->getConfiguration()->setMaxConnections($options['connections']);
8787
echo "# Set max connections: {$options['connections']}\n";
8888
}
8989
if (isset($options['deflate'])) {
@@ -121,8 +121,8 @@
121121
$msg .= " - Connected: " . json_encode($connection->isConnected()) . "\n";
122122
$msg .= " - Readable: " . json_encode($connection->isReadable()) . "\n";
123123
$msg .= " - Writable: " . json_encode($connection->isWritable()) . "\n";
124-
$msg .= " - Timeout: {$connection->getTimeout()}s\n";
125-
$msg .= " - Frame size: {$connection->getFrameSize()}b\n";
124+
$msg .= " - Timeout: {$connection->getConfiguration()->getTimeout()}s\n";
125+
$msg .= " - Frame size: {$connection->getConfiguration()->getFrameSize()}b\n";
126126
echo "< [{$connection->getRemoteName()}] {$msg}";
127127
$server->send(new Text($msg));
128128
break;

examples/random_server.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
* ssl: bool,
6464
* timeout: int<0, max>|float,
6565
* framesize: int<1, max>,
66-
* connections: int<0, max>|null,
66+
* connections: int<1, max>|null,
6767
* deflate: bool,
6868
* } $options
6969
*/
@@ -95,7 +95,7 @@
9595
echo "# Set frame size: {$options['framesize']}\n";
9696
}
9797
if (isset($options['connections'])) {
98-
$server->setMaxConnections($options['connections']);
98+
$server->getConfiguration()->setMaxConnections($options['connections']);
9999
echo "# Set max connections: {$options['connections']}\n";
100100
}
101101
if (isset($options['deflate'])) {

phpunit.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
<testsuites>
1515
<testsuite name="Unit tests">
1616
<directory suffix=".php">tests/suites/client</directory>
17+
<directory suffix=".php">tests/suites/configuration</directory>
1718
<directory suffix=".php">tests/suites/connection</directory>
1819
<directory suffix=".php">tests/suites/frame</directory>
1920
<directory suffix=".php">tests/suites/http</directory>

0 commit comments

Comments
 (0)