You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
1.6 KiB

3 years ago
  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. public function getProcess()
  34. {
  35. return $this->process;
  36. }
  37. public function isGeneralTimeout()
  38. {
  39. return self::TYPE_GENERAL === $this->timeoutType;
  40. }
  41. public function isIdleTimeout()
  42. {
  43. return self::TYPE_IDLE === $this->timeoutType;
  44. }
  45. public function getExceededTimeout()
  46. {
  47. switch ($this->timeoutType) {
  48. case self::TYPE_GENERAL:
  49. return $this->process->getTimeout();
  50. case self::TYPE_IDLE:
  51. return $this->process->getIdleTimeout();
  52. default:
  53. throw new \LogicException(sprintf('Unknown timeout type "%d".', $this->timeoutType));
  54. }
  55. }
  56. }