vendor/symfony/http-kernel/EventListener/DumpListener.php line 39

  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\HttpKernel\EventListener;
  11. use Symfony\Component\Console\ConsoleEvents;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. use Symfony\Component\VarDumper\Cloner\ClonerInterface;
  14. use Symfony\Component\VarDumper\Dumper\DataDumperInterface;
  15. use Symfony\Component\VarDumper\Server\Connection;
  16. use Symfony\Component\VarDumper\VarDumper;
  17. /**
  18.  * Configures dump() handler.
  19.  *
  20.  * @author Nicolas Grekas <p@tchwork.com>
  21.  */
  22. class DumpListener implements EventSubscriberInterface
  23. {
  24.     private ClonerInterface $cloner;
  25.     private DataDumperInterface $dumper;
  26.     private ?Connection $connection;
  27.     public function __construct(ClonerInterface $clonerDataDumperInterface $dumper, ?Connection $connection null)
  28.     {
  29.         $this->cloner $cloner;
  30.         $this->dumper $dumper;
  31.         $this->connection $connection;
  32.     }
  33.     /**
  34.      * @return void
  35.      */
  36.     public function configure()
  37.     {
  38.         $cloner $this->cloner;
  39.         $dumper $this->dumper;
  40.         $connection $this->connection;
  41.         VarDumper::setHandler(static function ($var, ?string $label null) use ($cloner$dumper$connection) {
  42.             $data $cloner->cloneVar($var);
  43.             if (null !== $label) {
  44.                 $data $data->withContext(['label' => $label]);
  45.             }
  46.             if (!$connection || !$connection->write($data)) {
  47.                 $dumper->dump($data);
  48.             }
  49.         });
  50.     }
  51.     public static function getSubscribedEvents(): array
  52.     {
  53.         if (!class_exists(ConsoleEvents::class)) {
  54.             return [];
  55.         }
  56.         // Register early to have a working dump() as early as possible
  57.         return [ConsoleEvents::COMMAND => ['configure'1024]];
  58.     }
  59. }