This repository was archived by the owner on Jul 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathBuilder.php
63 lines (47 loc) · 1.43 KB
/
Builder.php
1
2
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php
namespace Stack;
use Symfony\Component\HttpKernel\HttpKernelInterface;
class Builder
{
private $specs;
public function __construct()
{
$this->specs = new \SplStack();
}
public function unshift(/*$kernelClass, $args...*/)
{
if (func_num_args() === 0) {
throw new \InvalidArgumentException("Missing argument(s) when calling unshift");
}
$spec = func_get_args();
$this->specs->unshift($spec);
return $this;
}
public function push(/*$kernelClass, $args...*/)
{
if (func_num_args() === 0) {
throw new \InvalidArgumentException("Missing argument(s) when calling push");
}
$spec = func_get_args();
$this->specs->push($spec);
return $this;
}
public function resolve(HttpKernelInterface $app)
{
$middlewares = array($app);
foreach ($this->specs as $spec) {
$args = $spec;
$firstArg = array_shift($args);
if (is_callable($firstArg)) {
$app = $firstArg($app);
} else {
$kernelClass = $firstArg;
array_unshift($args, $app);
$reflection = new \ReflectionClass($kernelClass);
$app = $reflection->newInstanceArgs($args);
}
array_unshift($middlewares, $app);
}
return new StackedHttpKernel($app, $middlewares);
}
}