forked from illuminate/routing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStack.php
115 lines (99 loc) · 1.99 KB
/
Stack.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php namespace Illuminate\Routing;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Contracts\Container\Container;
class Stack {
/**
* The container instance.
*
* @var \Illuminate\Contracts\Container\Container
*/
protected $container;
/**
* The request instance.
*
* @var \Illuminate\Http\Request
*/
protected $request;
/**
* The middleware stack.
*
* @var array
*/
protected $middleware = array();
/**
* Create a new Stack instance.
*
* @param \Closure $app
* @param array $middlewares
* @return void
*/
public function __construct(Container $container)
{
$this->container = $container;
}
/**
* Set the request being sent through the stack.
*
* @param \Illuminate\Http\Request
* @return $this
*/
public function send(Request $request)
{
$this->request = $request;
return $this;
}
/**
* Set the layers / middleware of the stack.
*
* @param array $middleware
* @return $this
*/
public function through(array $middleware)
{
$this->middleware = $middleware;
return $this;
}
/**
* Run the stack with the given request.
*
* @param \Illuminate\Http\Request $request
* @return mixed
*/
public function then(Closure $app)
{
$firstSlice = $this->getInitialSlice($app);
$middleware = array_reverse($this->middleware);
return call_user_func(
array_reduce($middleware, $this->getSlice(), $firstSlice), $this->request
);
}
/**
* Get a Closure that represents a slice of the application onion.
*
* @return \Closure
*/
protected function getSlice()
{
return function($stack, $middleware)
{
return function($request) use ($stack, $middleware)
{
return $this->container->make($middleware)->handle($request, $stack);
};
};
}
/**
* Get the initial slice to begin the stack call.
*
* @param \Closure $app
* @return \Closure
*/
protected function getInitialSlice(Closure $app)
{
return function() use ($app)
{
return call_user_func($app, $this->request);
};
}
}