Skip to content

Commit

Permalink
Merge pull request #35 from michalsn/add-toolbar-decorator
Browse files Browse the repository at this point in the history
add ToolbarDecorator
  • Loading branch information
michalsn authored Jun 6, 2023
2 parents f24f6ed + ae4277a commit c03b86b
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/Config/Registrar.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@

namespace Michalsn\CodeIgniterHtmx\Config;

use Michalsn\CodeIgniterHtmx\View\ErrorModalDecorator;
use Michalsn\CodeIgniterHtmx\View\ToolbarDecorator;

class Registrar
{
public static function View(): array
{
return [
'decorators' => ['Michalsn\CodeIgniterHtmx\View\ErrorModalDecorator'],
'decorators' => [
ToolbarDecorator::class,
ErrorModalDecorator::class,
],
];
}
}
33 changes: 33 additions & 0 deletions src/View/ToolbarDecorator.php
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;
}
}
10 changes: 10 additions & 0 deletions src/View/toolbar_decorator.js
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);
}
});
}
62 changes: 62 additions & 0 deletions tests/View/ToolbarDecoratorTest.php
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'));
}
}

0 comments on commit c03b86b

Please sign in to comment.