Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame^] | 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | * This file is part of the Symfony package. |
| 5 | * |
| 6 | * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | * |
| 8 | * For the full copyright and license information, please view the LICENSE |
| 9 | * file that was distributed with this source code. |
| 10 | */ |
| 11 | |
| 12 | namespace Symfony\Component\VarDumper\Server; |
| 13 | |
| 14 | use Psr\Log\LoggerInterface; |
| 15 | use Symfony\Component\VarDumper\Cloner\Data; |
| 16 | use Symfony\Component\VarDumper\Cloner\Stub; |
| 17 | |
| 18 | /** |
| 19 | * A server collecting Data clones sent by a ServerDumper. |
| 20 | * |
| 21 | * @author Maxime Steinhausser <maxime.steinhausser@gmail.com> |
| 22 | * |
| 23 | * @final |
| 24 | */ |
| 25 | class DumpServer |
| 26 | { |
| 27 | private $host; |
| 28 | private $socket; |
| 29 | private $logger; |
| 30 | |
| 31 | public function __construct(string $host, LoggerInterface $logger = null) |
| 32 | { |
| 33 | if (!str_contains($host, '://')) { |
| 34 | $host = 'tcp://'.$host; |
| 35 | } |
| 36 | |
| 37 | $this->host = $host; |
| 38 | $this->logger = $logger; |
| 39 | } |
| 40 | |
| 41 | public function start(): void |
| 42 | { |
| 43 | if (!$this->socket = stream_socket_server($this->host, $errno, $errstr)) { |
| 44 | throw new \RuntimeException(sprintf('Server start failed on "%s": ', $this->host).$errstr.' '.$errno); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | public function listen(callable $callback): void |
| 49 | { |
| 50 | if (null === $this->socket) { |
| 51 | $this->start(); |
| 52 | } |
| 53 | |
| 54 | foreach ($this->getMessages() as $clientId => $message) { |
| 55 | if ($this->logger) { |
| 56 | $this->logger->info('Received a payload from client {clientId}', ['clientId' => $clientId]); |
| 57 | } |
| 58 | |
| 59 | $payload = @unserialize(base64_decode($message), ['allowed_classes' => [Data::class, Stub::class]]); |
| 60 | |
| 61 | // Impossible to decode the message, give up. |
| 62 | if (false === $payload) { |
| 63 | if ($this->logger) { |
| 64 | $this->logger->warning('Unable to decode a message from {clientId} client.', ['clientId' => $clientId]); |
| 65 | } |
| 66 | |
| 67 | continue; |
| 68 | } |
| 69 | |
| 70 | if (!\is_array($payload) || \count($payload) < 2 || !$payload[0] instanceof Data || !\is_array($payload[1])) { |
| 71 | if ($this->logger) { |
| 72 | $this->logger->warning('Invalid payload from {clientId} client. Expected an array of two elements (Data $data, array $context)', ['clientId' => $clientId]); |
| 73 | } |
| 74 | |
| 75 | continue; |
| 76 | } |
| 77 | |
| 78 | [$data, $context] = $payload; |
| 79 | |
| 80 | $callback($data, $context, $clientId); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | public function getHost(): string |
| 85 | { |
| 86 | return $this->host; |
| 87 | } |
| 88 | |
| 89 | private function getMessages(): iterable |
| 90 | { |
| 91 | $sockets = [(int) $this->socket => $this->socket]; |
| 92 | $write = []; |
| 93 | |
| 94 | while (true) { |
| 95 | $read = $sockets; |
| 96 | stream_select($read, $write, $write, null); |
| 97 | |
| 98 | foreach ($read as $stream) { |
| 99 | if ($this->socket === $stream) { |
| 100 | $stream = stream_socket_accept($this->socket); |
| 101 | $sockets[(int) $stream] = $stream; |
| 102 | } elseif (feof($stream)) { |
| 103 | unset($sockets[(int) $stream]); |
| 104 | fclose($stream); |
| 105 | } else { |
| 106 | yield (int) $stream => fgets($stream); |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | } |