Canary.php 852 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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\HttpClient\Internal;
  11. /**
  12. * @author Nicolas Grekas <p@tchwork.com>
  13. *
  14. * @internal
  15. */
  16. final class Canary
  17. {
  18. /**
  19. * @var \Closure
  20. */
  21. private $canceller;
  22. /**
  23. * @param \Closure $canceller
  24. */
  25. public function __construct($canceller)
  26. {
  27. $this->canceller = $canceller;
  28. }
  29. public function cancel()
  30. {
  31. if (isset($this->canceller)) {
  32. $canceller = $this->canceller;
  33. unset($this->canceller);
  34. $canceller();
  35. }
  36. }
  37. public function __destruct()
  38. {
  39. $this->cancel();
  40. }
  41. }