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