ProcessTimedOutException.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. private Process $process;
  22. private int $timeoutType;
  23. public function __construct(Process $process, int $timeoutType)
  24. {
  25. $this->process = $process;
  26. $this->timeoutType = $timeoutType;
  27. parent::__construct(sprintf(
  28. 'The process "%s" exceeded the timeout of %s seconds.',
  29. $process->getCommandLine(),
  30. $this->getExceededTimeout()
  31. ));
  32. }
  33. public function getProcess(): Process
  34. {
  35. return $this->process;
  36. }
  37. public function isGeneralTimeout(): bool
  38. {
  39. return self::TYPE_GENERAL === $this->timeoutType;
  40. }
  41. public function isIdleTimeout(): bool
  42. {
  43. return self::TYPE_IDLE === $this->timeoutType;
  44. }
  45. public function getExceededTimeout(): ?float
  46. {
  47. return match ($this->timeoutType) {
  48. self::TYPE_GENERAL => $this->process->getTimeout(),
  49. self::TYPE_IDLE => $this->process->getIdleTimeout(),
  50. default => throw new \LogicException(sprintf('Unknown timeout type "%d".', $this->timeoutType)),
  51. };
  52. }
  53. }