|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* AppserverIo\Routlt\BaseAction |
|
5
|
|
|
* |
|
6
|
|
|
* NOTICE OF LICENSE |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the Open Software License (OSL 3.0) |
|
9
|
|
|
* that is available through the world-wide-web at this URL: |
|
10
|
|
|
* http://opensource.org/licenses/osl-3.0.php |
|
11
|
|
|
* |
|
12
|
|
|
* PHP version 5 |
|
13
|
|
|
* |
|
14
|
|
|
* @author Tim Wagner <[email protected]> |
|
15
|
|
|
* @copyright 2015 TechDivision GmbH <[email protected]> |
|
16
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
17
|
|
|
* @link http://github.com/appserver-io/routlt |
|
18
|
|
|
* @link http://www.appserver.io |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
namespace AppserverIo\Routlt; |
|
22
|
|
|
|
|
23
|
|
|
use AppserverIo\Lang\Object; |
|
24
|
|
|
use AppserverIo\Routlt\Results\ResultInterface; |
|
25
|
|
|
use AppserverIo\Routlt\Util\ValidationAware; |
|
26
|
|
|
use AppserverIo\Routlt\Util\DescriptorAware; |
|
27
|
|
|
use AppserverIo\Routlt\Util\ServletContextAware; |
|
28
|
|
|
use AppserverIo\Psr\Deployment\DescriptorInterface; |
|
29
|
|
|
use AppserverIo\Psr\Servlet\ServletContextInterface; |
|
30
|
|
|
use AppserverIo\Psr\Servlet\Http\HttpServletRequestInterface; |
|
31
|
|
|
use AppserverIo\Psr\Servlet\Http\HttpServletResponseInterface; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* This class is the abstract base class for all Actions. |
|
35
|
|
|
* |
|
36
|
|
|
* @author Tim Wagner <[email protected]> |
|
37
|
|
|
* @copyright 2015 TechDivision GmbH <[email protected]> |
|
38
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
39
|
|
|
* @link http://github.com/appserver-io/routlt |
|
40
|
|
|
* @link http://www.appserver.io |
|
41
|
|
|
*/ |
|
42
|
|
|
abstract class BaseAction extends Object implements ActionInterface, ValidationAware, ServletContextAware, DescriptorAware |
|
43
|
|
|
{ |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Holds the name of the default method to invoke if the parameter with the method name to invoke is not specified. |
|
47
|
|
|
* |
|
48
|
|
|
* @var string |
|
49
|
|
|
*/ |
|
50
|
|
|
const DEFAULT_METHOD_NAME = 'perform'; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* The context for the actual request. |
|
54
|
|
|
* |
|
55
|
|
|
* @var \AppserverIo\Psr\Servlet\ServletContextInterface |
|
56
|
|
|
*/ |
|
57
|
|
|
protected $servletContext = null; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* The array with the action errors. |
|
61
|
|
|
* |
|
62
|
|
|
* @var array |
|
63
|
|
|
*/ |
|
64
|
|
|
protected $errors = array(); |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* The action specific attributes. |
|
68
|
|
|
* |
|
69
|
|
|
* @var array |
|
70
|
|
|
*/ |
|
71
|
|
|
protected $attributes = array(); |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* The array with the action results. |
|
75
|
|
|
* |
|
76
|
12 |
|
* @var array |
|
77
|
|
|
*/ |
|
78
|
12 |
|
protected $results = array(); |
|
79
|
12 |
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* The descriptor instance. |
|
82
|
|
|
* |
|
83
|
|
|
* @var \AppserverIo\Psr\Deployment\DescriptorInterface |
|
84
|
|
|
*/ |
|
85
|
|
|
protected $descriptor; |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Sets the actual servlet context instance. |
|
89
|
|
|
* |
|
90
|
1 |
|
* @param \AppserverIo\Psr\Servlet\ServletContextInterface $servletContext The servlet context instance |
|
91
|
|
|
* |
|
92
|
1 |
|
* @return void |
|
93
|
|
|
*/ |
|
94
|
|
|
public function setServletContext(ServletContextInterface $servletContext) |
|
95
|
|
|
{ |
|
96
|
|
|
$this->servletContext = $servletContext; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Returns the servlet context instance. |
|
101
|
|
|
* |
|
102
|
|
|
* @return \AppserverIo\Psr\Servlet\ServletContextInterface The servlet context instance |
|
103
|
|
|
*/ |
|
104
|
1 |
|
public function getServletContext() |
|
105
|
|
|
{ |
|
106
|
1 |
|
return $this->servletContext; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Method that will be invoked before we dispatch the request. |
|
111
|
|
|
* |
|
112
|
|
|
* @param \AppserverIo\Psr\Servlet\Http\HttpServletRequestInterface $servletRequest The request instance |
|
113
|
|
|
* @param \AppserverIo\Psr\Servlet\Http\HttpServletResponseInterface $servletResponse The response instance |
|
114
|
1 |
|
* |
|
115
|
|
|
* @return void |
|
116
|
1 |
|
* @see \AppserverIo\Routlt\ActionInterface::preDispatch() |
|
117
|
|
|
*/ |
|
118
|
|
|
public function preDispatch(HttpServletRequestInterface $servletRequest, HttpServletResponseInterface $servletResponse) |
|
119
|
|
|
{ |
|
120
|
|
|
return; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
2 |
|
* Method that will be invoked after we've dispatched the request. |
|
125
|
|
|
* |
|
126
|
2 |
|
* @param \AppserverIo\Psr\Servlet\Http\HttpServletRequestInterface $servletRequest The request instance |
|
127
|
|
|
* @param \AppserverIo\Psr\Servlet\Http\HttpServletResponseInterface $servletResponse The response instance |
|
128
|
|
|
* |
|
129
|
|
|
* @return void |
|
130
|
|
|
* @see \AppserverIo\Routlt\ActionInterface::postDispatch() |
|
131
|
|
|
*/ |
|
132
|
|
|
public function postDispatch(HttpServletRequestInterface $servletRequest, HttpServletResponseInterface $servletResponse) |
|
133
|
|
|
{ |
|
134
|
|
|
return; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
1 |
|
/** |
|
138
|
|
|
* This method returns the default action method name that has to be invoked . |
|
139
|
1 |
|
* |
|
140
|
1 |
|
* @return string The default action method name that has to be invoked |
|
141
|
|
|
*/ |
|
142
|
|
|
public function getDefaultMethod() |
|
143
|
|
|
{ |
|
144
|
|
|
return BaseAction::DEFAULT_METHOD_NAME; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Returns the attributes of the action instance. |
|
149
|
1 |
|
* |
|
150
|
|
|
* @return array The attributes of the action instance |
|
151
|
1 |
|
*/ |
|
152
|
|
|
public function getAttributes() |
|
153
|
|
|
{ |
|
154
|
|
|
return $this->attributes; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Query's whether or not an attribute with the passed key has been |
|
159
|
|
|
* attached to the action instance. |
|
160
|
|
|
* |
|
161
|
|
|
* @param string $key The key of the attribute to query for |
|
162
|
1 |
|
* |
|
163
|
|
|
* @return boolean TRUE if the attribute has been attached, else FALSE |
|
164
|
1 |
|
*/ |
|
165
|
1 |
|
public function hasAttribute($key) |
|
166
|
|
|
{ |
|
167
|
|
|
return isset($this->attributes[$key]); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* Attaches the passed value with passed key to the action instance. |
|
172
|
|
|
* |
|
173
|
|
|
* @param string $key The key to attach the data with |
|
174
|
|
|
* @param mixed $value The data to be attached |
|
175
|
2 |
|
* |
|
176
|
|
|
* @return void |
|
177
|
2 |
|
*/ |
|
178
|
1 |
|
public function setAttribute($key, $value) |
|
179
|
|
|
{ |
|
180
|
1 |
|
$this->attributes[$key] = $value; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* Returns the data with the passed key from the context of the action instance. |
|
185
|
|
|
* |
|
186
|
|
|
* @param string $key The key to return the data for |
|
187
|
|
|
* |
|
188
|
|
|
* @return mixed The requested data |
|
189
|
|
|
*/ |
|
190
|
|
|
public function getAttribute($key) |
|
191
|
1 |
|
{ |
|
192
|
|
|
if (isset($this->attributes[$key])) { |
|
193
|
1 |
|
return $this->attributes[$key]; |
|
194
|
1 |
|
} |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* Adds the result to the action. |
|
199
|
|
|
* |
|
200
|
|
|
* @param \AppserverIo\Routlt\Results\ResultInterface $result The result that has to be added |
|
201
|
|
|
* |
|
202
|
1 |
|
* @return void |
|
203
|
|
|
* @see \AppserverIo\Routlt\ActionInterface::addResult() |
|
204
|
1 |
|
*/ |
|
205
|
|
|
public function addResult(ResultInterface $result) |
|
206
|
|
|
{ |
|
207
|
|
|
$this->results[$result->getName()] = $result; |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* Tries to find and return the result with the passed name. |
|
212
|
|
|
* |
|
213
|
1 |
|
* @param string $name The name of the result to return |
|
214
|
|
|
* |
|
215
|
1 |
|
* @return \AppserverIo\Routlt\Results\ResultInterface|null The requested result |
|
216
|
|
|
* @see \AppserverIo\Routlt\ActionInterface::findResult() |
|
217
|
|
|
*/ |
|
218
|
|
|
public function findResult($name) |
|
219
|
|
|
{ |
|
220
|
|
|
if (isset($this->results[$name])) { |
|
221
|
|
|
return $this->results[$name]; |
|
222
|
|
|
} |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
/** |
|
226
|
|
|
* Adds a field error with the passed name and message. |
|
227
|
|
|
* |
|
228
|
|
|
* @param string $name The name to add the message with |
|
229
|
|
|
* @param string $message The message to add |
|
230
|
|
|
* |
|
231
|
|
|
* @return void |
|
232
|
|
|
* @see \AppserverIo\Routlt\Util\ValidationAware::addFieldError() |
|
233
|
|
|
*/ |
|
234
|
|
|
public function addFieldError($name, $message) |
|
235
|
|
|
{ |
|
236
|
|
|
$this->errors[$name] = $message; |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* Returns TRUE if validation found errors, else FALSE. |
|
241
|
|
|
* |
|
242
|
|
|
* @return boolean TRUE if validation found errors, else FALSE |
|
243
|
|
|
* @see \AppserverIo\Routlt\Util\ValidationAware::hasErrors() |
|
244
|
|
|
*/ |
|
245
|
|
|
public function hasErrors() |
|
246
|
|
|
{ |
|
247
|
|
|
return sizeof($this->errors) > 0; |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
/** |
|
251
|
|
|
* Returns the array with action errors. |
|
252
|
|
|
* |
|
253
|
|
|
* @return array The array with action errors |
|
254
|
|
|
* @see \AppserverIo\Routlt\Util\ValidationAware::getErrors() |
|
255
|
|
|
*/ |
|
256
|
|
|
public function getErrors() |
|
257
|
|
|
{ |
|
258
|
|
|
return $this->errors; |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
/** |
|
262
|
|
|
* Sets the descriptor instance. |
|
263
|
|
|
* |
|
264
|
|
|
* @param \AppserverIo\Psr\Deployment\DescriptorInterface $descriptor The descriptor instance |
|
265
|
|
|
* |
|
266
|
|
|
* @return void |
|
267
|
|
|
*/ |
|
268
|
|
|
public function setDescriptor(DescriptorInterface $descriptor) |
|
269
|
|
|
{ |
|
270
|
|
|
$this->descriptor = $descriptor; |
|
271
|
|
|
} |
|
272
|
|
|
|
|
273
|
|
|
/** |
|
274
|
|
|
* Returns the descriptor instance. |
|
275
|
|
|
* |
|
276
|
|
|
* @return \AppserverIo\Psr\Deployment\DescriptorInterface The descriptor instance |
|
277
|
|
|
*/ |
|
278
|
|
|
public function getDescriptor() |
|
279
|
|
|
{ |
|
280
|
|
|
return $this->descriptor; |
|
281
|
|
|
} |
|
282
|
|
|
} |
|
283
|
|
|
|