-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add option to disable previous URL storage (#82)
* fix: Disable save previous URL * docs: Update configuration.md * feat: Add in config parameter `storePreviousURL`
- Loading branch information
Showing
5 changed files
with
176 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
|
||
namespace Michalsn\CodeIgniterHtmx; | ||
|
||
use CodeIgniter\CodeIgniter as BaseCodeIgniter; | ||
use CodeIgniter\HTTP\CLIRequest; | ||
use CodeIgniter\HTTP\DownloadResponse; | ||
use CodeIgniter\HTTP\RedirectResponse; | ||
use CodeIgniter\HTTP\URI; | ||
use Michalsn\CodeIgniterHtmx\HTTP\IncomingRequest; | ||
|
||
/** | ||
* @property CLIRequest|IncomingRequest|null $request | ||
*/ | ||
class CodeIgniter extends BaseCodeIgniter | ||
{ | ||
/** | ||
* Web access? | ||
*/ | ||
private function isWeb(): bool | ||
{ | ||
return $this->context === 'web'; | ||
} | ||
|
||
/** | ||
* If we have a session object to use, store the current URI | ||
* as the previous URI. This is called just prior to sending the | ||
* response to the client, and will make it available next request. | ||
* | ||
* This helps provider safer, more reliable previous_url() detection. | ||
* | ||
* @param string|URI $uri | ||
* | ||
* @return void | ||
*/ | ||
public function storePreviousURL($uri) | ||
{ | ||
// Ignore CLI requests | ||
if (! $this->isWeb()) { | ||
return; | ||
} | ||
|
||
// Ignore AJAX requests | ||
if (method_exists($this->request, 'isAJAX') && $this->request->isAJAX()) { | ||
return; | ||
} | ||
|
||
// Ignore HTMX requests | ||
if (config('Htmx')->storePreviousURL === false | ||
&& method_exists($this->request, 'isHTMX') && $this->request->isHTMX()) { | ||
return; | ||
} | ||
|
||
// Ignore unroutable responses | ||
if ($this->response instanceof DownloadResponse || $this->response instanceof RedirectResponse) { | ||
return; | ||
} | ||
|
||
// Ignore non-HTML responses | ||
if (! str_contains($this->response->getHeaderLine('Content-Type'), 'text/html')) { | ||
return; | ||
} | ||
|
||
// This is mainly needed during testing... | ||
if (is_string($uri)) { | ||
$uri = service('uri', $uri, false); | ||
} | ||
|
||
if (isset($_SESSION)) { | ||
session()->set('_ci_previous_url', URI::createURIString( | ||
$uri->getScheme(), | ||
$uri->getAuthority(), | ||
$uri->getPath(), | ||
$uri->getQuery(), | ||
$uri->getFragment() | ||
)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests; | ||
|
||
use CodeIgniter\Test\CIUnitTestCase; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class CodeIgniterTest extends CIUnitTestCase | ||
{ | ||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->resetServices(); | ||
} | ||
|
||
protected function tearDown(): void | ||
{ | ||
parent::tearDown(); | ||
|
||
$this->resetServices(); | ||
} | ||
|
||
public function testStorePreviousURLIsHTMX(): void | ||
{ | ||
$_SESSION['_ci_previous_url'] = 'https://example.com/index.php/?previous=original'; | ||
|
||
config('Htmx')->storePreviousURL = true; | ||
|
||
service('uri')->setPath('/')->setQuery('previous=saved_from_htmx'); | ||
service('request')->appendHeader('HX-Request', 'true'); | ||
|
||
ob_start(); | ||
service('codeigniter', null, false)->setContext('web')->run(); | ||
ob_get_clean(); | ||
|
||
$this->assertTrue(service('request')->isHTMX()); | ||
$this->assertArrayHasKey('_ci_previous_url', $_SESSION); | ||
$this->assertSame('https://example.com/index.php/?previous=saved_from_htmx', $_SESSION['_ci_previous_url']); | ||
} | ||
|
||
public function testDontStorePreviousURLIsHTMX(): void | ||
{ | ||
$_SESSION['_ci_previous_url'] = 'https://example.com/index.php/?previous=original'; | ||
|
||
service('uri')->setPath('/')->setQuery('previous=original'); | ||
|
||
config('Htmx')->storePreviousURL = false; | ||
|
||
service('uri')->setPath('/')->setQuery('previous=not_saved_from_htmx'); | ||
service('request')->appendHeader('HX-Request', 'true'); | ||
|
||
ob_start(); | ||
service('codeigniter', null, false)->setContext('web')->run(); | ||
ob_get_clean(); | ||
|
||
$this->assertTrue(service('request')->isHTMX()); | ||
$this->assertArrayHasKey('_ci_previous_url', $_SESSION); | ||
$this->assertSame('https://example.com/index.php/?previous=original', $_SESSION['_ci_previous_url']); | ||
} | ||
} |