vendor/guzzlehttp/promises/src/TaskQueue.php line 52

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