Mercurial > hg > isophonics-drupal-site
annotate vendor/stack/builder/src/Stack/Builder.php @ 19:fa3358dc1485 tip
Add ndrum files
author | Chris Cannam |
---|---|
date | Wed, 28 Aug 2019 13:14:47 +0100 |
parents | 4c8ae668cc8c |
children |
rev | line source |
---|---|
Chris@0 | 1 <?php |
Chris@0 | 2 |
Chris@0 | 3 namespace Stack; |
Chris@0 | 4 |
Chris@0 | 5 use Symfony\Component\HttpKernel\HttpKernelInterface; |
Chris@0 | 6 |
Chris@0 | 7 class Builder |
Chris@0 | 8 { |
Chris@0 | 9 private $specs; |
Chris@0 | 10 |
Chris@0 | 11 public function __construct() |
Chris@0 | 12 { |
Chris@0 | 13 $this->specs = new \SplStack(); |
Chris@0 | 14 } |
Chris@0 | 15 |
Chris@0 | 16 public function unshift(/*$kernelClass, $args...*/) |
Chris@0 | 17 { |
Chris@0 | 18 if (func_num_args() === 0) { |
Chris@0 | 19 throw new \InvalidArgumentException("Missing argument(s) when calling unshift"); |
Chris@0 | 20 } |
Chris@0 | 21 |
Chris@0 | 22 $spec = func_get_args(); |
Chris@0 | 23 $this->specs->unshift($spec); |
Chris@0 | 24 |
Chris@0 | 25 return $this; |
Chris@0 | 26 } |
Chris@0 | 27 |
Chris@0 | 28 public function push(/*$kernelClass, $args...*/) |
Chris@0 | 29 { |
Chris@0 | 30 if (func_num_args() === 0) { |
Chris@0 | 31 throw new \InvalidArgumentException("Missing argument(s) when calling push"); |
Chris@0 | 32 } |
Chris@0 | 33 |
Chris@0 | 34 $spec = func_get_args(); |
Chris@0 | 35 $this->specs->push($spec); |
Chris@0 | 36 |
Chris@0 | 37 return $this; |
Chris@0 | 38 } |
Chris@0 | 39 |
Chris@0 | 40 public function resolve(HttpKernelInterface $app) |
Chris@0 | 41 { |
Chris@0 | 42 $middlewares = array($app); |
Chris@0 | 43 |
Chris@0 | 44 foreach ($this->specs as $spec) { |
Chris@0 | 45 $args = $spec; |
Chris@0 | 46 $firstArg = array_shift($args); |
Chris@0 | 47 |
Chris@0 | 48 if (is_callable($firstArg)) { |
Chris@0 | 49 $app = $firstArg($app); |
Chris@0 | 50 } else { |
Chris@0 | 51 $kernelClass = $firstArg; |
Chris@0 | 52 array_unshift($args, $app); |
Chris@0 | 53 |
Chris@0 | 54 $reflection = new \ReflectionClass($kernelClass); |
Chris@0 | 55 $app = $reflection->newInstanceArgs($args); |
Chris@0 | 56 } |
Chris@0 | 57 |
Chris@0 | 58 array_unshift($middlewares, $app); |
Chris@0 | 59 } |
Chris@0 | 60 |
Chris@0 | 61 return new StackedHttpKernel($app, $middlewares); |
Chris@0 | 62 } |
Chris@0 | 63 } |