Skip to content

Commit

Permalink
Fix: RedirectResponse is defined in Response
Browse files Browse the repository at this point in the history
  • Loading branch information
Olivier Laviale committed Sep 14, 2014
1 parent 310dc4f commit 3a01807
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 49 deletions.
61 changes: 61 additions & 0 deletions lib/RedirectResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

/*
* This file is part of the ICanBoogie package.
*
* (c) Olivier Laviale <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace ICanBoogie\HTTP;

/**
* A HTTP response doing a redirect.
*/
class RedirectResponse extends Response
{
/**
* Initializes the `Location` header.
*
* @param string $url URL to redirect to.
* @param int $status Status code (default to 302).
* @param array $headers Additional headers.
*
* @throws \InvalidArgumentException if the provided status code is not a redirect.
*/
public function __construct($url, $status=302, array $headers=[])
{
parent::__construct
(
function($response) {

$location = $response->location;
$title = \ICanBoogie\escape($location);

echo <<<EOT
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="refresh" content="1;url={$location}" />
<title>Redirecting to {$title}</title>
</head>
<body>
Redirecting to <a href="{$location}">{$title}</a>.
</body>
</html>
EOT;
},

$status, [ 'Location' => $url ] + $headers
);

if (!$this->is_redirect)
{
throw new \InvalidArgumentException("The HTTP status code is not a redirect: {$status}.");
}
}
}
49 changes: 0 additions & 49 deletions lib/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -894,53 +894,4 @@ protected function last_chance_set($property, $value, &$success)
throw new PropertyNotWritable(array($property, $this));
}
*/
}

/**
* A HTTP response doing a redirect.
*/
class RedirectResponse extends Response
{
/**
* Initializes the `Location` header.
*
* @param string $url URL to redirect to.
* @param int $status Status code (default to 302).
* @param array $headers Additional headers.
*
* @throws \InvalidArgumentException if the provided status code is not a redirect.
*/
public function __construct($url, $status=302, array $headers=[])
{
parent::__construct
(
function($response) {

$location = $response->location;
$title = \ICanBoogie\escape($location);

echo <<<EOT
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="refresh" content="1;url={$location}" />
<title>Redirecting to {$title}</title>
</head>
<body>
Redirecting to <a href="{$location}">{$title}</a>.
</body>
</html>
EOT;
},

$status, [ 'Location' => $url ] + $headers
);

if (!$this->is_redirect)
{
throw new \InvalidArgumentException("The HTTP status code is not a redirect: {$status}.");
}
}
}

0 comments on commit 3a01807

Please sign in to comment.