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
|
|
|
* @var array |
77
|
|
|
*/ |
78
|
|
|
protected $results = array(); |
79
|
|
|
|
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
|
|
|
* @param \AppserverIo\Psr\Servlet\ServletContextInterface $servletContext The servlet context instance |
91
|
|
|
* |
92
|
|
|
* @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
|
|
|
public function getServletContext() |
105
|
|
|
{ |
106
|
|
|
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
|
|
|
* |
115
|
|
|
* @return void |
116
|
|
|
* @see \AppserverIo\Routlt\ActionInterface::preDispatch() |
117
|
|
|
*/ |
118
|
1 |
|
public function preDispatch(HttpServletRequestInterface $servletRequest, HttpServletResponseInterface $servletResponse) |
119
|
|
|
{ |
120
|
1 |
|
return; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Method that will be invoked after we've dispatched the request. |
125
|
|
|
* |
126
|
|
|
* @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
|
1 |
|
public function postDispatch(HttpServletRequestInterface $servletRequest, HttpServletResponseInterface $servletResponse) |
133
|
|
|
{ |
134
|
1 |
|
return; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* This method returns the default action method name that has to be invoked . |
139
|
|
|
* |
140
|
|
|
* @return string The default action method name that has to be invoked |
141
|
|
|
*/ |
142
|
1 |
|
public function getDefaultMethod() |
143
|
|
|
{ |
144
|
1 |
|
return BaseAction::DEFAULT_METHOD_NAME; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Returns the attributes of the action instance. |
149
|
|
|
* |
150
|
|
|
* @return array The attributes of the action instance |
151
|
|
|
*/ |
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
|
|
|
* |
163
|
|
|
* @return boolean TRUE if the attribute has been attached, else FALSE |
164
|
|
|
*/ |
165
|
|
|
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
|
|
|
* |
176
|
|
|
* @return void |
177
|
|
|
*/ |
178
|
2 |
|
public function setAttribute($key, $value) |
179
|
|
|
{ |
180
|
2 |
|
$this->attributes[$key] = $value; |
181
|
2 |
|
} |
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
|
2 |
|
public function getAttribute($key) |
191
|
|
|
{ |
192
|
2 |
|
if (isset($this->attributes[$key])) { |
193
|
2 |
|
return $this->attributes[$key]; |
194
|
|
|
} |
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
|
|
|
* @return void |
203
|
|
|
* @see \AppserverIo\Routlt\ActionInterface::addResult() |
204
|
|
|
*/ |
205
|
1 |
|
public function addResult(ResultInterface $result) |
206
|
|
|
{ |
207
|
1 |
|
$this->results[$result->getName()] = $result; |
208
|
1 |
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Tries to find and return the result with the passed name. |
212
|
|
|
* |
213
|
|
|
* @param string $name The name of the result to return |
214
|
|
|
* |
215
|
|
|
* @return \AppserverIo\Routlt\Results\ResultInterface|null The requested result |
216
|
|
|
* @see \AppserverIo\Routlt\ActionInterface::findResult() |
217
|
|
|
*/ |
218
|
2 |
|
public function findResult($name) |
219
|
|
|
{ |
220
|
2 |
|
if (isset($this->results[$name])) { |
221
|
1 |
|
return $this->results[$name]; |
222
|
|
|
} |
223
|
1 |
|
} |
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
|
1 |
|
public function addFieldError($name, $message) |
235
|
|
|
{ |
236
|
1 |
|
$this->errors[$name] = $message; |
237
|
1 |
|
} |
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
|
1 |
|
public function hasErrors() |
246
|
|
|
{ |
247
|
1 |
|
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
|
1 |
|
public function getErrors() |
257
|
|
|
{ |
258
|
1 |
|
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
|
|
|
|