Skip to content

Commit

Permalink
fix: define d() with CSP support
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjis committed Dec 18, 2021
1 parent c2cb09c commit a039389
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
35 changes: 35 additions & 0 deletions system/Common.php
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,41 @@ function dd(...$vars)
}
}

if (! function_exists('d')) {
/**
* Prints a Kint debug report with CSP support
*
* @param array ...$vars
*/
function d(...$vars)
{
Kint::$aliases[] = 'd';

ob_start();
Kint::dump(...$vars);
$output = ob_get_clean();

/** @var App $config */
$config = config(App::class);

if ($config->CSPEnabled === true) {
$output = str_replace(
[
'<script class="kint-rich-script">',
'<style class="kint-rich-style">',
],
[
'<script {csp-script-nonce} class="kint-rich-script">',
'<style {csp-style-nonce} class="kint-rich-style">',
],
$output
);
}

echo $output;
}
}

if (! function_exists('env')) {
/**
* Allows user to retrieve values from the environment
Expand Down
19 changes: 19 additions & 0 deletions tests/system/CommonFunctionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use Config\Logger;
use Config\Modules;
use InvalidArgumentException;
use Kint;
use stdClass;
use Tests\Support\Models\JobModel;

Expand Down Expand Up @@ -482,4 +483,22 @@ public function testIsCli()
$this->assertIsBool(is_cli());
$this->assertTrue(is_cli());
}

public function testD()
{
/** @var App $config */
$config = config(App::class);
$CSPEnabled = $config->CSPEnabled;
$cliDetection = Kint::$cli_detection;

$config->CSPEnabled = true;
Kint::$cli_detection = false;

$this->expectOutputRegex('/<script {csp-script-nonce} class="kint-rich-script">/u');
d('string');

// Restore settings
$config->CSPEnabled = $CSPEnabled;
Kint::$cli_detection = $cliDetection;
}
}

0 comments on commit a039389

Please sign in to comment.