Verzeichnisstruktur phpBB-3.3.15


Veröffentlicht
28.08.2024

So funktioniert es


Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück

Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis.
Auf den Verzeichnisnamen klicken, dies zeigt nur das Verzeichnis mit Inhalt an

(Beispiel Datei-Icons)

Auf das Icon klicken um den Quellcode anzuzeigen

Utils.php

Zuletzt modifiziert: 02.04.2025, 15:03 - Dateigröße: 8.51 KiB


001  <?php
002   
003  namespace GuzzleHttp\Promise;
004   
005  final class Utils
006  {
007      /**
008       * Get the global task queue used for promise resolution.
009       *
010       * This task queue MUST be run in an event loop in order for promises to be
011       * settled asynchronously. It will be automatically run when synchronously
012       * waiting on a promise.
013       *
014       * <code>
015       * while ($eventLoop->isRunning()) {
016       *     GuzzleHttp\Promise\Utils::queue()->run();
017       * }
018       * </code>
019       *
020       * @param TaskQueueInterface $assign Optionally specify a new queue instance.
021       *
022       * @return TaskQueueInterface
023       */
024      public static function queue(TaskQueueInterface $assign = null)
025      {
026          static $queue;
027   
028          if ($assign) {
029              $queue = $assign;
030          } elseif (!$queue) {
031              $queue = new TaskQueue();
032          }
033   
034          return $queue;
035      }
036   
037      /**
038       * Adds a function to run in the task queue when it is next `run()` and
039       * returns a promise that is fulfilled or rejected with the result.
040       *
041       * @param callable $task Task function to run.
042       *
043       * @return PromiseInterface
044       */
045      public static function task(callable $task)
046      {
047          $queue = self::queue();
048          $promise = new Promise([$queue, 'run']);
049          $queue->add(function () use ($task, $promise) {
050              try {
051                  if (Is::pending($promise)) {
052                      $promise->resolve($task());
053                  }
054              } catch (\Throwable $e) {
055                  $promise->reject($e);
056              } catch (\Exception $e) {
057                  $promise->reject($e);
058              }
059          });
060   
061          return $promise;
062      }
063   
064      /**
065       * Synchronously waits on a promise to resolve and returns an inspection
066       * state array.
067       *
068       * Returns a state associative array containing a "state" key mapping to a
069       * valid promise state. If the state of the promise is "fulfilled", the
070       * array will contain a "value" key mapping to the fulfilled value of the
071       * promise. If the promise is rejected, the array will contain a "reason"
072       * key mapping to the rejection reason of the promise.
073       *
074       * @param PromiseInterface $promise Promise or value.
075       *
076       * @return array
077       */
078      public static function inspect(PromiseInterface $promise)
079      {
080          try {
081              return [
082                  'state' => PromiseInterface::FULFILLED,
083                  'value' => $promise->wait()
084              ];
085          } catch (RejectionException $e) {
086              return ['state' => PromiseInterface::REJECTED, 'reason' => $e->getReason()];
087          } catch (\Throwable $e) {
088              return ['state' => PromiseInterface::REJECTED, 'reason' => $e];
089          } catch (\Exception $e) {
090              return ['state' => PromiseInterface::REJECTED, 'reason' => $e];
091          }
092      }
093   
094      /**
095       * Waits on all of the provided promises, but does not unwrap rejected
096       * promises as thrown exception.
097       *
098       * Returns an array of inspection state arrays.
099       *
100       * @see inspect for the inspection state array format.
101       *
102       * @param PromiseInterface[] $promises Traversable of promises to wait upon.
103       *
104       * @return array
105       */
106      public static function inspectAll($promises)
107      {
108          $results = [];
109          foreach ($promises as $key => $promise) {
110              $results[$key] = self::inspect($promise);
111          }
112   
113          return $results;
114      }
115   
116      /**
117       * Waits on all of the provided promises and returns the fulfilled values.
118       *
119       * Returns an array that contains the value of each promise (in the same
120       * order the promises were provided). An exception is thrown if any of the
121       * promises are rejected.
122       *
123       * @param iterable<PromiseInterface> $promises Iterable of PromiseInterface objects to wait on.
124       *
125       * @return array
126       *
127       * @throws \Exception on error
128       * @throws \Throwable on error in PHP >=7
129       */
130      public static function unwrap($promises)
131      {
132          $results = [];
133          foreach ($promises as $key => $promise) {
134              $results[$key] = $promise->wait();
135          }
136   
137          return $results;
138      }
139   
140      /**
141       * Given an array of promises, return a promise that is fulfilled when all
142       * the items in the array are fulfilled.
143       *
144       * The promise's fulfillment value is an array with fulfillment values at
145       * respective positions to the original array. If any promise in the array
146       * rejects, the returned promise is rejected with the rejection reason.
147       *
148       * @param mixed $promises  Promises or values.
149       * @param bool  $recursive If true, resolves new promises that might have been added to the stack during its own resolution.
150       *
151       * @return PromiseInterface
152       */
153      public static function all($promises, $recursive = false)
154      {
155          $results = [];
156          $promise = Each::of(
157              $promises,
158              function ($value, $idx) use (&$results) {
159                  $results[$idx] = $value;
160              },
161              function ($reason, $idx, Promise $aggregate) {
162                  $aggregate->reject($reason);
163              }
164          )->then(function () use (&$results) {
165              ksort($results);
166              return $results;
167          });
168   
169          if (true === $recursive) {
170              $promise = $promise->then(function ($results) use ($recursive, &$promises) {
171                  foreach ($promises as $promise) {
172                      if (Is::pending($promise)) {
173                          return self::all($promises, $recursive);
174                      }
175                  }
176                  return $results;
177              });
178          }
179   
180          return $promise;
181      }
182   
183      /**
184       * Initiate a competitive race between multiple promises or values (values
185       * will become immediately fulfilled promises).
186       *
187       * When count amount of promises have been fulfilled, the returned promise
188       * is fulfilled with an array that contains the fulfillment values of the
189       * winners in order of resolution.
190       *
191       * This promise is rejected with a {@see AggregateException} if the number
192       * of fulfilled promises is less than the desired $count.
193       *
194       * @param int   $count    Total number of promises.
195       * @param mixed $promises Promises or values.
196       *
197       * @return PromiseInterface
198       */
199      public static function some($count, $promises)
200      {
201          $results = [];
202          $rejections = [];
203   
204          return Each::of(
205              $promises,
206              function ($value, $idx, PromiseInterface $p) use (&$results, $count) {
207                  if (Is::settled($p)) {
208                      return;
209                  }
210                  $results[$idx] = $value;
211                  if (count($results) >= $count) {
212                      $p->resolve(null);
213                  }
214              },
215              function ($reason) use (&$rejections) {
216                  $rejections[] = $reason;
217              }
218          )->then(
219              function () use (&$results, &$rejections, $count) {
220                  if (count($results) !== $count) {
221                      throw new AggregateException(
222                          'Not enough promises to fulfill count',
223                          $rejections
224                      );
225                  }
226                  ksort($results);
227                  return array_values($results);
228              }
229          );
230      }
231   
232      /**
233       * Like some(), with 1 as count. However, if the promise fulfills, the
234       * fulfillment value is not an array of 1 but the value directly.
235       *
236       * @param mixed $promises Promises or values.
237       *
238       * @return PromiseInterface
239       */
240      public static function any($promises)
241      {
242          return self::some(1, $promises)->then(function ($values) {
243              return $values[0];
244          });
245      }
246   
247      /**
248       * Returns a promise that is fulfilled when all of the provided promises have
249       * been fulfilled or rejected.
250       *
251       * The returned promise is fulfilled with an array of inspection state arrays.
252       *
253       * @see inspect for the inspection state array format.
254       *
255       * @param mixed $promises Promises or values.
256       *
257       * @return PromiseInterface
258       */
259      public static function settle($promises)
260      {
261          $results = [];
262   
263          return Each::of(
264              $promises,
265              function ($value, $idx) use (&$results) {
266                  $results[$idx] = ['state' => PromiseInterface::FULFILLED, 'value' => $value];
267              },
268              function ($reason, $idx) use (&$results) {
269                  $results[$idx] = ['state' => PromiseInterface::REJECTED, 'reason' => $reason];
270              }
271          )->then(function () use (&$results) {
272              ksort($results);
273              return $results;
274          });
275      }
276  }
277