-
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.
Merge pull request #35 from michalsn/add-toolbar-decorator
add ToolbarDecorator
- Loading branch information
Showing
4 changed files
with
112 additions
and
1 deletion.
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,33 @@ | ||
<?php | ||
|
||
namespace Michalsn\CodeIgniterHtmx\View; | ||
|
||
use CodeIgniter\View\ViewDecoratorInterface; | ||
|
||
class ToolbarDecorator implements ViewDecoratorInterface | ||
{ | ||
public static function decorate(string $html): string | ||
{ | ||
if (CI_DEBUG | ||
&& (! is_cli() || ENVIRONMENT === 'testing') | ||
&& ! service('request')->isHtmx() | ||
&& str_contains($html, '</head>') | ||
&& ! str_contains($html, 'id="htmxToolbarScript"') | ||
) { | ||
$script = sprintf( | ||
'<script %s id="htmxToolbarScript">%s</script>', | ||
csp_script_nonce(), | ||
file_get_contents(__DIR__ . '/toolbar_decorator.js') | ||
); | ||
|
||
$html = preg_replace( | ||
'/<\/head>/', | ||
$script . '</head>', | ||
$html, | ||
1 | ||
); | ||
} | ||
|
||
return $html; | ||
} | ||
} |
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,10 @@ | ||
if (typeof window.htmx !== 'undefined' && | ||
typeof window.loadDoc !== 'undefined' && | ||
document.getElementById('debugbar_dynamic_script')) { | ||
htmx.on('htmx:afterSettle', function (event) { | ||
let debugBarTime = event.detail.xhr.getResponseHeader('debugbar-time'); | ||
if (debugBarTime !== null) { | ||
loadDoc(debugBarTime); | ||
} | ||
}); | ||
} |
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,62 @@ | ||
<?php | ||
|
||
namespace Tests\View; | ||
|
||
use CodeIgniter\Autoloader\FileLocator; | ||
use CodeIgniter\Config\Factories; | ||
use CodeIgniter\Config\Services; | ||
use CodeIgniter\Test\CIUnitTestCase; | ||
use CodeIgniter\View\View; | ||
use Config\View as ViewConfig; | ||
use Michalsn\CodeIgniterHtmx\View\ToolbarDecorator; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class ToolbarDecoratorTest extends CIUnitTestCase | ||
{ | ||
private FileLocator $loader; | ||
private string $viewsDir; | ||
private ViewConfig $config; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->loader = Services::locator(); | ||
$this->viewsDir = SUPPORTPATH . 'Views'; | ||
$this->config = new ViewConfig(); | ||
} | ||
|
||
public function testDecoratorDontApply(): void | ||
{ | ||
$config = $this->config; | ||
$config->decorators = [ToolbarDecorator::class]; | ||
Factories::injectMock('config', 'View', $config); | ||
|
||
$view = new View($this->config, $this->viewsDir, $this->loader); | ||
|
||
$view->setVar('testString', 'Hello World'); | ||
$expected1 = '<h1>Hello World</h1>'; | ||
$expected2 = 'id="htmxToolbarScript"'; | ||
|
||
$this->assertStringContainsString($expected1, $view->render('without_decorator')); | ||
$this->assertStringNotContainsString($expected2, $view->render('without_decorator')); | ||
} | ||
|
||
public function testDecoratorApply(): void | ||
{ | ||
$config = $this->config; | ||
$config->decorators = [ToolbarDecorator::class]; | ||
Factories::injectMock('config', 'View', $config); | ||
|
||
$view = new View($this->config, $this->viewsDir, $this->loader); | ||
|
||
$view->setVar('testString', 'Hello World'); | ||
$expected1 = '</script></head><body>Hello World'; | ||
$expected2 = 'id="htmxToolbarScript"'; | ||
|
||
$this->assertStringContainsString($expected1, $view->render('with_decorator')); | ||
$this->assertStringContainsString($expected2, $view->render('with_decorator')); | ||
} | ||
} |