Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/updraftp.../vendor/guzzleht.../guzzle/src
File: Middleware.php
<?php
[0] Fix | Delete
namespace GuzzleHttp;
[1] Fix | Delete
[2] Fix | Delete
use GuzzleHttp\Cookie\CookieJarInterface;
[3] Fix | Delete
use GuzzleHttp\Exception\RequestException;
[4] Fix | Delete
use GuzzleHttp\Promise\RejectedPromise;
[5] Fix | Delete
use GuzzleHttp\Psr7;
[6] Fix | Delete
use Psr\Http\Message\ResponseInterface;
[7] Fix | Delete
use Psr\Log\LoggerInterface;
[8] Fix | Delete
[9] Fix | Delete
/**
[10] Fix | Delete
* Functions used to create and wrap handlers with handler middleware.
[11] Fix | Delete
*/
[12] Fix | Delete
final class Middleware
[13] Fix | Delete
{
[14] Fix | Delete
/**
[15] Fix | Delete
* Middleware that adds cookies to requests.
[16] Fix | Delete
*
[17] Fix | Delete
* The options array must be set to a CookieJarInterface in order to use
[18] Fix | Delete
* cookies. This is typically handled for you by a client.
[19] Fix | Delete
*
[20] Fix | Delete
* @return callable Returns a function that accepts the next handler.
[21] Fix | Delete
*/
[22] Fix | Delete
public static function cookies()
[23] Fix | Delete
{
[24] Fix | Delete
return function (callable $handler) {
[25] Fix | Delete
return function ($request, array $options) use ($handler) {
[26] Fix | Delete
if (empty($options['cookies'])) {
[27] Fix | Delete
return $handler($request, $options);
[28] Fix | Delete
} elseif (!($options['cookies'] instanceof CookieJarInterface)) {
[29] Fix | Delete
throw new \InvalidArgumentException('cookies must be an instance of GuzzleHttp\Cookie\CookieJarInterface');
[30] Fix | Delete
}
[31] Fix | Delete
$cookieJar = $options['cookies'];
[32] Fix | Delete
$request = $cookieJar->withCookieHeader($request);
[33] Fix | Delete
return $handler($request, $options)
[34] Fix | Delete
->then(
[35] Fix | Delete
function ($response) use ($cookieJar, $request) {
[36] Fix | Delete
$cookieJar->extractCookies($request, $response);
[37] Fix | Delete
return $response;
[38] Fix | Delete
}
[39] Fix | Delete
);
[40] Fix | Delete
};
[41] Fix | Delete
};
[42] Fix | Delete
}
[43] Fix | Delete
[44] Fix | Delete
/**
[45] Fix | Delete
* Middleware that throws exceptions for 4xx or 5xx responses when the
[46] Fix | Delete
* "http_error" request option is set to true.
[47] Fix | Delete
*
[48] Fix | Delete
* @return callable Returns a function that accepts the next handler.
[49] Fix | Delete
*/
[50] Fix | Delete
public static function httpErrors()
[51] Fix | Delete
{
[52] Fix | Delete
return function (callable $handler) {
[53] Fix | Delete
return function ($request, array $options) use ($handler) {
[54] Fix | Delete
if (empty($options['http_errors'])) {
[55] Fix | Delete
return $handler($request, $options);
[56] Fix | Delete
}
[57] Fix | Delete
return $handler($request, $options)->then(
[58] Fix | Delete
function (ResponseInterface $response) use ($request) {
[59] Fix | Delete
$code = $response->getStatusCode();
[60] Fix | Delete
if ($code < 400) {
[61] Fix | Delete
return $response;
[62] Fix | Delete
}
[63] Fix | Delete
throw RequestException::create($request, $response);
[64] Fix | Delete
}
[65] Fix | Delete
);
[66] Fix | Delete
};
[67] Fix | Delete
};
[68] Fix | Delete
}
[69] Fix | Delete
[70] Fix | Delete
/**
[71] Fix | Delete
* Middleware that pushes history data to an ArrayAccess container.
[72] Fix | Delete
*
[73] Fix | Delete
* @param array|\ArrayAccess $container Container to hold the history (by reference).
[74] Fix | Delete
*
[75] Fix | Delete
* @return callable Returns a function that accepts the next handler.
[76] Fix | Delete
* @throws \InvalidArgumentException if container is not an array or ArrayAccess.
[77] Fix | Delete
*/
[78] Fix | Delete
public static function history(&$container)
[79] Fix | Delete
{
[80] Fix | Delete
if (!is_array($container) && !$container instanceof \ArrayAccess) {
[81] Fix | Delete
throw new \InvalidArgumentException('history container must be an array or object implementing ArrayAccess');
[82] Fix | Delete
}
[83] Fix | Delete
[84] Fix | Delete
return function (callable $handler) use (&$container) {
[85] Fix | Delete
return function ($request, array $options) use ($handler, &$container) {
[86] Fix | Delete
return $handler($request, $options)->then(
[87] Fix | Delete
function ($value) use ($request, &$container, $options) {
[88] Fix | Delete
$container[] = [
[89] Fix | Delete
'request' => $request,
[90] Fix | Delete
'response' => $value,
[91] Fix | Delete
'error' => null,
[92] Fix | Delete
'options' => $options
[93] Fix | Delete
];
[94] Fix | Delete
return $value;
[95] Fix | Delete
},
[96] Fix | Delete
function ($reason) use ($request, &$container, $options) {
[97] Fix | Delete
$container[] = [
[98] Fix | Delete
'request' => $request,
[99] Fix | Delete
'response' => null,
[100] Fix | Delete
'error' => $reason,
[101] Fix | Delete
'options' => $options
[102] Fix | Delete
];
[103] Fix | Delete
return \GuzzleHttp\Promise\rejection_for($reason);
[104] Fix | Delete
}
[105] Fix | Delete
);
[106] Fix | Delete
};
[107] Fix | Delete
};
[108] Fix | Delete
}
[109] Fix | Delete
[110] Fix | Delete
/**
[111] Fix | Delete
* Middleware that invokes a callback before and after sending a request.
[112] Fix | Delete
*
[113] Fix | Delete
* The provided listener cannot modify or alter the response. It simply
[114] Fix | Delete
* "taps" into the chain to be notified before returning the promise. The
[115] Fix | Delete
* before listener accepts a request and options array, and the after
[116] Fix | Delete
* listener accepts a request, options array, and response promise.
[117] Fix | Delete
*
[118] Fix | Delete
* @param callable $before Function to invoke before forwarding the request.
[119] Fix | Delete
* @param callable $after Function invoked after forwarding.
[120] Fix | Delete
*
[121] Fix | Delete
* @return callable Returns a function that accepts the next handler.
[122] Fix | Delete
*/
[123] Fix | Delete
public static function tap(callable $before = null, callable $after = null)
[124] Fix | Delete
{
[125] Fix | Delete
return function (callable $handler) use ($before, $after) {
[126] Fix | Delete
return function ($request, array $options) use ($handler, $before, $after) {
[127] Fix | Delete
if ($before) {
[128] Fix | Delete
$before($request, $options);
[129] Fix | Delete
}
[130] Fix | Delete
$response = $handler($request, $options);
[131] Fix | Delete
if ($after) {
[132] Fix | Delete
$after($request, $options, $response);
[133] Fix | Delete
}
[134] Fix | Delete
return $response;
[135] Fix | Delete
};
[136] Fix | Delete
};
[137] Fix | Delete
}
[138] Fix | Delete
[139] Fix | Delete
/**
[140] Fix | Delete
* Middleware that handles request redirects.
[141] Fix | Delete
*
[142] Fix | Delete
* @return callable Returns a function that accepts the next handler.
[143] Fix | Delete
*/
[144] Fix | Delete
public static function redirect()
[145] Fix | Delete
{
[146] Fix | Delete
return function (callable $handler) {
[147] Fix | Delete
return new RedirectMiddleware($handler);
[148] Fix | Delete
};
[149] Fix | Delete
}
[150] Fix | Delete
[151] Fix | Delete
/**
[152] Fix | Delete
* Middleware that retries requests based on the boolean result of
[153] Fix | Delete
* invoking the provided "decider" function.
[154] Fix | Delete
*
[155] Fix | Delete
* If no delay function is provided, a simple implementation of exponential
[156] Fix | Delete
* backoff will be utilized.
[157] Fix | Delete
*
[158] Fix | Delete
* @param callable $decider Function that accepts the number of retries,
[159] Fix | Delete
* a request, [response], and [exception] and
[160] Fix | Delete
* returns true if the request is to be retried.
[161] Fix | Delete
* @param callable $delay Function that accepts the number of retries and
[162] Fix | Delete
* returns the number of milliseconds to delay.
[163] Fix | Delete
*
[164] Fix | Delete
* @return callable Returns a function that accepts the next handler.
[165] Fix | Delete
*/
[166] Fix | Delete
public static function retry(callable $decider, callable $delay = null)
[167] Fix | Delete
{
[168] Fix | Delete
return function (callable $handler) use ($decider, $delay) {
[169] Fix | Delete
return new RetryMiddleware($decider, $handler, $delay);
[170] Fix | Delete
};
[171] Fix | Delete
}
[172] Fix | Delete
[173] Fix | Delete
/**
[174] Fix | Delete
* Middleware that logs requests, responses, and errors using a message
[175] Fix | Delete
* formatter.
[176] Fix | Delete
*
[177] Fix | Delete
* @param LoggerInterface $logger Logs messages.
[178] Fix | Delete
* @param MessageFormatter $formatter Formatter used to create message strings.
[179] Fix | Delete
* @param string $logLevel Level at which to log requests.
[180] Fix | Delete
*
[181] Fix | Delete
* @return callable Returns a function that accepts the next handler.
[182] Fix | Delete
*/
[183] Fix | Delete
public static function log(LoggerInterface $logger, MessageFormatter $formatter, $logLevel = 'info' /* \Psr\Log\LogLevel::INFO */)
[184] Fix | Delete
{
[185] Fix | Delete
return function (callable $handler) use ($logger, $formatter, $logLevel) {
[186] Fix | Delete
return function ($request, array $options) use ($handler, $logger, $formatter, $logLevel) {
[187] Fix | Delete
return $handler($request, $options)->then(
[188] Fix | Delete
function ($response) use ($logger, $request, $formatter, $logLevel) {
[189] Fix | Delete
$message = $formatter->format($request, $response);
[190] Fix | Delete
$logger->log($logLevel, $message);
[191] Fix | Delete
return $response;
[192] Fix | Delete
},
[193] Fix | Delete
function ($reason) use ($logger, $request, $formatter) {
[194] Fix | Delete
$response = $reason instanceof RequestException
[195] Fix | Delete
? $reason->getResponse()
[196] Fix | Delete
: null;
[197] Fix | Delete
$message = $formatter->format($request, $response, $reason);
[198] Fix | Delete
$logger->notice($message);
[199] Fix | Delete
return \GuzzleHttp\Promise\rejection_for($reason);
[200] Fix | Delete
}
[201] Fix | Delete
);
[202] Fix | Delete
};
[203] Fix | Delete
};
[204] Fix | Delete
}
[205] Fix | Delete
[206] Fix | Delete
/**
[207] Fix | Delete
* This middleware adds a default content-type if possible, a default
[208] Fix | Delete
* content-length or transfer-encoding header, and the expect header.
[209] Fix | Delete
*
[210] Fix | Delete
* @return callable
[211] Fix | Delete
*/
[212] Fix | Delete
public static function prepareBody()
[213] Fix | Delete
{
[214] Fix | Delete
return function (callable $handler) {
[215] Fix | Delete
return new PrepareBodyMiddleware($handler);
[216] Fix | Delete
};
[217] Fix | Delete
}
[218] Fix | Delete
[219] Fix | Delete
/**
[220] Fix | Delete
* Middleware that applies a map function to the request before passing to
[221] Fix | Delete
* the next handler.
[222] Fix | Delete
*
[223] Fix | Delete
* @param callable $fn Function that accepts a RequestInterface and returns
[224] Fix | Delete
* a RequestInterface.
[225] Fix | Delete
* @return callable
[226] Fix | Delete
*/
[227] Fix | Delete
public static function mapRequest(callable $fn)
[228] Fix | Delete
{
[229] Fix | Delete
return function (callable $handler) use ($fn) {
[230] Fix | Delete
return function ($request, array $options) use ($handler, $fn) {
[231] Fix | Delete
return $handler($fn($request), $options);
[232] Fix | Delete
};
[233] Fix | Delete
};
[234] Fix | Delete
}
[235] Fix | Delete
[236] Fix | Delete
/**
[237] Fix | Delete
* Middleware that applies a map function to the resolved promise's
[238] Fix | Delete
* response.
[239] Fix | Delete
*
[240] Fix | Delete
* @param callable $fn Function that accepts a ResponseInterface and
[241] Fix | Delete
* returns a ResponseInterface.
[242] Fix | Delete
* @return callable
[243] Fix | Delete
*/
[244] Fix | Delete
public static function mapResponse(callable $fn)
[245] Fix | Delete
{
[246] Fix | Delete
return function (callable $handler) use ($fn) {
[247] Fix | Delete
return function ($request, array $options) use ($handler, $fn) {
[248] Fix | Delete
return $handler($request, $options)->then($fn);
[249] Fix | Delete
};
[250] Fix | Delete
};
[251] Fix | Delete
}
[252] Fix | Delete
}
[253] Fix | Delete
[254] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function