|
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\ActionAware; |
|
24
|
|
|
use AppserverIo\Routlt\Util\EncodingAware; |
|
25
|
|
|
use AppserverIo\Routlt\Util\ValidationAware; |
|
26
|
|
|
use AppserverIo\Routlt\Util\DefaultHeadersAware; |
|
27
|
|
|
use AppserverIo\Psr\Servlet\ServletRequestInterface; |
|
28
|
|
|
use AppserverIo\Psr\Servlet\ServletResponseInterface; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Result implementation that supports action based default headers and encoding. |
|
32
|
|
|
* |
|
33
|
|
|
* @author Tim Wagner <[email protected]> |
|
34
|
|
|
* @copyright 2015 TechDivision GmbH <[email protected]> |
|
35
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
36
|
|
|
* @link http://github.com/appserver-io/routlt |
|
37
|
|
|
* @link http://www.appserver.io |
|
38
|
|
|
*/ |
|
39
|
|
|
class RawResult implements ResultInterface, ActionAware |
|
40
|
|
|
{ |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Trait proving basic result functionality. |
|
44
|
|
|
* |
|
45
|
|
|
* @var \AppserverIo\Routlt\Results\ResultTrait |
|
46
|
|
|
*/ |
|
47
|
|
|
use ResultTrait; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Processes an action result by dispatching the configured servlet. |
|
51
|
|
|
* |
|
52
|
|
|
* @param \AppserverIo\Psr\Servlet\ServletRequestInterface $servletRequest The request instance |
|
53
|
|
|
* @param \AppserverIo\Psr\Servlet\ServletResponseInterface $servletResponse The response sent back to the client |
|
54
|
|
|
* |
|
55
|
|
|
* @return void |
|
56
|
|
|
*/ |
|
57
|
|
|
public function process(ServletRequestInterface $servletRequest, ServletResponseInterface $servletResponse) |
|
58
|
|
|
{ |
|
59
|
|
|
|
|
60
|
|
|
// load the action instance |
|
61
|
|
|
$action = $this->getAction(); |
|
62
|
|
|
|
|
63
|
|
|
// add the actions default headers to the response |
|
64
|
|
|
if ($action instanceof DefaultHeadersAware && $action->hasDefaultHeaders()) { |
|
65
|
|
|
foreach ($action->getDefaultHeaders() as $name => $value) { |
|
66
|
|
|
// query whether or not a header with the given name has already been set |
|
67
|
|
|
if ($servletResponse->hasHeader($name)) { |
|
|
|
|
|
|
68
|
|
|
continue; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
// if not, set the default header |
|
72
|
|
|
$servletResponse->addHeader($name, $value); |
|
|
|
|
|
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
// query whether the action contains errors or not |
|
77
|
|
|
if ($action instanceof ValidationAware && $action->hasErrors()) { |
|
78
|
|
|
$bodyContent = $action->getErrors(); |
|
79
|
|
|
} else { |
|
80
|
|
|
$bodyContent = $action->getAttribute($this->getResult()); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
// query whether the action requires content encoding or not |
|
84
|
|
|
if ($action instanceof EncodingAware && !empty($bodyContent)) { |
|
85
|
|
|
$bodyContent = $action->encode($bodyContent); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
// set the encoded body content |
|
89
|
|
|
$servletResponse->appendBodyStream($bodyContent); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
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: