var-dump-server 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #!/usr/bin/env php
  2. <?php
  3. /*
  4. * This file is part of the Symfony package.
  5. *
  6. * (c) Fabien Potencier <fabien@symfony.com>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. /**
  12. * Starts a dump server to collect and output dumps on a single place with multiple formats support.
  13. *
  14. * @author Maxime Steinhausser <maxime.steinhausser@gmail.com>
  15. */
  16. use Psr\Log\LoggerInterface;
  17. use Symfony\Component\Console\Application;
  18. use Symfony\Component\Console\Input\ArgvInput;
  19. use Symfony\Component\Console\Input\InputOption;
  20. use Symfony\Component\Console\Logger\ConsoleLogger;
  21. use Symfony\Component\Console\Output\ConsoleOutput;
  22. use Symfony\Component\VarDumper\Command\ServerDumpCommand;
  23. use Symfony\Component\VarDumper\Server\DumpServer;
  24. function includeIfExists(string $file): bool
  25. {
  26. return file_exists($file) && include $file;
  27. }
  28. if (
  29. !includeIfExists(__DIR__ . '/../../../../autoload.php') &&
  30. !includeIfExists(__DIR__ . '/../../vendor/autoload.php') &&
  31. !includeIfExists(__DIR__ . '/../../../../../../vendor/autoload.php')
  32. ) {
  33. fwrite(STDERR, 'Install dependencies using Composer.'.PHP_EOL);
  34. exit(1);
  35. }
  36. if (!class_exists(Application::class)) {
  37. fwrite(STDERR, 'You need the "symfony/console" component in order to run the VarDumper server.'.PHP_EOL);
  38. exit(1);
  39. }
  40. $input = new ArgvInput();
  41. $output = new ConsoleOutput();
  42. $defaultHost = '127.0.0.1:9912';
  43. $host = $input->getParameterOption(['--host'], $_SERVER['VAR_DUMPER_SERVER'] ?? $defaultHost, true);
  44. $logger = interface_exists(LoggerInterface::class) ? new ConsoleLogger($output->getErrorOutput()) : null;
  45. $app = new Application();
  46. $app->getDefinition()->addOption(
  47. new InputOption('--host', null, InputOption::VALUE_REQUIRED, 'The address the server should listen to', $defaultHost)
  48. );
  49. $app->add($command = new ServerDumpCommand(new DumpServer($host, $logger)))
  50. ->getApplication()
  51. ->setDefaultCommand($command->getName(), true)
  52. ->run($input, $output)
  53. ;