PluginProbe
StockPack – Stock photos and AI images from Unsplash, Adobe Stock, Freepik and more / 2.4.1
StockPack – Stock photos and AI images from Unsplash, Adobe Stock, Freepik and more v2.4.1
3.7.2 3.7.1 3.7.0 3.6.0 3.6.1 3.5.2 trunk 2.0 2.1 2.2 2.3 2.4 2.4.1 2.4.2 2.4.3 2.5.0 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.7 2.6.0 2.6.1 All 73 releases
stockpack / vendor / guzzlehttp / promises / src / TaskQueue.php

TaskQueue.php in StockPack – Stock photos and AI images from Unsplash, Adobe Stock, Freepik and more 2.4.1, at vendor/guzzlehttp/promises/src/TaskQueue.php

67 lines 1.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace GuzzleHttp\Promise;
3
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\queue()->run();
12 */
13 class TaskQueue implements TaskQueueInterface
14 {
15 private $enableShutdown = true;
16 private $queue = [];
17
18 public function __construct($withShutdown = true)
19 {
20 if ($withShutdown) {
21 register_shutdown_function(function () {
22 if ($this->enableShutdown) {
23 // Only run the tasks if an E_ERROR didn't occur.
24 $err = error_get_last();
25 if (!$err || ($err['type'] ^ E_ERROR)) {
26 $this->run();
27 }
28 }
29 });
30 }
31 }
32
33 public function isEmpty()
34 {
35 return !$this->queue;
36 }
37
38 public function add(callable $task)
39 {
40 $this->queue[] = $task;
41 }
42
43 public function run()
44 {
45 /** @var callable $task */
46 while ($task = array_shift($this->queue)) {
47 $task();
48 }
49 }
50
51 /**
52 * The task queue will be run and exhausted by default when the process
53 * exits IFF the exit is not the result of a PHP E_ERROR error.
54 *
55 * You can disable running the automatic shutdown of the queue by calling
56 * this function. If you disable the task queue shutdown process, then you
57 * MUST either run the task queue (as a result of running your event loop
58 * or manually using the run() method) or wait on each outstanding promise.
59 *
60 * Note: This shutdown will occur before any destructors are triggered.
61 */
62 public function disableShutdown()
63 {
64 $this->enableShutdown = false;
65 }
66 }
67