Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/updraftp.../vendor/guzzleht.../guzzle/src
File: HandlerStack.php
<?php
[0] Fix | Delete
namespace GuzzleHttp;
[1] Fix | Delete
[2] Fix | Delete
use GuzzleHttp\Promise\PromiseInterface;
[3] Fix | Delete
use Psr\Http\Message\RequestInterface;
[4] Fix | Delete
use Psr\Http\Message\ResponseInterface;
[5] Fix | Delete
[6] Fix | Delete
/**
[7] Fix | Delete
* Creates a composed Guzzle handler function by stacking middlewares on top of
[8] Fix | Delete
* an HTTP handler function.
[9] Fix | Delete
*/
[10] Fix | Delete
class HandlerStack
[11] Fix | Delete
{
[12] Fix | Delete
/** @var callable|null */
[13] Fix | Delete
private $handler;
[14] Fix | Delete
[15] Fix | Delete
/** @var array */
[16] Fix | Delete
private $stack = [];
[17] Fix | Delete
[18] Fix | Delete
/** @var callable|null */
[19] Fix | Delete
private $cached;
[20] Fix | Delete
[21] Fix | Delete
/**
[22] Fix | Delete
* Creates a default handler stack that can be used by clients.
[23] Fix | Delete
*
[24] Fix | Delete
* The returned handler will wrap the provided handler or use the most
[25] Fix | Delete
* appropriate default handler for your system. The returned HandlerStack has
[26] Fix | Delete
* support for cookies, redirects, HTTP error exceptions, and preparing a body
[27] Fix | Delete
* before sending.
[28] Fix | Delete
*
[29] Fix | Delete
* The returned handler stack can be passed to a client in the "handler"
[30] Fix | Delete
* option.
[31] Fix | Delete
*
[32] Fix | Delete
* @param callable $handler HTTP handler function to use with the stack. If no
[33] Fix | Delete
* handler is provided, the best handler for your
[34] Fix | Delete
* system will be utilized.
[35] Fix | Delete
*
[36] Fix | Delete
* @return HandlerStack
[37] Fix | Delete
*/
[38] Fix | Delete
public static function create(callable $handler = null)
[39] Fix | Delete
{
[40] Fix | Delete
$stack = new self($handler ?: choose_handler());
[41] Fix | Delete
$stack->push(Middleware::httpErrors(), 'http_errors');
[42] Fix | Delete
$stack->push(Middleware::redirect(), 'allow_redirects');
[43] Fix | Delete
$stack->push(Middleware::cookies(), 'cookies');
[44] Fix | Delete
$stack->push(Middleware::prepareBody(), 'prepare_body');
[45] Fix | Delete
[46] Fix | Delete
return $stack;
[47] Fix | Delete
}
[48] Fix | Delete
[49] Fix | Delete
/**
[50] Fix | Delete
* @param callable $handler Underlying HTTP handler.
[51] Fix | Delete
*/
[52] Fix | Delete
public function __construct(callable $handler = null)
[53] Fix | Delete
{
[54] Fix | Delete
$this->handler = $handler;
[55] Fix | Delete
}
[56] Fix | Delete
[57] Fix | Delete
/**
[58] Fix | Delete
* Invokes the handler stack as a composed handler
[59] Fix | Delete
*
[60] Fix | Delete
* @param RequestInterface $request
[61] Fix | Delete
* @param array $options
[62] Fix | Delete
*
[63] Fix | Delete
* @return ResponseInterface|PromiseInterface
[64] Fix | Delete
*/
[65] Fix | Delete
public function __invoke(RequestInterface $request, array $options)
[66] Fix | Delete
{
[67] Fix | Delete
$handler = $this->resolve();
[68] Fix | Delete
[69] Fix | Delete
return $handler($request, $options);
[70] Fix | Delete
}
[71] Fix | Delete
[72] Fix | Delete
/**
[73] Fix | Delete
* Dumps a string representation of the stack.
[74] Fix | Delete
*
[75] Fix | Delete
* @return string
[76] Fix | Delete
*/
[77] Fix | Delete
public function __toString()
[78] Fix | Delete
{
[79] Fix | Delete
$depth = 0;
[80] Fix | Delete
$stack = [];
[81] Fix | Delete
if ($this->handler) {
[82] Fix | Delete
$stack[] = "0) Handler: " . $this->debugCallable($this->handler);
[83] Fix | Delete
}
[84] Fix | Delete
[85] Fix | Delete
$result = '';
[86] Fix | Delete
foreach (array_reverse($this->stack) as $tuple) {
[87] Fix | Delete
$depth++;
[88] Fix | Delete
$str = "{$depth}) Name: '{$tuple[1]}', ";
[89] Fix | Delete
$str .= "Function: " . $this->debugCallable($tuple[0]);
[90] Fix | Delete
$result = "> {$str}\n{$result}";
[91] Fix | Delete
$stack[] = $str;
[92] Fix | Delete
}
[93] Fix | Delete
[94] Fix | Delete
foreach (array_keys($stack) as $k) {
[95] Fix | Delete
$result .= "< {$stack[$k]}\n";
[96] Fix | Delete
}
[97] Fix | Delete
[98] Fix | Delete
return $result;
[99] Fix | Delete
}
[100] Fix | Delete
[101] Fix | Delete
/**
[102] Fix | Delete
* Set the HTTP handler that actually returns a promise.
[103] Fix | Delete
*
[104] Fix | Delete
* @param callable $handler Accepts a request and array of options and
[105] Fix | Delete
* returns a Promise.
[106] Fix | Delete
*/
[107] Fix | Delete
public function setHandler(callable $handler)
[108] Fix | Delete
{
[109] Fix | Delete
$this->handler = $handler;
[110] Fix | Delete
$this->cached = null;
[111] Fix | Delete
}
[112] Fix | Delete
[113] Fix | Delete
/**
[114] Fix | Delete
* Returns true if the builder has a handler.
[115] Fix | Delete
*
[116] Fix | Delete
* @return bool
[117] Fix | Delete
*/
[118] Fix | Delete
public function hasHandler()
[119] Fix | Delete
{
[120] Fix | Delete
return (bool) $this->handler;
[121] Fix | Delete
}
[122] Fix | Delete
[123] Fix | Delete
/**
[124] Fix | Delete
* Unshift a middleware to the bottom of the stack.
[125] Fix | Delete
*
[126] Fix | Delete
* @param callable $middleware Middleware function
[127] Fix | Delete
* @param string $name Name to register for this middleware.
[128] Fix | Delete
*/
[129] Fix | Delete
public function unshift(callable $middleware, $name = null)
[130] Fix | Delete
{
[131] Fix | Delete
array_unshift($this->stack, [$middleware, $name]);
[132] Fix | Delete
$this->cached = null;
[133] Fix | Delete
}
[134] Fix | Delete
[135] Fix | Delete
/**
[136] Fix | Delete
* Push a middleware to the top of the stack.
[137] Fix | Delete
*
[138] Fix | Delete
* @param callable $middleware Middleware function
[139] Fix | Delete
* @param string $name Name to register for this middleware.
[140] Fix | Delete
*/
[141] Fix | Delete
public function push(callable $middleware, $name = '')
[142] Fix | Delete
{
[143] Fix | Delete
$this->stack[] = [$middleware, $name];
[144] Fix | Delete
$this->cached = null;
[145] Fix | Delete
}
[146] Fix | Delete
[147] Fix | Delete
/**
[148] Fix | Delete
* Add a middleware before another middleware by name.
[149] Fix | Delete
*
[150] Fix | Delete
* @param string $findName Middleware to find
[151] Fix | Delete
* @param callable $middleware Middleware function
[152] Fix | Delete
* @param string $withName Name to register for this middleware.
[153] Fix | Delete
*/
[154] Fix | Delete
public function before($findName, callable $middleware, $withName = '')
[155] Fix | Delete
{
[156] Fix | Delete
$this->splice($findName, $withName, $middleware, true);
[157] Fix | Delete
}
[158] Fix | Delete
[159] Fix | Delete
/**
[160] Fix | Delete
* Add a middleware after another middleware by name.
[161] Fix | Delete
*
[162] Fix | Delete
* @param string $findName Middleware to find
[163] Fix | Delete
* @param callable $middleware Middleware function
[164] Fix | Delete
* @param string $withName Name to register for this middleware.
[165] Fix | Delete
*/
[166] Fix | Delete
public function after($findName, callable $middleware, $withName = '')
[167] Fix | Delete
{
[168] Fix | Delete
$this->splice($findName, $withName, $middleware, false);
[169] Fix | Delete
}
[170] Fix | Delete
[171] Fix | Delete
/**
[172] Fix | Delete
* Remove a middleware by instance or name from the stack.
[173] Fix | Delete
*
[174] Fix | Delete
* @param callable|string $remove Middleware to remove by instance or name.
[175] Fix | Delete
*/
[176] Fix | Delete
public function remove($remove)
[177] Fix | Delete
{
[178] Fix | Delete
$this->cached = null;
[179] Fix | Delete
$idx = is_callable($remove) ? 0 : 1;
[180] Fix | Delete
$this->stack = array_values(array_filter(
[181] Fix | Delete
$this->stack,
[182] Fix | Delete
function ($tuple) use ($idx, $remove) {
[183] Fix | Delete
return $tuple[$idx] !== $remove;
[184] Fix | Delete
}
[185] Fix | Delete
));
[186] Fix | Delete
}
[187] Fix | Delete
[188] Fix | Delete
/**
[189] Fix | Delete
* Compose the middleware and handler into a single callable function.
[190] Fix | Delete
*
[191] Fix | Delete
* @return callable
[192] Fix | Delete
*/
[193] Fix | Delete
public function resolve()
[194] Fix | Delete
{
[195] Fix | Delete
if (!$this->cached) {
[196] Fix | Delete
if (!($prev = $this->handler)) {
[197] Fix | Delete
throw new \LogicException('No handler has been specified');
[198] Fix | Delete
}
[199] Fix | Delete
[200] Fix | Delete
foreach (array_reverse($this->stack) as $fn) {
[201] Fix | Delete
$prev = $fn[0]($prev);
[202] Fix | Delete
}
[203] Fix | Delete
[204] Fix | Delete
$this->cached = $prev;
[205] Fix | Delete
}
[206] Fix | Delete
[207] Fix | Delete
return $this->cached;
[208] Fix | Delete
}
[209] Fix | Delete
[210] Fix | Delete
/**
[211] Fix | Delete
* @param string $name
[212] Fix | Delete
* @return int
[213] Fix | Delete
*/
[214] Fix | Delete
private function findByName($name)
[215] Fix | Delete
{
[216] Fix | Delete
foreach ($this->stack as $k => $v) {
[217] Fix | Delete
if ($v[1] === $name) {
[218] Fix | Delete
return $k;
[219] Fix | Delete
}
[220] Fix | Delete
}
[221] Fix | Delete
[222] Fix | Delete
throw new \InvalidArgumentException("Middleware not found: $name");
[223] Fix | Delete
}
[224] Fix | Delete
[225] Fix | Delete
/**
[226] Fix | Delete
* Splices a function into the middleware list at a specific position.
[227] Fix | Delete
*
[228] Fix | Delete
* @param string $findName
[229] Fix | Delete
* @param string $withName
[230] Fix | Delete
* @param callable $middleware
[231] Fix | Delete
* @param bool $before
[232] Fix | Delete
*/
[233] Fix | Delete
private function splice($findName, $withName, callable $middleware, $before)
[234] Fix | Delete
{
[235] Fix | Delete
$this->cached = null;
[236] Fix | Delete
$idx = $this->findByName($findName);
[237] Fix | Delete
$tuple = [$middleware, $withName];
[238] Fix | Delete
[239] Fix | Delete
if ($before) {
[240] Fix | Delete
if ($idx === 0) {
[241] Fix | Delete
array_unshift($this->stack, $tuple);
[242] Fix | Delete
} else {
[243] Fix | Delete
$replacement = [$tuple, $this->stack[$idx]];
[244] Fix | Delete
array_splice($this->stack, $idx, 1, $replacement);
[245] Fix | Delete
}
[246] Fix | Delete
} elseif ($idx === count($this->stack) - 1) {
[247] Fix | Delete
$this->stack[] = $tuple;
[248] Fix | Delete
} else {
[249] Fix | Delete
$replacement = [$this->stack[$idx], $tuple];
[250] Fix | Delete
array_splice($this->stack, $idx, 1, $replacement);
[251] Fix | Delete
}
[252] Fix | Delete
}
[253] Fix | Delete
[254] Fix | Delete
/**
[255] Fix | Delete
* Provides a debug string for a given callable.
[256] Fix | Delete
*
[257] Fix | Delete
* @param array|callable $fn Function to write as a string.
[258] Fix | Delete
*
[259] Fix | Delete
* @return string
[260] Fix | Delete
*/
[261] Fix | Delete
private function debugCallable($fn)
[262] Fix | Delete
{
[263] Fix | Delete
if (is_string($fn)) {
[264] Fix | Delete
return "callable({$fn})";
[265] Fix | Delete
}
[266] Fix | Delete
[267] Fix | Delete
if (is_array($fn)) {
[268] Fix | Delete
return is_string($fn[0])
[269] Fix | Delete
? "callable({$fn[0]}::{$fn[1]})"
[270] Fix | Delete
: "callable(['" . get_class($fn[0]) . "', '{$fn[1]}'])";
[271] Fix | Delete
}
[272] Fix | Delete
[273] Fix | Delete
return 'callable(' . spl_object_hash($fn) . ')';
[274] Fix | Delete
}
[275] Fix | Delete
}
[276] Fix | Delete
[277] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function