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 Symfony\Component\VarDumper\Cloner\Data; |
| 15 | use Symfony\Component\VarDumper\Dumper\ContextProvider\ContextProviderInterface; |
| 16 | |
| 17 | /** |
| 18 | * Forwards serialized Data clones to a server. |
| 19 | * |
| 20 | * @author Maxime Steinhausser <maxime.steinhausser@gmail.com> |
| 21 | */ |
| 22 | class Connection |
| 23 | { |
| 24 | private $host; |
| 25 | private $contextProviders; |
| 26 | private $socket; |
| 27 | |
| 28 | /** |
| 29 | * @param string $host The server host |
| 30 | * @param ContextProviderInterface[] $contextProviders Context providers indexed by context name |
| 31 | */ |
| 32 | public function __construct(string $host, array $contextProviders = []) |
| 33 | { |
| 34 | if (!str_contains($host, '://')) { |
| 35 | $host = 'tcp://'.$host; |
| 36 | } |
| 37 | |
| 38 | $this->host = $host; |
| 39 | $this->contextProviders = $contextProviders; |
| 40 | } |
| 41 | |
| 42 | public function getContextProviders(): array |
| 43 | { |
| 44 | return $this->contextProviders; |
| 45 | } |
| 46 | |
| 47 | public function write(Data $data): bool |
| 48 | { |
| 49 | $socketIsFresh = !$this->socket; |
| 50 | if (!$this->socket = $this->socket ?: $this->createSocket()) { |
| 51 | return false; |
| 52 | } |
| 53 | |
| 54 | $context = ['timestamp' => microtime(true)]; |
| 55 | foreach ($this->contextProviders as $name => $provider) { |
| 56 | $context[$name] = $provider->getContext(); |
| 57 | } |
| 58 | $context = array_filter($context); |
| 59 | $encodedPayload = base64_encode(serialize([$data, $context]))."\n"; |
| 60 | |
| 61 | set_error_handler([self::class, 'nullErrorHandler']); |
| 62 | try { |
| 63 | if (-1 !== stream_socket_sendto($this->socket, $encodedPayload)) { |
| 64 | return true; |
| 65 | } |
| 66 | if (!$socketIsFresh) { |
| 67 | stream_socket_shutdown($this->socket, \STREAM_SHUT_RDWR); |
| 68 | fclose($this->socket); |
| 69 | $this->socket = $this->createSocket(); |
| 70 | } |
| 71 | if (-1 !== stream_socket_sendto($this->socket, $encodedPayload)) { |
| 72 | return true; |
| 73 | } |
| 74 | } finally { |
| 75 | restore_error_handler(); |
| 76 | } |
| 77 | |
| 78 | return false; |
| 79 | } |
| 80 | |
| 81 | private static function nullErrorHandler(int $t, string $m) |
| 82 | { |
| 83 | // no-op |
| 84 | } |
| 85 | |
| 86 | private function createSocket() |
| 87 | { |
| 88 | set_error_handler([self::class, 'nullErrorHandler']); |
| 89 | try { |
| 90 | return stream_socket_client($this->host, $errno, $errstr, 3, \STREAM_CLIENT_CONNECT | \STREAM_CLIENT_ASYNC_CONNECT); |
| 91 | } finally { |
| 92 | restore_error_handler(); |
| 93 | } |
| 94 | } |
| 95 | } |