Skip to content

Commit

Permalink
Merge pull request #7545 from iRedds/move-redirect-exception
Browse files Browse the repository at this point in the history
[4.4] refactor: moving RedirectException.
  • Loading branch information
kenjis authored Jun 11, 2023
2 parents 5ea0f6f + 57af142 commit efa0991
Show file tree
Hide file tree
Showing 12 changed files with 65 additions and 11 deletions.
5 changes: 3 additions & 2 deletions system/CodeIgniter.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@
use CodeIgniter\Exceptions\PageNotFoundException;
use CodeIgniter\HTTP\CLIRequest;
use CodeIgniter\HTTP\DownloadResponse;
use CodeIgniter\HTTP\Exceptions\RedirectException;
use CodeIgniter\HTTP\IncomingRequest;
use CodeIgniter\HTTP\RedirectResponse;
use CodeIgniter\HTTP\Request;
use CodeIgniter\HTTP\ResponseInterface;
use CodeIgniter\HTTP\URI;
use CodeIgniter\Router\Exceptions\RedirectException;
use CodeIgniter\Router\Exceptions\RedirectException as DeprecatedRedirectException;
use CodeIgniter\Router\RouteCollectionInterface;
use CodeIgniter\Router\Router;
use Config\App;
Expand Down Expand Up @@ -343,7 +344,7 @@ public function run(?RouteCollectionInterface $routes = null, bool $returnRespon

try {
$this->response = $this->handleRequest($routes, config(Cache::class), $returnResponse);
} catch (RedirectException $e) {
} catch (RedirectException|DeprecatedRedirectException $e) {
$this->outputBufferingEnd();
$logger = Services::logger();
$logger->info('REDIRECTED ROUTE at ' . $e->getMessage());
Expand Down
2 changes: 1 addition & 1 deletion system/Commands/Utilities/Routes/FilterFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

use CodeIgniter\Exceptions\PageNotFoundException;
use CodeIgniter\Filters\Filters;
use CodeIgniter\Router\Exceptions\RedirectException;
use CodeIgniter\HTTP\Exceptions\RedirectException;
use CodeIgniter\Router\Router;
use Config\Services;

Expand Down
28 changes: 28 additions & 0 deletions system/HTTP/Exceptions/RedirectException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

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

namespace CodeIgniter\HTTP\Exceptions;

use CodeIgniter\Exceptions\HTTPExceptionInterface;
use Exception;

/**
* RedirectException
*/
class RedirectException extends Exception implements HTTPExceptionInterface
{
/**
* HTTP status code for redirects
*
* @var int
*/
protected $code = 302;
}
2 changes: 2 additions & 0 deletions system/Router/Exceptions/RedirectException.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

/**
* RedirectException
*
* @deprecated Use \CodeIgniter\HTTP\Exceptions\RedirectException instead
*/
class RedirectException extends Exception implements HTTPExceptionInterface
{
Expand Down
2 changes: 1 addition & 1 deletion system/Router/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@

use Closure;
use CodeIgniter\Exceptions\PageNotFoundException;
use CodeIgniter\HTTP\Exceptions\RedirectException;
use CodeIgniter\HTTP\Request;
use CodeIgniter\Router\Exceptions\RedirectException;
use CodeIgniter\Router\Exceptions\RouterException;

/**
Expand Down
3 changes: 1 addition & 2 deletions system/Test/FeatureTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@
namespace CodeIgniter\Test;

use CodeIgniter\Events\Events;
use CodeIgniter\HTTP\Exceptions\RedirectException;
use CodeIgniter\HTTP\IncomingRequest;
use CodeIgniter\HTTP\Request;
use CodeIgniter\HTTP\URI;
use CodeIgniter\HTTP\UserAgent;
use CodeIgniter\Router\Exceptions\RedirectException;
use CodeIgniter\Router\RouteCollection;
use Config\Services;
use Exception;
use ReflectionException;
Expand Down
3 changes: 1 addition & 2 deletions system/Test/FeatureTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@
namespace CodeIgniter\Test;

use CodeIgniter\Events\Events;
use CodeIgniter\HTTP\Exceptions\RedirectException;
use CodeIgniter\HTTP\IncomingRequest;
use CodeIgniter\HTTP\Request;
use CodeIgniter\HTTP\URI;
use CodeIgniter\Router\Exceptions\RedirectException;
use CodeIgniter\Router\RouteCollection;
use Config\App;
use Config\Services;
use Exception;
Expand Down
24 changes: 24 additions & 0 deletions tests/system/CodeIgniterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use CodeIgniter\Config\Services;
use CodeIgniter\Exceptions\ConfigException;
use CodeIgniter\HTTP\Response;
use CodeIgniter\Router\Exceptions\RedirectException;
use CodeIgniter\Router\RouteCollection;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\Filters\CITestStreamFilter;
Expand Down Expand Up @@ -570,6 +571,29 @@ public function testRunRedirectionWithPOSTAndHTTPCode301()
$this->assertSame(301, $response->getStatusCode());
}

/**
* test for deprecated \CodeIgniter\Router\Exceptions\RedirectException for backward compatibility
*/
public function testRedirectExceptionDeprecated(): void
{
$_SERVER['argv'] = ['index.php', '/'];
$_SERVER['argc'] = 2;

// Inject mock router.
$routes = Services::routes();
$routes->get('/', static function () {
throw new RedirectException('redirect-exception', 503);
});

$router = Services::router($routes, Services::incomingrequest());
Services::injectMock('router', $router);

$response = $this->codeigniter->run($routes, true);

$this->assertSame(503, $response->getStatusCode());
$this->assertSame('http://example.com/redirect-exception', $response->getHeaderLine('Location'));
}

public function testStoresPreviousURL()
{
$_SERVER['argv'] = ['index.php', '/'];
Expand Down
2 changes: 1 addition & 1 deletion tests/system/Router/RouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@

use CodeIgniter\Config\Services;
use CodeIgniter\Exceptions\PageNotFoundException;
use CodeIgniter\HTTP\Exceptions\RedirectException;
use CodeIgniter\HTTP\IncomingRequest;
use CodeIgniter\Router\Exceptions\RedirectException;
use CodeIgniter\Router\Exceptions\RouterException;
use CodeIgniter\Test\CIUnitTestCase;
use Config\Modules;
Expand Down
1 change: 1 addition & 0 deletions user_guide_src/source/changelogs/v4.4.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ Deprecations
``ExceptionHandler``.
- **Autoloader:** ``Autoloader::sanitizeFilename()`` is deprecated.
- **CodeIgniter:** ``CodeIgniter::$returnResponse`` property is deprecated. No longer used.
- **RedirectException:** ``\CodeIgniter\Router\Exceptions\RedirectException`` is deprecated. Use \CodeIgniter\HTTP\Exceptions\RedirectException instead.

Bugs Fixed
**********
Expand Down
2 changes: 1 addition & 1 deletion user_guide_src/source/general/errors/010.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<?php

throw new \CodeIgniter\Router\Exceptions\RedirectException($route);
throw new \CodeIgniter\HTTP\Exceptions\RedirectException($route);
2 changes: 1 addition & 1 deletion user_guide_src/source/general/errors/011.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<?php

throw new \CodeIgniter\Router\Exceptions\RedirectException($route, 301);
throw new \CodeIgniter\HTTP\Exceptions\RedirectException($route, 301);

0 comments on commit efa0991

Please sign in to comment.