DumpServer.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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": %s %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. $payload = @unserialize(base64_decode($message), ['allowed_classes' => [Data::class, Stub::class]]);
  47. // Impossible to decode the message, give up.
  48. if (false === $payload) {
  49. if ($this->logger) {
  50. $this->logger->warning('Unable to decode a message from {clientId} client.', ['clientId' => $clientId]);
  51. }
  52. continue;
  53. }
  54. if (!\is_array($payload) || \count($payload) < 2 || !$payload[0] instanceof Data || !\is_array($payload[1])) {
  55. if ($this->logger) {
  56. $this->logger->warning('Invalid payload from {clientId} client. Expected an array of two elements (Data $data, array $context)', ['clientId' => $clientId]);
  57. }
  58. continue;
  59. }
  60. list($data, $context) = $payload;
  61. $callback($data, $context, $clientId);
  62. }
  63. }
  64. public function getHost(): string
  65. {
  66. return $this->host;
  67. }
  68. private function getMessages(): iterable
  69. {
  70. $sockets = [(int) $this->socket => $this->socket];
  71. $write = [];
  72. while (true) {
  73. $read = $sockets;
  74. stream_select($read, $write, $write, null);
  75. foreach ($read as $stream) {
  76. if ($this->socket === $stream) {
  77. $stream = stream_socket_accept($this->socket);
  78. $sockets[(int) $stream] = $stream;
  79. } elseif (feof($stream)) {
  80. unset($sockets[(int) $stream]);
  81. fclose($stream);
  82. } else {
  83. yield (int) $stream => fgets($stream);
  84. }
  85. }
  86. }
  87. }
  88. }