| 1 |
<?php |
| 2 |
namespace Aws; |
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
class HandlerList implements \Countable |
| 33 |
{ |
| 34 |
const INIT = 'init'; |
| 35 |
const VALIDATE = 'validate'; |
| 36 |
const BUILD = 'build'; |
| 37 |
const SIGN = 'sign'; |
| 38 |
const ATTEMPT = 'attempt'; |
| 39 |
|
| 40 |
|
| 41 |
private $handler; |
| 42 |
|
| 43 |
|
| 44 |
private $named = []; |
| 45 |
|
| 46 |
|
| 47 |
private $sorted; |
| 48 |
|
| 49 |
|
| 50 |
private $interposeFn; |
| 51 |
|
| 52 |
|
| 53 |
private $steps = [ |
| 54 |
self::ATTEMPT => [], |
| 55 |
self::SIGN => [], |
| 56 |
self::BUILD => [], |
| 57 |
self::VALIDATE => [], |
| 58 |
self::INIT => [], |
| 59 |
]; |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
public function __construct(?callable $handler = null) |
| 65 |
{ |
| 66 |
$this->handler = $handler; |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
public function __toString() |
| 75 |
{ |
| 76 |
$str = ''; |
| 77 |
$i = 0; |
| 78 |
|
| 79 |
foreach (array_reverse($this->steps) as $k => $step) { |
| 80 |
foreach (array_reverse($step) as $j => $tuple) { |
| 81 |
$str .= "{$i}) Step: {$k}, "; |
| 82 |
if ($tuple[1]) { |
| 83 |
$str .= "Name: {$tuple[1]}, "; |
| 84 |
} |
| 85 |
$str .= "Function: " . $this->debugCallable($tuple[0]) . "\n"; |
| 86 |
$i++; |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
if ($this->handler) { |
| 91 |
$str .= "{$i}) Handler: " . $this->debugCallable($this->handler) . "\n"; |
| 92 |
} |
| 93 |
|
| 94 |
return $str; |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
public function setHandler(callable $handler) |
| 104 |
{ |
| 105 |
$this->handler = $handler; |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
public function hasHandler() |
| 114 |
{ |
| 115 |
return (bool) $this->handler; |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
public function hasMiddleware(string $name): bool |
| 128 |
{ |
| 129 |
return isset($this->named[$name]); |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
public function appendInit(callable $middleware, $name = null) |
| 139 |
{ |
| 140 |
$this->add(self::INIT, $name, $middleware); |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
public function prependInit(callable $middleware, $name = null) |
| 150 |
{ |
| 151 |
$this->add(self::INIT, $name, $middleware, true); |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
public function appendValidate(callable $middleware, $name = null) |
| 161 |
{ |
| 162 |
$this->add(self::VALIDATE, $name, $middleware); |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
public function prependValidate(callable $middleware, $name = null) |
| 172 |
{ |
| 173 |
$this->add(self::VALIDATE, $name, $middleware, true); |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
public function appendBuild(callable $middleware, $name = null) |
| 183 |
{ |
| 184 |
$this->add(self::BUILD, $name, $middleware); |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
public function prependBuild(callable $middleware, $name = null) |
| 194 |
{ |
| 195 |
$this->add(self::BUILD, $name, $middleware, true); |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
public function appendSign(callable $middleware, $name = null) |
| 205 |
{ |
| 206 |
$this->add(self::SIGN, $name, $middleware); |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
public function prependSign(callable $middleware, $name = null) |
| 216 |
{ |
| 217 |
$this->add(self::SIGN, $name, $middleware, true); |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
public function appendAttempt(callable $middleware, $name = null) |
| 227 |
{ |
| 228 |
$this->add(self::ATTEMPT, $name, $middleware); |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
public function prependAttempt(callable $middleware, $name = null) |
| 238 |
{ |
| 239 |
$this->add(self::ATTEMPT, $name, $middleware, true); |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
public function before($findName, $withName, callable $middleware) |
| 250 |
{ |
| 251 |
$this->splice($findName, $withName, $middleware, true); |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
public function after($findName, $withName, callable $middleware) |
| 262 |
{ |
| 263 |
$this->splice($findName, $withName, $middleware, false); |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
public function remove($nameOrInstance) |
| 272 |
{ |
| 273 |
if (is_callable($nameOrInstance)) { |
| 274 |
$this->removeByInstance($nameOrInstance); |
| 275 |
} elseif (is_string($nameOrInstance)) { |
| 276 |
$this->removeByName($nameOrInstance); |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
public function interpose(?callable $fn = null) |
| 295 |
{ |
| 296 |
$this->sorted = null; |
| 297 |
$this->interposeFn = $fn; |
| 298 |
} |
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
public function resolve() |
| 306 |
{ |
| 307 |
if (!($prev = $this->handler)) { |
| 308 |
throw new \LogicException('No handler has been specified'); |
| 309 |
} |
| 310 |
|
| 311 |
if ($this->sorted === null) { |
| 312 |
$this->sortMiddleware(); |
| 313 |
} |
| 314 |
|
| 315 |
foreach ($this->sorted as $fn) { |
| 316 |
$prev = $fn($prev); |
| 317 |
} |
| 318 |
|
| 319 |
return $prev; |
| 320 |
} |
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
#[\ReturnTypeWillChange] |
| 326 |
public function count() |
| 327 |
{ |
| 328 |
return count($this->steps[self::INIT]) |
| 329 |
+ count($this->steps[self::VALIDATE]) |
| 330 |
+ count($this->steps[self::BUILD]) |
| 331 |
+ count($this->steps[self::SIGN]) |
| 332 |
+ count($this->steps[self::ATTEMPT]); |
| 333 |
} |
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
private function splice($findName, $withName, callable $middleware, $before) |
| 344 |
{ |
| 345 |
if (!isset($this->named[$findName])) { |
| 346 |
throw new \InvalidArgumentException("$findName not found"); |
| 347 |
} |
| 348 |
|
| 349 |
$idx = $this->sorted = null; |
| 350 |
$step = $this->named[$findName]; |
| 351 |
|
| 352 |
if ($withName) { |
| 353 |
$this->named[$withName] = $step; |
| 354 |
} |
| 355 |
|
| 356 |
foreach ($this->steps[$step] as $i => $tuple) { |
| 357 |
if ($tuple[1] === $findName) { |
| 358 |
$idx = $i; |
| 359 |
break; |
| 360 |
} |
| 361 |
} |
| 362 |
|
| 363 |
$replacement = $before |
| 364 |
? [$this->steps[$step][$idx], [$middleware, $withName]] |
| 365 |
: [[$middleware, $withName], $this->steps[$step][$idx]]; |
| 366 |
array_splice($this->steps[$step], $idx, 1, $replacement); |
| 367 |
} |
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
private function debugCallable($fn) |
| 377 |
{ |
| 378 |
if (is_string($fn)) { |
| 379 |
return "callable({$fn})"; |
| 380 |
} |
| 381 |
|
| 382 |
if (is_array($fn)) { |
| 383 |
$ele = is_string($fn[0]) ? $fn[0] : get_class($fn[0]); |
| 384 |
return "callable(['{$ele}', '{$fn[1]}'])"; |
| 385 |
} |
| 386 |
|
| 387 |
return 'callable(' . spl_object_hash($fn) . ')'; |
| 388 |
} |
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
private function sortMiddleware() |
| 394 |
{ |
| 395 |
$this->sorted = []; |
| 396 |
|
| 397 |
if (!$this->interposeFn) { |
| 398 |
foreach ($this->steps as $step) { |
| 399 |
foreach ($step as $fn) { |
| 400 |
$this->sorted[] = $fn[0]; |
| 401 |
} |
| 402 |
} |
| 403 |
return; |
| 404 |
} |
| 405 |
|
| 406 |
$ifn = $this->interposeFn; |
| 407 |
|
| 408 |
foreach ($this->steps as $stepName => $step) { |
| 409 |
foreach ($step as $fn) { |
| 410 |
$this->sorted[] = $ifn($stepName, $fn[1]); |
| 411 |
$this->sorted[] = $fn[0]; |
| 412 |
} |
| 413 |
} |
| 414 |
} |
| 415 |
|
| 416 |
private function removeByName($name) |
| 417 |
{ |
| 418 |
if (!isset($this->named[$name])) { |
| 419 |
return; |
| 420 |
} |
| 421 |
|
| 422 |
$this->sorted = null; |
| 423 |
$step = $this->named[$name]; |
| 424 |
$this->steps[$step] = array_values( |
| 425 |
array_filter( |
| 426 |
$this->steps[$step], |
| 427 |
function ($tuple) use ($name) { |
| 428 |
return $tuple[1] !== $name; |
| 429 |
} |
| 430 |
) |
| 431 |
); |
| 432 |
} |
| 433 |
|
| 434 |
private function removeByInstance(callable $fn) |
| 435 |
{ |
| 436 |
foreach ($this->steps as $k => $step) { |
| 437 |
foreach ($step as $j => $tuple) { |
| 438 |
if ($tuple[0] === $fn) { |
| 439 |
$this->sorted = null; |
| 440 |
unset($this->named[$this->steps[$k][$j][1]]); |
| 441 |
unset($this->steps[$k][$j]); |
| 442 |
} |
| 443 |
} |
| 444 |
} |
| 445 |
} |
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
private function add($step, $name, callable $middleware, $prepend = false) |
| 456 |
{ |
| 457 |
$this->sorted = null; |
| 458 |
|
| 459 |
if ($prepend) { |
| 460 |
$this->steps[$step][] = [$middleware, $name]; |
| 461 |
} else { |
| 462 |
array_unshift($this->steps[$step], [$middleware, $name]); |
| 463 |
} |
| 464 |
|
| 465 |
if ($name) { |
| 466 |
$this->named[$name] = $step; |
| 467 |
} |
| 468 |
} |
| 469 |
} |