1
|
|
|
<?php namespace Comodojo\Dispatcher\Output; |
2
|
|
|
|
3
|
|
|
use \Comodojo\Dispatcher\Request\Model as Request; |
4
|
|
|
use \Comodojo\Dispatcher\Response\Model as Response; |
5
|
|
|
use \Comodojo\Dispatcher\Router\Route; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @package Comodojo Dispatcher |
9
|
|
|
* @author Marco Giovinazzi <[email protected]> |
10
|
|
|
* @author Marco Castiello <[email protected]> |
11
|
|
|
* @license MIT |
12
|
|
|
* |
13
|
|
|
* LICENSE: |
14
|
|
|
* |
15
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
16
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
17
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
18
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
19
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
20
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
21
|
|
|
* THE SOFTWARE. |
22
|
|
|
*/ |
23
|
|
|
|
24
|
|
|
class Redirect { |
25
|
|
|
|
26
|
|
|
public static function compose( |
27
|
|
|
Request $request, |
28
|
|
|
Response $response, |
29
|
|
|
Route $route |
30
|
|
|
) { |
31
|
|
|
|
32
|
|
|
$status = $response->getStatus(); |
33
|
|
|
$content = $response->getContent(); |
34
|
|
|
$headers = $response->getHeaders(); |
35
|
|
|
|
36
|
|
|
$code = $route->getRedirectCode(); |
37
|
|
|
|
38
|
|
|
$location = $route->getRedirectLocation(); |
39
|
|
|
$uri = empty($location) ? (string) $request->getUri() : $location; |
40
|
|
|
|
41
|
|
|
$message = $route->getRedirectMessage(); |
42
|
|
|
|
43
|
|
|
if ( $route->getRedirectType() == Route::REDIRECT_REFRESH ) { |
|
|
|
|
44
|
|
|
|
45
|
|
|
$output = empty($message) ? |
46
|
|
|
"Please follow <a href=\"$location\">this link</a>" |
|
|
|
|
47
|
|
|
: $message; |
48
|
|
|
|
49
|
|
|
$status->set(200); |
50
|
|
|
$headers->set("Refresh", "0;url=$location"); |
|
|
|
|
51
|
|
|
$content->set($output); |
52
|
|
|
|
53
|
|
|
} else { |
54
|
|
|
|
55
|
|
|
if ( !empty($code) ) { |
56
|
|
|
$status->set($code); |
57
|
|
|
} else if ( $request->getVersion() === 'HTTP/1.1' ) { |
|
|
|
|
58
|
|
|
$status->set( (string) $request->getMethod() !== 'GET' ? 303 : 307); |
59
|
|
|
} else { |
60
|
|
|
$status->set(302); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$content->set($message); |
64
|
|
|
$response->getLocation()->set($uri); |
65
|
|
|
|
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
} |
71
|
|
|
|