From b9f5c83593598b3a1296ad9d2631e5044c5d0c47 Mon Sep 17 00:00:00 2001 From: Andrey Pyzhikov <5071@mail.ru> Date: Wed, 7 Jun 2023 01:42:58 +0800 Subject: [PATCH 1/3] refactor: moving RedirectException. --- system/CodeIgniter.php | 2 +- .../Utilities/Routes/FilterFinder.php | 2 +- system/HTTP/Exceptions/RedirectException.php | 28 +++++++++++++++++++ .../Router/Exceptions/RedirectException.php | 2 ++ system/Router/Router.php | 2 +- system/Test/FeatureTestCase.php | 3 +- system/Test/FeatureTestTrait.php | 3 +- tests/system/Router/RouterTest.php | 2 +- user_guide_src/source/changelogs/v4.4.0.rst | 1 + 9 files changed, 37 insertions(+), 8 deletions(-) create mode 100644 system/HTTP/Exceptions/RedirectException.php diff --git a/system/CodeIgniter.php b/system/CodeIgniter.php index 54e2f70f8872..2d919d3394cf 100644 --- a/system/CodeIgniter.php +++ b/system/CodeIgniter.php @@ -18,12 +18,12 @@ 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\RouteCollectionInterface; use CodeIgniter\Router\Router; use Config\App; diff --git a/system/Commands/Utilities/Routes/FilterFinder.php b/system/Commands/Utilities/Routes/FilterFinder.php index 0f25eebf47f1..59e4ac49fbee 100644 --- a/system/Commands/Utilities/Routes/FilterFinder.php +++ b/system/Commands/Utilities/Routes/FilterFinder.php @@ -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; diff --git a/system/HTTP/Exceptions/RedirectException.php b/system/HTTP/Exceptions/RedirectException.php new file mode 100644 index 000000000000..5cda9fb00cd0 --- /dev/null +++ b/system/HTTP/Exceptions/RedirectException.php @@ -0,0 +1,28 @@ + + * + * 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; +} diff --git a/system/Router/Exceptions/RedirectException.php b/system/Router/Exceptions/RedirectException.php index a23d651855de..5e19e2e9ac5c 100644 --- a/system/Router/Exceptions/RedirectException.php +++ b/system/Router/Exceptions/RedirectException.php @@ -16,6 +16,8 @@ /** * RedirectException + * + * @deprecated Use \CodeIgniter\HTTP\Exceptions\RedirectException instead */ class RedirectException extends Exception implements HTTPExceptionInterface { diff --git a/system/Router/Router.php b/system/Router/Router.php index 8dd35c656e39..1ec3e6aa36d3 100644 --- a/system/Router/Router.php +++ b/system/Router/Router.php @@ -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; /** diff --git a/system/Test/FeatureTestCase.php b/system/Test/FeatureTestCase.php index 2c2233df81b0..7119b914493b 100644 --- a/system/Test/FeatureTestCase.php +++ b/system/Test/FeatureTestCase.php @@ -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; diff --git a/system/Test/FeatureTestTrait.php b/system/Test/FeatureTestTrait.php index fd12efd4962b..30d6f23b2bf1 100644 --- a/system/Test/FeatureTestTrait.php +++ b/system/Test/FeatureTestTrait.php @@ -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; diff --git a/tests/system/Router/RouterTest.php b/tests/system/Router/RouterTest.php index 2898f154716e..bf23289ba191 100644 --- a/tests/system/Router/RouterTest.php +++ b/tests/system/Router/RouterTest.php @@ -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; diff --git a/user_guide_src/source/changelogs/v4.4.0.rst b/user_guide_src/source/changelogs/v4.4.0.rst index aab872fa6c1c..af154ec748be 100644 --- a/user_guide_src/source/changelogs/v4.4.0.rst +++ b/user_guide_src/source/changelogs/v4.4.0.rst @@ -143,6 +143,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 ********** From 6cffb8764ffd530597b15f83edd5fa5eb19eebfc Mon Sep 17 00:00:00 2001 From: Andrey Pyzhikov <5071@mail.ru> Date: Wed, 7 Jun 2023 12:34:10 +0800 Subject: [PATCH 2/3] update user guide --- user_guide_src/source/general/errors/010.php | 2 +- user_guide_src/source/general/errors/011.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/user_guide_src/source/general/errors/010.php b/user_guide_src/source/general/errors/010.php index ff9cfee974ef..9bae2ed6354b 100644 --- a/user_guide_src/source/general/errors/010.php +++ b/user_guide_src/source/general/errors/010.php @@ -1,3 +1,3 @@ Date: Wed, 7 Jun 2023 23:35:14 +0800 Subject: [PATCH 3/3] Test and exception handler for backwards compatibility. --- system/CodeIgniter.php | 3 ++- tests/system/CodeIgniterTest.php | 24 ++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/system/CodeIgniter.php b/system/CodeIgniter.php index 2d919d3394cf..b2b0a19bddf6 100644 --- a/system/CodeIgniter.php +++ b/system/CodeIgniter.php @@ -24,6 +24,7 @@ use CodeIgniter\HTTP\Request; use CodeIgniter\HTTP\ResponseInterface; use CodeIgniter\HTTP\URI; +use CodeIgniter\Router\Exceptions\RedirectException as DeprecatedRedirectException; use CodeIgniter\Router\RouteCollectionInterface; use CodeIgniter\Router\Router; use Config\App; @@ -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()); diff --git a/tests/system/CodeIgniterTest.php b/tests/system/CodeIgniterTest.php index ca084c0ed274..8f45881ba944 100644 --- a/tests/system/CodeIgniterTest.php +++ b/tests/system/CodeIgniterTest.php @@ -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; @@ -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', '/'];