|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* AppserverIo\Routlt\Results\RedirectResult |
|
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\Psr\HttpMessage\Protocol; |
|
24
|
|
|
use AppserverIo\Psr\Servlet\ServletRequestInterface; |
|
25
|
|
|
use AppserverIo\Psr\Servlet\ServletResponseInterface; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Result implementation redirects to the page defined as result. |
|
29
|
|
|
* |
|
30
|
|
|
* @author Tim Wagner <[email protected]> |
|
31
|
|
|
* @copyright 2015 TechDivision GmbH <[email protected]> |
|
32
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
33
|
|
|
* @link http://github.com/appserver-io/routlt |
|
34
|
|
|
* @link http://www.appserver.io |
|
35
|
|
|
*/ |
|
36
|
|
|
class RedirectResult implements ResultInterface |
|
37
|
|
|
{ |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Trait proving basic result functionality. |
|
41
|
|
|
* |
|
42
|
|
|
* @var \AppserverIo\Routlt\Results\ResultTrait |
|
43
|
|
|
*/ |
|
44
|
|
|
use ResultTrait; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Processes an action result by dispatching the configured servlet. |
|
48
|
|
|
* |
|
49
|
|
|
* @param \AppserverIo\Psr\Servlet\ServletRequestInterface $servletRequest The request instance |
|
50
|
|
|
* @param \AppserverIo\Psr\Servlet\ServletResponseInterface $servletResponse The response sent back to the client |
|
51
|
|
|
* |
|
52
|
|
|
* @return void |
|
53
|
|
|
*/ |
|
54
|
|
|
public function process(ServletRequestInterface $servletRequest, ServletResponseInterface $servletResponse) |
|
55
|
|
|
{ |
|
56
|
|
|
|
|
57
|
|
|
// initialize the location we want to redirect to |
|
58
|
|
|
$result = $this->getResult(); |
|
59
|
|
|
|
|
60
|
|
|
// add the base modifier, if necessary |
|
61
|
|
|
if ($baseModifier = $servletRequest->getBaseModifier()) { |
|
|
|
|
|
|
62
|
|
|
$result = $baseModifier . $result; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
// stop processing the request and redirect to the URL |
|
66
|
|
|
$servletRequest->setDispatched(true); |
|
67
|
|
|
$servletResponse->setStatusCode($this->getCode()); |
|
|
|
|
|
|
68
|
|
|
$servletResponse->addHeader(Protocol::HEADER_LOCATION, $result); |
|
|
|
|
|
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
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: