CliRequest.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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' => $command->command::class, '_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(int $exitCode)
  45. {
  46. parent::__construct();
  47. // getStatusCode() is final and setStatusCode() rejects an exit code
  48. $this->statusCode = $exitCode;
  49. }
  50. };
  51. }
  52. public function getClientIp(): string
  53. {
  54. $application = $this->command->getApplication();
  55. return $application->getName().' '.$application->getVersion();
  56. }
  57. }