1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace EventEspresso\core\services\request; |
4
|
|
|
|
5
|
|
|
use EventEspresso\core\services\loaders\LoaderInterface; |
6
|
|
|
use SplDoublyLinkedList; |
7
|
|
|
|
8
|
|
|
defined('EVENT_ESPRESSO_VERSION') || exit; |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class RequestStackBuilder |
14
|
|
|
* Assembles the EventEspresso RequestStack |
15
|
|
|
* ! IMPORTANT ! middleware stack operates FIRST IN FIRST OUT |
16
|
|
|
* so items at the beginning of the final middleware array will run last |
17
|
|
|
* |
18
|
|
|
* @package EventEspresso\core\services\request |
19
|
|
|
* @author Brent Christensen |
20
|
|
|
* @since 4.9.53 |
21
|
|
|
*/ |
22
|
|
|
class RequestStackBuilder extends SplDoublyLinkedList |
23
|
|
|
{ |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @type LoaderInterface $loader |
27
|
|
|
*/ |
28
|
|
|
private $loader; |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* RequestStackBuilder constructor. |
33
|
|
|
* |
34
|
|
|
* @param LoaderInterface $loader |
35
|
|
|
*/ |
36
|
|
|
public function __construct(LoaderInterface $loader) |
37
|
|
|
{ |
38
|
|
|
$this->loader = $loader; |
39
|
|
|
$this->setIteratorMode(SplDoublyLinkedList::IT_MODE_LIFO | SplDoublyLinkedList::IT_MODE_KEEP); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* builds decorated middleware stack |
45
|
|
|
* by continuously injecting previous middleware app into the next |
46
|
|
|
* |
47
|
|
|
* @param RequestStackCoreAppInterface $application |
48
|
|
|
* @return RequestStack |
49
|
|
|
*/ |
50
|
|
|
public function resolve(RequestStackCoreAppInterface $application) |
51
|
|
|
{ |
52
|
|
|
$core_app = $application; |
53
|
|
|
// NOW... because the RequestStack is following the decorator pattern, |
54
|
|
|
// the first stack app we add will end up at the center of the stack, |
55
|
|
|
// and will end up being the last item to actually run, but we don't want that! |
56
|
|
|
// Basically we're dealing with TWO stacks, and transferring items from one to the other, |
57
|
|
|
// BUT... we want the final stack to be in the same order as the first. |
58
|
|
|
// So we need to reverse the iterator mode when transferring items, |
59
|
|
|
// because if we don't, the second stack will end up in the incorrect order. |
60
|
|
|
$this->setIteratorMode(SplDoublyLinkedList::IT_MODE_FIFO | SplDoublyLinkedList::IT_MODE_KEEP); |
61
|
|
|
for ($this->rewind(); $this->valid(); $this->next()) { |
62
|
|
|
$middleware_app = $this->current(); |
63
|
|
|
$middleware_app_class = array_shift($middleware_app); |
64
|
|
|
$middleware_app_args = is_array($middleware_app) ? $middleware_app : array(); |
65
|
|
|
$middleware_app_args = array($application, $this->loader) + $middleware_app_args; |
66
|
|
|
$application = $this->loader->getShared($middleware_app_class, $middleware_app_args); |
67
|
|
|
} |
68
|
|
|
return new RequestStack($application, $core_app); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|