Open Journal Systems  3.3.0
HandlerStack.php
1 <?php
2 namespace GuzzleHttp;
3 
7 
13 {
15  private $handler;
16 
18  private $stack = [];
19 
21  private $cached;
22 
40  public static function create(callable $handler = null)
41  {
42  $stack = new self($handler ?: choose_handler());
43  $stack->push(Middleware::httpErrors(), 'http_errors');
44  $stack->push(Middleware::redirect(), 'allow_redirects');
45  $stack->push(Middleware::cookies(), 'cookies');
46  $stack->push(Middleware::prepareBody(), 'prepare_body');
47 
48  return $stack;
49  }
50 
54  public function __construct(callable $handler = null)
55  {
56  $this->handler = $handler;
57  }
58 
67  public function __invoke(RequestInterface $request, array $options)
68  {
69  $handler = $this->resolve();
70 
71  return $handler($request, $options);
72  }
73 
79  public function __toString()
80  {
81  $depth = 0;
82  $stack = [];
83  if ($this->handler) {
84  $stack[] = "0) Handler: " . $this->debugCallable($this->handler);
85  }
86 
87  $result = '';
88  foreach (array_reverse($this->stack) as $tuple) {
89  $depth++;
90  $str = "{$depth}) Name: '{$tuple[1]}', ";
91  $str .= "Function: " . $this->debugCallable($tuple[0]);
92  $result = "> {$str}\n{$result}";
93  $stack[] = $str;
94  }
95 
96  foreach (array_keys($stack) as $k) {
97  $result .= "< {$stack[$k]}\n";
98  }
99 
100  return $result;
101  }
102 
109  public function setHandler(callable $handler)
110  {
111  $this->handler = $handler;
112  $this->cached = null;
113  }
114 
120  public function hasHandler()
121  {
122  return (bool) $this->handler;
123  }
124 
131  public function unshift(callable $middleware, $name = null)
132  {
133  array_unshift($this->stack, [$middleware, $name]);
134  $this->cached = null;
135  }
136 
143  public function push(callable $middleware, $name = '')
144  {
145  $this->stack[] = [$middleware, $name];
146  $this->cached = null;
147  }
148 
156  public function before($findName, callable $middleware, $withName = '')
157  {
158  $this->splice($findName, $withName, $middleware, true);
159  }
160 
168  public function after($findName, callable $middleware, $withName = '')
169  {
170  $this->splice($findName, $withName, $middleware, false);
171  }
172 
178  public function remove($remove)
179  {
180  $this->cached = null;
181  $idx = is_callable($remove) ? 0 : 1;
182  $this->stack = array_values(array_filter(
183  $this->stack,
184  function ($tuple) use ($idx, $remove) {
185  return $tuple[$idx] !== $remove;
186  }
187  ));
188  }
189 
195  public function resolve()
196  {
197  if (!$this->cached) {
198  if (!($prev = $this->handler)) {
199  throw new \LogicException('No handler has been specified');
200  }
201 
202  foreach (array_reverse($this->stack) as $fn) {
203  $prev = $fn[0]($prev);
204  }
205 
206  $this->cached = $prev;
207  }
208 
209  return $this->cached;
210  }
211 
216  private function findByName($name)
217  {
218  foreach ($this->stack as $k => $v) {
219  if ($v[1] === $name) {
220  return $k;
221  }
222  }
223 
224  throw new \InvalidArgumentException("Middleware not found: $name");
225  }
226 
235  private function splice($findName, $withName, callable $middleware, $before)
236  {
237  $this->cached = null;
238  $idx = $this->findByName($findName);
239  $tuple = [$middleware, $withName];
240 
241  if ($before) {
242  if ($idx === 0) {
243  array_unshift($this->stack, $tuple);
244  } else {
245  $replacement = [$tuple, $this->stack[$idx]];
246  array_splice($this->stack, $idx, 1, $replacement);
247  }
248  } elseif ($idx === count($this->stack) - 1) {
249  $this->stack[] = $tuple;
250  } else {
251  $replacement = [$this->stack[$idx], $tuple];
252  array_splice($this->stack, $idx, 1, $replacement);
253  }
254  }
255 
263  private function debugCallable($fn)
264  {
265  if (is_string($fn)) {
266  return "callable({$fn})";
267  }
268 
269  if (is_array($fn)) {
270  return is_string($fn[0])
271  ? "callable({$fn[0]}::{$fn[1]})"
272  : "callable(['" . get_class($fn[0]) . "', '{$fn[1]}'])";
273  }
274 
275  return 'callable(' . spl_object_hash($fn) . ')';
276  }
277 }
GuzzleHttp
Definition: vendor/guzzlehttp/guzzle/src/Client.php:2
GuzzleHttp\HandlerStack\__invoke
__invoke(RequestInterface $request, array $options)
Definition: HandlerStack.php:76
GuzzleHttp\HandlerStack\push
push(callable $middleware, $name='')
Definition: HandlerStack.php:152
GuzzleHttp\Promise\PromiseInterface
Definition: PromiseInterface.php:13
Psr\Http\Message\RequestInterface
Definition: vendor/psr/http-message/src/RequestInterface.php:24
GuzzleHttp\HandlerStack\__toString
__toString()
Definition: HandlerStack.php:88
GuzzleHttp\HandlerStack\before
before($findName, callable $middleware, $withName='')
Definition: HandlerStack.php:165
GuzzleHttp\HandlerStack\resolve
resolve()
Definition: HandlerStack.php:204
GuzzleHttp\HandlerStack\hasHandler
hasHandler()
Definition: HandlerStack.php:129
GuzzleHttp\HandlerStack\create
static create(callable $handler=null)
Definition: HandlerStack.php:49
GuzzleHttp\HandlerStack\after
after($findName, callable $middleware, $withName='')
Definition: HandlerStack.php:177
GuzzleHttp\HandlerStack
Definition: HandlerStack.php:12
GuzzleHttp\choose_handler
choose_handler()
Definition: guzzlehttp/guzzle/src/functions.php:103
Seboettg\Collection\count
count()
Definition: ArrayListTrait.php:253
Psr\Http\Message\ResponseInterface
Definition: vendor/psr/http-message/src/ResponseInterface.php:20
GuzzleHttp\HandlerStack\setHandler
setHandler(callable $handler)
Definition: HandlerStack.php:118
GuzzleHttp\HandlerStack\__construct
__construct(callable $handler=null)
Definition: HandlerStack.php:63
Http\Message\Decorator\__toString
__toString()
Definition: StreamDecorator.php:25
GuzzleHttp\HandlerStack\unshift
unshift(callable $middleware, $name=null)
Definition: HandlerStack.php:140