/
halo_handler_AbstractHandlerMapping.php
176 lines (154 loc) · 4.49 KB
/
halo_handler_AbstractHandlerMapping.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
require_once('halo_IHandlerMapping.php');
require_once('substrate_Context.php');
require_once('substrate_stones_IContextAware.php');
abstract class halo_AbstractHandlerMapping implements halo_IHandlerMapping, substrate_stones_IContextAware {
/**
* Substrate context
* @var substrate_Context
*/
protected $context;
/**
* Default handler
* @var mixed
*/
protected $defaultHandler;
/**
* List of interceptors
* @var Array
*/
protected $interceptors = array();
/**
* List of adapted interceptors
* @var Array
*/
protected $adaptedInterceptors = array();
/**
* Stone order
* @var number
*/
protected $stoneOrder = null;
/**
* Get a handler
* @param halo_HttpRequest $httpRequest
*/
final public function getHandler(halo_HttpRequest $httpRequest) {
$handler = $this->getHandlerInternal($httpRequest);
if ( $handler === null ) {
$handler = $this->defaultHandler;
}
if ( $handler === null ) {
return null;
}
if ( is_string($handler) ) {
//
}
return $handler;
}
/**
* We are Substrate context aware
* @param $context
*/
public function informAboutContext(substrate_Context $context) {
$this->context = $context;
$this->initContext();
}
/**
* Initialize context
*/
protected function initContext() {
$this->extendInterceptors($this->interceptors);
$this->initInterceptors();
}
/**
* Get the adapted interceptors.
*/
protected function getAdaptedInterceptors() {
return $this->adaptedInterceptors;
}
/**
* Get the handler execution chain
* @param mixed $handler
* @return halo_HandlerExecutionChain
*/
public function getHandlerExecutionChain(halo_HttpRequest $httpRequest) {
$handler = $this->getHandler($httpRequest);
if ( $handler !== null ) {
// We must have found a handler, wrap it!
return $this->getHandlerExecutionChainInternal($handler);
}
return null;
}
/**
* Get the handler execution chain (internal)
* @param mixed $handler
* @return halo_HandlerExecutionChain
*/
protected function getHandlerExecutionChainInternal($handler) {
if ( $handler instanceof halo_HandlerExecutionChain ) {
$handler->addInterceptors($this->getAdaptedInterceptors());
return $handler;
} else {
return new halo_HandlerExecutionChain($handler, $this->getAdaptedInterceptors());
}
}
/**
* Set interceptors
* @param $interceptors
*/
public function setInterceptors($interceptors) {
if ( ! is_array($interceptors) ) { $interceptors = array($interceptors); }
foreach ( $interceptors as $interceptor ) {
$this->interceptors[] = $interceptor;
}
}
/**
* Hook to allow for additional interceptors to be registred.
* @param Array
*/
public function extendInterceptors($interceptors) {
// noop
}
/**
* Initialize interceptors
*/
public function initInterceptors() {
foreach ( $this->interceptors as $interceptor ) {
if ( is_string($interceptor) ) {
$interceptor = $this->context->get($interceptor);
}
$this->adaptedInterceptors[] = $this->adaptInterceptor($interceptor);
}
}
/**
* Make certain that this interceptor is something we can use
* @param $interceptor
*/
public function adaptInterceptor($interceptor) {
if ( $interceptor instanceof halo_IHandlerInterceptor ) {
return $interceptor;
//} elseif ( $interceptor instanceof halo_ISomethingElse ) {
//return new halo_HandlerInterceptorInterfaceAdapter($interceptor);
} else {
throw new Exception('Interceptor of type "' . get_class($interceptor) . '" not supported.');
}
}
/**
* Get the order of this stone
*/
public function getStoneOrder() {
return $this->stoneOrder;
}
/**
* Set the order of this stone
* @param $stoneOrder
*/
public function setStoneOrder($stoneOrder) {
$this->stoneOrder = $stoneOrder;
}
/**
* Get a handler
* @param $httpRequest
*/
abstract protected function getHandlerInternal(halo_HttpRequest $httpRequest);
}