PluginProbe
Metricool – Social media and site statistics / 2.1.1
Metricool – Social media and site statistics v2.1.1
2.1.1 2.1.0 2.0.2 2.0.1 2.0.0 1.27 trunk
metricool / vendor / _scoped / guzzlehttp / promises / src / TaskQueue.php

TaskQueue.php in Metricool – Social media and site statistics 2.1.1, at vendor/_scoped/guzzlehttp/promises/src/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 Metricool\Vendor\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