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

Middleware.php

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


001  <?php
002  namespace GuzzleHttp;
003   
004  use GuzzleHttp\Cookie\CookieJarInterface;
005  use GuzzleHttp\Exception\RequestException;
006  use GuzzleHttp\Promise\RejectedPromise;
007  use GuzzleHttp\Psr7;
008  use Psr\Http\Message\ResponseInterface;
009  use Psr\Log\LoggerInterface;
010   
011  /**
012   * Functions used to create and wrap handlers with handler middleware.
013   */
014  final class Middleware
015  {
016      /**
017       * Middleware that adds cookies to requests.
018       *
019       * The options array must be set to a CookieJarInterface in order to use
020       * cookies. This is typically handled for you by a client.
021       *
022       * @return callable Returns a function that accepts the next handler.
023       */
024      public static function cookies()
025      {
026          return function (callable $handler) {
027              return function ($request, array $options) use ($handler) {
028                  if (empty($options['cookies'])) {
029                      return $handler($request, $options);
030                  } elseif (!($options['cookies'] instanceof CookieJarInterface)) {
031                      throw new \InvalidArgumentException('cookies must be an instance of GuzzleHttp\Cookie\CookieJarInterface');
032                  }
033                  $cookieJar = $options['cookies'];
034                  $request = $cookieJar->withCookieHeader($request);
035                  return $handler($request, $options)
036                      ->then(
037                          function ($response) use ($cookieJar, $request) {
038                              $cookieJar->extractCookies($request, $response);
039                              return $response;
040                          }
041                      );
042              };
043          };
044      }
045   
046      /**
047       * Middleware that throws exceptions for 4xx or 5xx responses when the
048       * "http_error" request option is set to true.
049       *
050       * @return callable Returns a function that accepts the next handler.
051       */
052      public static function httpErrors()
053      {
054          return function (callable $handler) {
055              return function ($request, array $options) use ($handler) {
056                  if (empty($options['http_errors'])) {
057                      return $handler($request, $options);
058                  }
059                  return $handler($request, $options)->then(
060                      function (ResponseInterface $response) use ($request) {
061                          $code = $response->getStatusCode();
062                          if ($code < 400) {
063                              return $response;
064                          }
065                          throw RequestException::create($request, $response);
066                      }
067                  );
068              };
069          };
070      }
071   
072      /**
073       * Middleware that pushes history data to an ArrayAccess container.
074       *
075       * @param array|\ArrayAccess $container Container to hold the history (by reference).
076       *
077       * @return callable Returns a function that accepts the next handler.
078       * @throws \InvalidArgumentException if container is not an array or ArrayAccess.
079       */
080      public static function history(&$container)
081      {
082          if (!is_array($container) && !$container instanceof \ArrayAccess) {
083              throw new \InvalidArgumentException('history container must be an array or object implementing ArrayAccess');
084          }
085   
086          return function (callable $handler) use (&$container) {
087              return function ($request, array $options) use ($handler, &$container) {
088                  return $handler($request, $options)->then(
089                      function ($value) use ($request, &$container, $options) {
090                          $container[] = [
091                              'request'  => $request,
092                              'response' => $value,
093                              'error'    => null,
094                              'options'  => $options
095                          ];
096                          return $value;
097                      },
098                      function ($reason) use ($request, &$container, $options) {
099                          $container[] = [
100                              'request'  => $request,
101                              'response' => null,
102                              'error'    => $reason,
103                              'options'  => $options
104                          ];
105                          return \GuzzleHttp\Promise\rejection_for($reason);
106                      }
107                  );
108              };
109          };
110      }
111   
112      /**
113       * Middleware that invokes a callback before and after sending a request.
114       *
115       * The provided listener cannot modify or alter the response. It simply
116       * "taps" into the chain to be notified before returning the promise. The
117       * before listener accepts a request and options array, and the after
118       * listener accepts a request, options array, and response promise.
119       *
120       * @param callable $before Function to invoke before forwarding the request.
121       * @param callable $after  Function invoked after forwarding.
122       *
123       * @return callable Returns a function that accepts the next handler.
124       */
125      public static function tap(callable $before = null, callable $after = null)
126      {
127          return function (callable $handler) use ($before, $after) {
128              return function ($request, array $options) use ($handler, $before, $after) {
129                  if ($before) {
130                      $before($request, $options);
131                  }
132                  $response = $handler($request, $options);
133                  if ($after) {
134                      $after($request, $options, $response);
135                  }
136                  return $response;
137              };
138          };
139      }
140   
141      /**
142       * Middleware that handles request redirects.
143       *
144       * @return callable Returns a function that accepts the next handler.
145       */
146      public static function redirect()
147      {
148          return function (callable $handler) {
149              return new RedirectMiddleware($handler);
150          };
151      }
152   
153      /**
154       * Middleware that retries requests based on the boolean result of
155       * invoking the provided "decider" function.
156       *
157       * If no delay function is provided, a simple implementation of exponential
158       * backoff will be utilized.
159       *
160       * @param callable $decider Function that accepts the number of retries,
161       *                          a request, [response], and [exception] and
162       *                          returns true if the request is to be retried.
163       * @param callable $delay   Function that accepts the number of retries and
164       *                          returns the number of milliseconds to delay.
165       *
166       * @return callable Returns a function that accepts the next handler.
167       */
168      public static function retry(callable $decider, callable $delay = null)
169      {
170          return function (callable $handler) use ($decider, $delay) {
171              return new RetryMiddleware($decider, $handler, $delay);
172          };
173      }
174   
175      /**
176       * Middleware that logs requests, responses, and errors using a message
177       * formatter.
178       *
179       * @param LoggerInterface  $logger Logs messages.
180       * @param MessageFormatter $formatter Formatter used to create message strings.
181       * @param string           $logLevel Level at which to log requests.
182       *
183       * @return callable Returns a function that accepts the next handler.
184       */
185      public static function log(LoggerInterface $logger, MessageFormatter $formatter, $logLevel = 'info' /* \Psr\Log\LogLevel::INFO */)
186      {
187          return function (callable $handler) use ($logger, $formatter, $logLevel) {
188              return function ($request, array $options) use ($handler, $logger, $formatter, $logLevel) {
189                  return $handler($request, $options)->then(
190                      function ($response) use ($logger, $request, $formatter, $logLevel) {
191                          $message = $formatter->format($request, $response);
192                          $logger->log($logLevel, $message);
193                          return $response;
194                      },
195                      function ($reason) use ($logger, $request, $formatter) {
196                          $response = $reason instanceof RequestException
197                              ? $reason->getResponse()
198                              : null;
199                          $message = $formatter->format($request, $response, $reason);
200                          $logger->notice($message);
201                          return \GuzzleHttp\Promise\rejection_for($reason);
202                      }
203                  );
204              };
205          };
206      }
207   
208      /**
209       * This middleware adds a default content-type if possible, a default
210       * content-length or transfer-encoding header, and the expect header.
211       *
212       * @return callable
213       */
214      public static function prepareBody()
215      {
216          return function (callable $handler) {
217              return new PrepareBodyMiddleware($handler);
218          };
219      }
220   
221      /**
222       * Middleware that applies a map function to the request before passing to
223       * the next handler.
224       *
225       * @param callable $fn Function that accepts a RequestInterface and returns
226       *                     a RequestInterface.
227       * @return callable
228       */
229      public static function mapRequest(callable $fn)
230      {
231          return function (callable $handler) use ($fn) {
232              return function ($request, array $options) use ($handler, $fn) {
233                  return $handler($fn($request), $options);
234              };
235          };
236      }
237   
238      /**
239       * Middleware that applies a map function to the resolved promise's
240       * response.
241       *
242       * @param callable $fn Function that accepts a ResponseInterface and
243       *                     returns a ResponseInterface.
244       * @return callable
245       */
246      public static function mapResponse(callable $fn)
247      {
248          return function (callable $handler) use ($fn) {
249              return function ($request, array $options) use ($handler, $fn) {
250                  return $handler($request, $options)->then($fn);
251              };
252          };
253      }
254  }
255