-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.php
More file actions
128 lines (110 loc) · 2.13 KB
/
Stack.php
File metadata and controls
128 lines (110 loc) · 2.13 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php
/**
* @package framework
* @copyright Copyright (c) 2005-2020 The Regents of the University of California.
* @license http://opensource.org/licenses/MIT MIT
*/
namespace Qubeshub\Base;
use Hubzero\Http\Request;
/**
* Request stack
*
* Inspired, in part, by Laravel
* http://laravel.com
*/
class Stack
{
/**
* Current position in stack
*
* @var integer
*/
protected $position;
/**
* Layers to run though
*
* @var array
*/
protected $layers;
/**
* Object to pass through layers
*
* @var object
*/
protected $request;
/**
* Create stack with first layer as core
*
* @param object $core Core service
* @return void
*/
public function __construct($core)
{
$this->position = 0;
$this->layers = array($core);
}
/**
* Send request through stack
*
* @param object $request Request object
* @return object
*/
public function send(Request $request)
{
$this->request = $request;
return $this;
}
/**
* Set layers on stack
*
* @param array $layers Array of services
* @return object
*/
public function through($layers)
{
// Merge existing layers (core)
$this->layers = array_merge(
$this->layers, $layers
);
// Put the layers in reverse
$this->layers = array_values(array_reverse($this->layers));
return $this;
}
/**
* Add something to the stack
*
* @param mixed $layer
* @return object
*/
public function push($layer)
{
array_push($this->layers, $layer);
return $this;
}
/**
* Final callback
*
* @param object $callback Callback after stack is run
* @return void Result of callback
*/
public function then(\Closure $callback)
{
$response = $this->layers[0]->handle($this->request);
return call_user_func($callback, $this->request, $response);
}
/**
* Call next layer in stack
*
* @param object $request Request object
* @return object
*/
public function next(Request $request)
{
// Update the stack position
$this->position++;
// Get the next layer
$layer = $this->layers[$this->position];
// Call handle on next layer
return $layer->handle($request);
}
}