Skip to content

Commit

Permalink
fix: Disable save previous URL
Browse files Browse the repository at this point in the history
  • Loading branch information
neznaika0 committed Nov 5, 2024
1 parent 463a474 commit d3c6cb6
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 0 deletions.
74 changes: 74 additions & 0 deletions src/CodeIgniter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace Michalsn\CodeIgniterHtmx;

use CodeIgniter\CodeIgniter as BaseCodeIgniter;
use CodeIgniter\HTTP\CLIRequest;
use CodeIgniter\HTTP\DownloadResponse;
use CodeIgniter\HTTP\IncomingRequest;
use CodeIgniter\HTTP\RedirectResponse;
use CodeIgniter\HTTP\URI;
use Michalsn\CodeIgniterHtmx\HTTP\IncomingRequest as HTMXIncomingRequest;

/**
* @property CLIRequest|HTMXIncomingRequest|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 and HTMX requests
if ((method_exists($this->request, 'isHTMX') && $this->request->isHTMX()) || (method_exists($this->request, 'isAJAX') && $this->request->isAJAX())) {
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()
));
}
}
}
17 changes: 17 additions & 0 deletions src/Config/Services.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Config\Services as AppServices;
use Config\Toolbar as ToolbarConfig;
use Config\View as ViewConfig;
use Michalsn\CodeIgniterHtmx\CodeIgniter;
use Michalsn\CodeIgniterHtmx\Debug\Toolbar;
use Michalsn\CodeIgniterHtmx\HTTP\IncomingRequest;
use Michalsn\CodeIgniterHtmx\HTTP\RedirectResponse;
Expand All @@ -19,6 +20,22 @@

class Services extends BaseService
{
/**
* CodeIgniter, the core of the framework.
*
* @return CodeIgniter
*/
public static function codeigniter(?App $config = null, bool $getShared = true)
{
if ($getShared) {
return static::getSharedInstance('codeigniter', $config);
}

$config ??= config(App::class);

return new CodeIgniter($config);
}

/**
* The Renderer class is the class that actually displays a file to the user.
* The default View class within CodeIgniter is intentionally simple, but this
Expand Down
55 changes: 55 additions & 0 deletions tests/CodeIgniterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?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 testIsHTMXNotSavePreviousURL(): void
{
// Default request behavior
$uri = service('uri');
$request = service('incomingrequest');

$uri->setPath('/')->setQuery('previous=original');

ob_start();
service('codeigniter', null, false)->setContext('web')->run();
ob_get_clean();

$this->assertArrayHasKey('_ci_previous_url', $_SESSION);
$this->assertSame('https://example.com/index.php/?previous=original', $_SESSION['_ci_previous_url']);

// HTMX request
$uri->setPath('/')->setQuery('previous=htmx');
$request->appendHeader('HX-Request', 'true');

ob_start();
service('codeigniter', null, false)->setContext('web')->run();
ob_get_clean();

$this->assertTrue($request->isHTMX());
$this->assertArrayHasKey('_ci_previous_url', $_SESSION);
$this->assertSame('https://example.com/index.php/?previous=original', $_SESSION['_ci_previous_url']);
}
}

0 comments on commit d3c6cb6

Please sign in to comment.