1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* AppserverIo\Routlt\Results\RawResult |
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\Results; |
22
|
|
|
|
23
|
|
|
use AppserverIo\Routlt\Util\EncodingAware; |
24
|
|
|
use AppserverIo\Routlt\Util\ValidationAware; |
25
|
|
|
use AppserverIo\Routlt\Util\DefaultHeadersAware; |
26
|
|
|
use AppserverIo\Psr\Servlet\ServletRequestInterface; |
27
|
|
|
use AppserverIo\Psr\Servlet\ServletResponseInterface; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Result implementation that supports action based default headers and encoding. |
31
|
|
|
* |
32
|
|
|
* @author Tim Wagner <[email protected]> |
33
|
|
|
* @copyright 2015 TechDivision GmbH <[email protected]> |
34
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
35
|
|
|
* @link http://github.com/appserver-io/routlt |
36
|
|
|
* @link http://www.appserver.io |
37
|
|
|
* |
38
|
|
|
* @Result(shared=false) |
39
|
|
|
*/ |
40
|
|
|
class RawResult extends AbstractResult |
41
|
|
|
{ |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Processes an action result by dispatching the configured servlet. |
45
|
|
|
* |
46
|
|
|
* @param \AppserverIo\Psr\Servlet\ServletRequestInterface $servletRequest The request instance |
47
|
|
|
* @param \AppserverIo\Psr\Servlet\ServletResponseInterface $servletResponse The response sent back to the client |
48
|
|
|
* |
49
|
|
|
* @return void |
50
|
|
|
*/ |
51
|
3 |
|
public function process(ServletRequestInterface $servletRequest, ServletResponseInterface $servletResponse) |
52
|
|
|
{ |
53
|
|
|
|
54
|
|
|
// load the action instance |
55
|
3 |
|
$action = $this->getAction(); |
56
|
|
|
|
57
|
|
|
// add the actions default headers to the response |
58
|
3 |
|
if ($action instanceof DefaultHeadersAware && $action->hasDefaultHeaders()) { |
59
|
3 |
|
foreach ($action->getDefaultHeaders() as $name => $value) { |
60
|
|
|
// query whether or not a header with the given name has already been set |
61
|
3 |
|
if ($servletResponse->hasHeader($name)) { |
|
|
|
|
62
|
|
|
continue; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
// if not, set the default header |
66
|
3 |
|
$servletResponse->addHeader($name, $value); |
|
|
|
|
67
|
3 |
|
} |
68
|
3 |
|
} |
69
|
|
|
|
70
|
|
|
// query whether the action contains errors or not |
71
|
3 |
|
if ($action instanceof ValidationAware && $action->hasErrors()) { |
72
|
1 |
|
$bodyContent = $action->getErrors(); |
73
|
1 |
|
} else { |
74
|
2 |
|
$bodyContent = $action->getAttribute($this->getResult()); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
// query whether the action requires content encoding or not |
78
|
3 |
|
if ($action instanceof EncodingAware && !empty($bodyContent)) { |
79
|
2 |
|
$bodyContent = $action->encode($bodyContent); |
80
|
2 |
|
} |
81
|
|
|
|
82
|
|
|
// set the encoded body content |
83
|
3 |
|
$servletResponse->appendBodyStream($bodyContent); |
84
|
3 |
|
} |
85
|
|
|
} |
86
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: