ProcessTimedOutException.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. const TYPE_GENERAL = 1;
  20. const TYPE_IDLE = 2;
  21. private $process;
  22. private $timeoutType;
  23. public function __construct(Process $process, $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 $this->timeoutType === self::TYPE_GENERAL;
  40. }
  41. public function isIdleTimeout()
  42. {
  43. return $this->timeoutType === self::TYPE_IDLE;
  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. }