ProcessTimedOutException.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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;
  22. private $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. /**
  34. * @return Process
  35. */
  36. public function getProcess()
  37. {
  38. return $this->process;
  39. }
  40. /**
  41. * @return bool
  42. */
  43. public function isGeneralTimeout()
  44. {
  45. return self::TYPE_GENERAL === $this->timeoutType;
  46. }
  47. /**
  48. * @return bool
  49. */
  50. public function isIdleTimeout()
  51. {
  52. return self::TYPE_IDLE === $this->timeoutType;
  53. }
  54. public function getExceededTimeout(): ?float
  55. {
  56. return match ($this->timeoutType) {
  57. self::TYPE_GENERAL => $this->process->getTimeout(),
  58. self::TYPE_IDLE => $this->process->getIdleTimeout(),
  59. default => throw new \LogicException(sprintf('Unknown timeout type "%d".', $this->timeoutType)),
  60. };
  61. }
  62. }