DumpServer.php 3.2 KB

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