CliRequest.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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\Console\Debug;
  11. use Symfony\Component\Console\Command\TraceableCommand;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. /**
  15. * @internal
  16. */
  17. final class CliRequest extends Request
  18. {
  19. public function __construct(
  20. public readonly TraceableCommand $command,
  21. ) {
  22. parent::__construct(
  23. attributes: ['_controller' => \get_class($command->command), '_virtual_type' => 'command'],
  24. server: $_SERVER,
  25. );
  26. }
  27. // Methods below allow to populate a profile, thus enable search and filtering
  28. public function getUri(): string
  29. {
  30. if ($this->server->has('SYMFONY_CLI_BINARY_NAME')) {
  31. $binary = $this->server->get('SYMFONY_CLI_BINARY_NAME').' console';
  32. } else {
  33. $binary = $this->server->get('argv')[0];
  34. }
  35. return $binary.' '.$this->command->input;
  36. }
  37. public function getMethod(): string
  38. {
  39. return $this->command->isInteractive ? 'INTERACTIVE' : 'BATCH';
  40. }
  41. public function getResponse(): Response
  42. {
  43. return new class($this->command->exitCode) extends Response {
  44. public function __construct(private readonly int $exitCode)
  45. {
  46. parent::__construct();
  47. }
  48. public function getStatusCode(): int
  49. {
  50. return $this->exitCode;
  51. }
  52. };
  53. }
  54. public function getClientIp(): string
  55. {
  56. $application = $this->command->getApplication();
  57. return $application->getName().' '.$application->getVersion();
  58. }
  59. }