ProcessTimedOutException.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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\Process\Exception;
  11. use Symfony\Component\Process\Process;
  12. /**
  13. * Exception that is thrown when a process times out.
  14. *
  15. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  16. */
  17. class ProcessTimedOutException extends RuntimeException
  18. {
  19. public const TYPE_GENERAL = 1;
  20. public const TYPE_IDLE = 2;
  21. public function __construct(
  22. private Process $process,
  23. private int $timeoutType,
  24. ) {
  25. parent::__construct(\sprintf(
  26. 'The process "%s" exceeded the timeout of %s seconds.',
  27. $process->getCommandLine(),
  28. $this->getExceededTimeout()
  29. ));
  30. }
  31. public function getProcess(): Process
  32. {
  33. return $this->process;
  34. }
  35. public function isGeneralTimeout(): bool
  36. {
  37. return self::TYPE_GENERAL === $this->timeoutType;
  38. }
  39. public function isIdleTimeout(): bool
  40. {
  41. return self::TYPE_IDLE === $this->timeoutType;
  42. }
  43. public function getExceededTimeout(): ?float
  44. {
  45. return match ($this->timeoutType) {
  46. self::TYPE_GENERAL => $this->process->getTimeout(),
  47. self::TYPE_IDLE => $this->process->getIdleTimeout(),
  48. default => throw new \LogicException(\sprintf('Unknown timeout type "%d".', $this->timeoutType)),
  49. };
  50. }
  51. }