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

Pool.php

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


001  <?php
002  namespace GuzzleHttp;
003   
004  use GuzzleHttp\Promise\EachPromise;
005  use GuzzleHttp\Promise\PromiseInterface;
006  use GuzzleHttp\Promise\PromisorInterface;
007  use Psr\Http\Message\RequestInterface;
008   
009  /**
010   * Sends an iterator of requests concurrently using a capped pool size.
011   *
012   * The pool will read from an iterator until it is cancelled or until the
013   * iterator is consumed. When a request is yielded, the request is sent after
014   * applying the "request_options" request options (if provided in the ctor).
015   *
016   * When a function is yielded by the iterator, the function is provided the
017   * "request_options" array that should be merged on top of any existing
018   * options, and the function MUST then return a wait-able promise.
019   */
020  class Pool implements PromisorInterface
021  {
022      /** @var EachPromise */
023      private $each;
024   
025      /**
026       * @param ClientInterface $client   Client used to send the requests.
027       * @param array|\Iterator $requests Requests or functions that return
028       *                                  requests to send concurrently.
029       * @param array           $config   Associative array of options
030       *     - concurrency: (int) Maximum number of requests to send concurrently
031       *     - options: Array of request options to apply to each request.
032       *     - fulfilled: (callable) Function to invoke when a request completes.
033       *     - rejected: (callable) Function to invoke when a request is rejected.
034       */
035      public function __construct(
036          ClientInterface $client,
037          $requests,
038          array $config = []
039      ) {
040          // Backwards compatibility.
041          if (isset($config['pool_size'])) {
042              $config['concurrency'] = $config['pool_size'];
043          } elseif (!isset($config['concurrency'])) {
044              $config['concurrency'] = 25;
045          }
046   
047          if (isset($config['options'])) {
048              $opts = $config['options'];
049              unset($config['options']);
050          } else {
051              $opts = [];
052          }
053   
054          $iterable = \GuzzleHttp\Promise\iter_for($requests);
055          $requests = function () use ($iterable, $client, $opts) {
056              foreach ($iterable as $key => $rfn) {
057                  if ($rfn instanceof RequestInterface) {
058                      yield $key => $client->sendAsync($rfn, $opts);
059                  } elseif (is_callable($rfn)) {
060                      yield $key => $rfn($opts);
061                  } else {
062                      throw new \InvalidArgumentException('Each value yielded by '
063                          . 'the iterator must be a Psr7\Http\Message\RequestInterface '
064                          . 'or a callable that returns a promise that fulfills '
065                          . 'with a Psr7\Message\Http\ResponseInterface object.');
066                  }
067              }
068          };
069   
070          $this->each = new EachPromise($requests(), $config);
071      }
072   
073      /**
074       * Get promise
075       *
076       * @return PromiseInterface
077       */
078      public function promise()
079      {
080          return $this->each->promise();
081      }
082   
083      /**
084       * Sends multiple requests concurrently and returns an array of responses
085       * and exceptions that uses the same ordering as the provided requests.
086       *
087       * IMPORTANT: This method keeps every request and response in memory, and
088       * as such, is NOT recommended when sending a large number or an
089       * indeterminate number of requests concurrently.
090       *
091       * @param ClientInterface $client   Client used to send the requests
092       * @param array|\Iterator $requests Requests to send concurrently.
093       * @param array           $options  Passes through the options available in
094       *                                  {@see GuzzleHttp\Pool::__construct}
095       *
096       * @return array Returns an array containing the response or an exception
097       *               in the same order that the requests were sent.
098       * @throws \InvalidArgumentException if the event format is incorrect.
099       */
100      public static function batch(
101          ClientInterface $client,
102          $requests,
103          array $options = []
104      ) {
105          $res = [];
106          self::cmpCallback($options, 'fulfilled', $res);
107          self::cmpCallback($options, 'rejected', $res);
108          $pool = new static($client, $requests, $options);
109          $pool->promise()->wait();
110          ksort($res);
111   
112          return $res;
113      }
114   
115      /**
116       * Execute callback(s)
117       *
118       * @return void
119       */
120      private static function cmpCallback(array &$options, $name, array &$results)
121      {
122          if (!isset($options[$name])) {
123              $options[$name] = function ($v, $k) use (&$results) {
124                  $results[$k] = $v;
125              };
126          } else {
127              $currentFn = $options[$name];
128              $options[$name] = function ($v, $k) use (&$results, $currentFn) {
129                  $currentFn($v, $k);
130                  $results[$k] = $v;
131              };
132          }
133      }
134  }
135