PluginProbe
Media Cloud Sync / 1.3.11
Media Cloud Sync v1.3.11
1.4.0 1.3.12 1.3.11 1.3.10 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.2.0 1.2.10 1.2.11 1.2.12 1.2.13 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 All 34 releases
media-cloud-sync / includes / sdk / s3 / GuzzleHttp / Promise / TaskQueue.php

TaskQueue.php in Media Cloud Sync 1.3.11, at includes/sdk/s3/GuzzleHttp/Promise/TaskQueue.php

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