Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

67 linhas
1.9 KiB

  1. <?php
  2. namespace GuzzleHttp\Promise;
  3. /**
  4. * A task queue that executes tasks in a FIFO order.
  5. *
  6. * This task queue class is used to settle promises asynchronously and
  7. * maintains a constant stack size. You can use the task queue asynchronously
  8. * by calling the `run()` function of the global task queue in an event loop.
  9. *
  10. * GuzzleHttp\Promise\queue()->run();
  11. */
  12. class TaskQueue implements TaskQueueInterface
  13. {
  14. private $enableShutdown = true;
  15. private $queue = [];
  16. public function __construct($withShutdown = true)
  17. {
  18. if ($withShutdown) {
  19. register_shutdown_function(function () {
  20. if ($this->enableShutdown) {
  21. // Only run the tasks if an E_ERROR didn't occur.
  22. $err = error_get_last();
  23. if (!$err || ($err['type'] ^ E_ERROR)) {
  24. $this->run();
  25. }
  26. }
  27. });
  28. }
  29. }
  30. public function isEmpty()
  31. {
  32. return !$this->queue;
  33. }
  34. public function add(callable $task)
  35. {
  36. $this->queue[] = $task;
  37. }
  38. public function run()
  39. {
  40. /** @var callable $task */
  41. while ($task = array_shift($this->queue)) {
  42. $task();
  43. }
  44. }
  45. /**
  46. * The task queue will be run and exhausted by default when the process
  47. * exits IFF the exit is not the result of a PHP E_ERROR error.
  48. *
  49. * You can disable running the automatic shutdown of the queue by calling
  50. * this function. If you disable the task queue shutdown process, then you
  51. * MUST either run the task queue (as a result of running your event loop
  52. * or manually using the run() method) or wait on each outstanding promise.
  53. *
  54. * Note: This shutdown will occur before any destructors are triggered.
  55. */
  56. public function disableShutdown()
  57. {
  58. $this->enableShutdown = false;
  59. }
  60. }