ServerDumper.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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\Dumper;
  11. use Symfony\Component\VarDumper\Cloner\Data;
  12. use Symfony\Component\VarDumper\Dumper\ContextProvider\ContextProviderInterface;
  13. use Symfony\Component\VarDumper\Server\Connection;
  14. /**
  15. * ServerDumper forwards serialized Data clones to a server.
  16. *
  17. * @author Maxime Steinhausser <maxime.steinhausser@gmail.com>
  18. */
  19. class ServerDumper implements DataDumperInterface
  20. {
  21. private Connection $connection;
  22. private ?DataDumperInterface $wrappedDumper;
  23. /**
  24. * @param string $host The server host
  25. * @param DataDumperInterface|null $wrappedDumper A wrapped instance used whenever we failed contacting the server
  26. * @param ContextProviderInterface[] $contextProviders Context providers indexed by context name
  27. */
  28. public function __construct(string $host, ?DataDumperInterface $wrappedDumper = null, array $contextProviders = [])
  29. {
  30. $this->connection = new Connection($host, $contextProviders);
  31. $this->wrappedDumper = $wrappedDumper;
  32. }
  33. public function getContextProviders(): array
  34. {
  35. return $this->connection->getContextProviders();
  36. }
  37. public function dump(Data $data): ?string
  38. {
  39. if (!$this->connection->write($data) && $this->wrappedDumper) {
  40. return $this->wrappedDumper->dump($data);
  41. }
  42. return null;
  43. }
  44. }