Dumper.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. namespace BeyondCode\DumpServer;
  3. use Symfony\Component\VarDumper\Cloner\VarCloner;
  4. use Symfony\Component\VarDumper\Dumper\CliDumper;
  5. use Symfony\Component\VarDumper\Dumper\HtmlDumper;
  6. use Symfony\Component\VarDumper\Server\Connection;
  7. class Dumper
  8. {
  9. /**
  10. * The connection.
  11. *
  12. * @var \Symfony\Component\VarDumper\Server\Connection|null
  13. */
  14. private $connection;
  15. /**
  16. * Dumper constructor.
  17. *
  18. * @param \Symfony\Component\VarDumper\Server\Connection|null $connection
  19. * @return void
  20. */
  21. public function __construct(Connection $connection = null)
  22. {
  23. $this->connection = $connection;
  24. }
  25. /**
  26. * Dump a value with elegance.
  27. *
  28. * @param mixed $value
  29. * @return void
  30. */
  31. public function dump($value)
  32. {
  33. if (class_exists(CliDumper::class)) {
  34. $data = (new VarCloner)->cloneVar($value);
  35. if ($this->connection === null || $this->connection->write($data) === false) {
  36. $dumper = in_array(PHP_SAPI, ['cli', 'phpdbg']) ? new CliDumper : new HtmlDumper;
  37. $dumper->dump($data);
  38. }
  39. } else {
  40. var_dump($value);
  41. }
  42. }
  43. }