Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stream & Composite handlers #95

Merged
merged 44 commits into from Oct 8, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
e58f2b3
Add UDP and Composite handlers
xepozz Sep 4, 2023
258cbb3
Apply fixes from StyleCI
StyleCIBot Sep 4, 2023
744d994
Fix psalm errors
xepozz Sep 4, 2023
ced31cb
Add changelog
xepozz Sep 4, 2023
257d216
Remove default values
xepozz Sep 4, 2023
8d0b155
Rename udp handler to stream handler, add tests
xepozz Sep 8, 2023
54544d7
Apply fixes from StyleCI
StyleCIBot Sep 8, 2023
db6e45a
Fix psalm errors
xepozz Sep 9, 2023
0a6114e
Merge remote-tracking branch 'origin/upd-handler' into upd-handler
xepozz Sep 9, 2023
7e87045
Remove redundant code
xepozz Sep 9, 2023
734d9c5
Apply fixes from StyleCI
StyleCIBot Sep 9, 2023
c5ee52a
Merge remote-tracking branch 'origin/upd-handler' into upd-handler
xepozz Sep 9, 2023
efbd838
Add test for CompositeHandler
xepozz Sep 9, 2023
8e9e9ba
Add test for EchoHandler
xepozz Sep 9, 2023
a3a5c33
Fix rector
xepozz Sep 9, 2023
06b3477
Add more tests
xepozz Sep 9, 2023
af4113b
Add more tests
xepozz Sep 9, 2023
c344ac0
Fix review comments
xepozz Sep 9, 2023
6c10118
Fix psalm
xepozz Sep 9, 2023
7ad6b8c
Fix composer-require-checker
xepozz Sep 9, 2023
36a4317
Apply fixes from StyleCI
StyleCIBot Sep 9, 2023
429af6f
Add php8.2
xepozz Sep 9, 2023
7b35ada
Disable a few tests on Windows
xepozz Sep 9, 2023
0cfe382
Merge remote-tracking branch 'origin/upd-handler' into upd-handler
xepozz Sep 9, 2023
251fc9f
Disable a few tests on Windows
xepozz Sep 9, 2023
51c4a28
Add a changelog entry
xepozz Sep 9, 2023
43862cf
Fix suggestion text
xepozz Sep 9, 2023
b670bcb
Add destructor tests
xepozz Sep 9, 2023
7b56f8a
Separate sockets and streams
xepozz Sep 9, 2023
d2796d0
Fix test for php 8.0
xepozz Sep 9, 2023
319e9d2
Add immutability test
xepozz Sep 9, 2023
e02ca9e
Apply suggestions from code review
xepozz Sep 16, 2023
64c737e
Apply code review suggestions
xepozz Sep 16, 2023
a9f8dff
Remove `readonly` keyword
xepozz Sep 16, 2023
c19d4c4
Support Socket object
xepozz Sep 16, 2023
9f572c2
Fix composer-require-checker
xepozz Sep 16, 2023
9d270f7
Fix psalm
xepozz Sep 16, 2023
14e59f3
Corrections for PR 95 (#96)
arogachev Sep 20, 2023
8f2b075
Fix psalm
xepozz Sep 21, 2023
6445954
Apply suggestions from code review
xepozz Oct 4, 2023
1edd721
Add phpdoc
xepozz Oct 4, 2023
4dec406
Update .github/workflows/mutation.yml
xepozz Oct 4, 2023
eff8f8f
Update readme
xepozz Oct 4, 2023
9fd803d
Merge remote-tracking branch 'origin/upd-handler' into upd-handler
xepozz Oct 4, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,7 @@

## 1.7.0 under development

- New #95: Add `UdpHandler` and `CompositeHandler` (@xepozz)
- Enh #94: Add a middleware handler to control dumps' output destination (@xepozz)
- New #94: Add `dump` function (@xepozz)

Expand Down
11 changes: 11 additions & 0 deletions README.md
Expand Up @@ -100,6 +100,17 @@ In the above `asJson()` will give you nicely formatted code. You can remove form

`$depth` argument allows you to set maximum recursion depth.

## Output destination

Choose one of existing or create a new one class to control the destination where "dumps" will be sent to:
xepozz marked this conversation as resolved.
Show resolved Hide resolved
- [EchoHandler](./src/Handler/EchoHandler.php)
- Uses `echo` to write to stdout stream.
- Uses by default.
xepozz marked this conversation as resolved.
Show resolved Hide resolved
- [UdpHandler](./src/Handler/UdpHandler.php)
- Uses `ext-sockets` to sent encoded with `json_encode` dumps to a UDP socket.
xepozz marked this conversation as resolved.
Show resolved Hide resolved
- [CompositeHandler](./src/Handler/CompositeHandler.php)
- Helpful class to sent dumps to multiple handlers in a row, for example `EchoHandler` and `UdpHandler`.

xepozz marked this conversation as resolved.
Show resolved Hide resolved
## Limitations

Current limitations are:
Expand Down
3 changes: 3 additions & 0 deletions composer.json
Expand Up @@ -33,6 +33,9 @@
"spatie/phpunit-watcher": "^1.23",
"vimeo/psalm": "^4.30|^5.3"
},
"suggest": {
"ext-sockets": "Send dumps to a server through UDP protocol"
},
"autoload": {
"psr-4": {
"Yiisoft\\VarDumper\\": "src"
Expand Down
25 changes: 25 additions & 0 deletions src/Handler/CompositeHandler.php
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace Yiisoft\VarDumper\Handler;

use Yiisoft\VarDumper\HandlerInterface;

final class CompositeHandler implements HandlerInterface
arogachev marked this conversation as resolved.
Show resolved Hide resolved
{
/**
* @param HandlerInterface[] $handlers
*/
public function __construct(

Check warning on line 14 in src/Handler/CompositeHandler.php

View check run for this annotation

Codecov / codecov/patch

src/Handler/CompositeHandler.php#L14

Added line #L14 was not covered by tests
private array $handlers
) {
}

Check warning on line 17 in src/Handler/CompositeHandler.php

View check run for this annotation

Codecov / codecov/patch

src/Handler/CompositeHandler.php#L17

Added line #L17 was not covered by tests

public function handle(mixed $variable, int $depth, bool $highlight = false): void

Check warning on line 19 in src/Handler/CompositeHandler.php

View check run for this annotation

Codecov / codecov/patch

src/Handler/CompositeHandler.php#L19

Added line #L19 was not covered by tests
{
foreach ($this->handlers as $handler) {
$handler->handle($variable, $depth, $highlight);

Check warning on line 22 in src/Handler/CompositeHandler.php

View check run for this annotation

Codecov / codecov/patch

src/Handler/CompositeHandler.php#L21-L22

Added lines #L21 - L22 were not covered by tests
}
}
}
57 changes: 57 additions & 0 deletions src/Handler/UdpHandler.php
@@ -0,0 +1,57 @@
<?php

declare(strict_types=1);

namespace Yiisoft\VarDumper\Handler;

use Exception;
use RuntimeException;
use Socket;
use Yiisoft\VarDumper\HandlerInterface;

final class UdpHandler implements HandlerInterface
{
private ?Socket $socket = null;

public function __construct(

Check warning on line 16 in src/Handler/UdpHandler.php

View check run for this annotation

Codecov / codecov/patch

src/Handler/UdpHandler.php#L16

Added line #L16 was not covered by tests
private string $host,
private int $port,
) {
if (!extension_loaded('sockets')) {
xepozz marked this conversation as resolved.
Show resolved Hide resolved
throw new Exception('The "ext-socket" extension is not installed.');

Check warning on line 21 in src/Handler/UdpHandler.php

View check run for this annotation

Codecov / codecov/patch

src/Handler/UdpHandler.php#L20-L21

Added lines #L20 - L21 were not covered by tests
}
}

/**
* Sends encode with {@see \json_encode()} function $variable to a UDP socket.
*/
public function handle(mixed $variable, int $depth, bool $highlight = false): void

Check warning on line 28 in src/Handler/UdpHandler.php

View check run for this annotation

Codecov / codecov/patch

src/Handler/UdpHandler.php#L28

Added line #L28 was not covered by tests
{
$socket = $this->getSocket();

Check warning on line 30 in src/Handler/UdpHandler.php

View check run for this annotation

Codecov / codecov/patch

src/Handler/UdpHandler.php#L30

Added line #L30 was not covered by tests

$data = json_encode($variable);
if (!socket_sendto($socket, $data, strlen($data), 0, $this->host, $this->port)) {
throw new RuntimeException(
sprintf(
'Could not send a dump to %s:%d',
$this->host,
$this->port,
)
);

Check warning on line 40 in src/Handler/UdpHandler.php

View check run for this annotation

Codecov / codecov/patch

src/Handler/UdpHandler.php#L32-L40

Added lines #L32 - L40 were not covered by tests
}
}

/**
* @throws RuntimeException When a connection cannot be opened.
*/
private function getSocket(): Socket

Check warning on line 47 in src/Handler/UdpHandler.php

View check run for this annotation

Codecov / codecov/patch

src/Handler/UdpHandler.php#L47

Added line #L47 was not covered by tests
{
if ($this->socket === null) {
$this->socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
if (!$this->socket) {
throw new RuntimeException('Cannot create a UDP socket connection.');

Check warning on line 52 in src/Handler/UdpHandler.php

View check run for this annotation

Codecov / codecov/patch

src/Handler/UdpHandler.php#L49-L52

Added lines #L49 - L52 were not covered by tests
}
}
return $this->socket;

Check warning on line 55 in src/Handler/UdpHandler.php

View check run for this annotation

Codecov / codecov/patch

src/Handler/UdpHandler.php#L55

Added line #L55 was not covered by tests
}
}