You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
3.2 KiB

3 years ago
  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. }