comparison vendor/guzzlehttp/promises/src/TaskQueue.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
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 }