Skip to content

Commit

Permalink
Fixed failing {debug} tag. (#923)
Browse files Browse the repository at this point in the history
Fixes #922
  • Loading branch information
wisskid committed Jan 1, 2024
1 parent f2a6636 commit 08c9066
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 52 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Expand Up @@ -6,8 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [5.0.0-rc2] - 2023-11-11
### Fixed
- The {debug} tag was broken in v5 [#922](https://github.com/smarty-php/smarty/issues/922)

## [5.0.0-rc2] - 2023-11-11

### Fixed
- Registered output filters wouldn't run [#899](https://github.com/smarty-php/smarty/issues/899)
Expand Down
83 changes: 37 additions & 46 deletions src/Debug.php
Expand Up @@ -6,16 +6,7 @@
* Smarty Internal Plugin Debug
* Class to collect data for the Smarty Debugging Console
*
* @author Uwe Tews
*/

/**
* Smarty Internal Plugin Debug Class
*
*/
class Debug extends Data
{
Expand All @@ -24,14 +15,14 @@ class Debug extends Data
*
* @var array
*/
public $template_data = array();
public $template_data = [];

/**
* List of uid's which shall be ignored
*
* @var array
*/
public $ignore_uid = array();
public $ignore_uid = [];

/**
* Index of display() and fetch() calls
Expand All @@ -50,10 +41,10 @@ class Debug extends Data
/**
* Start logging template
*
* @param \Smarty\Template $template template
* @param Template $template template
* @param null $mode true: display false: fetch null: subtemplate
*/
public function start_template(\Smarty\Template $template, $mode = null)
public function start_template(Template $template, $mode = null)
{
if (isset($mode) && !$template->_isSubTpl()) {
$this->index++;
Expand All @@ -67,9 +58,9 @@ public function start_template(\Smarty\Template $template, $mode = null)
/**
* End logging of cache time
*
* @param \Smarty\Template $template cached template
* @param Template $template cached template
*/
public function end_template(\Smarty\Template $template)
public function end_template(Template $template)
{
$key = $this->get_key($template);
$this->template_data[ $this->index ][ $key ][ 'total_time' ] +=
Expand All @@ -79,15 +70,15 @@ public function end_template(\Smarty\Template $template)
/**
* Start logging of compile time
*
* @param \Smarty\Template $template
* @param Template $template
*/
public function start_compile(\Smarty\Template $template)
public function start_compile(Template $template)
{
static $_is_stringy = array('string' => true, 'eval' => true);
if (!empty($template->getCompiler()->trace_uid)) {
$key = $template->getCompiler()->trace_uid;
if (!isset($this->template_data[ $this->index ][ $key ])) {
$this->saveTemplateData($_is_stringy, $template, $key);
$this->saveTemplateData($_is_stringy, $template, $key);
}
} else {
if (isset($this->ignore_uid[ $template->getSource()->uid ])) {
Expand All @@ -101,9 +92,9 @@ public function start_compile(\Smarty\Template $template)
/**
* End logging of compile time
*
* @param \Smarty\Template $template
* @param Template $template
*/
public function end_compile(\Smarty\Template $template)
public function end_compile(Template $template)
{
if (!empty($template->getCompiler()->trace_uid)) {
$key = $template->getCompiler()->trace_uid;
Expand All @@ -120,9 +111,9 @@ public function end_compile(\Smarty\Template $template)
/**
* Start logging of render time
*
* @param \Smarty\Template $template
* @param Template $template
*/
public function start_render(\Smarty\Template $template)
public function start_render(Template $template)
{
$key = $this->get_key($template);
$this->template_data[ $this->index ][ $key ][ 'start_time' ] = microtime(true);
Expand All @@ -131,9 +122,9 @@ public function start_render(\Smarty\Template $template)
/**
* End logging of compile time
*
* @param \Smarty\Template $template
* @param Template $template
*/
public function end_render(\Smarty\Template $template)
public function end_render(Template $template)
{
$key = $this->get_key($template);
$this->template_data[ $this->index ][ $key ][ 'render_time' ] +=
Expand All @@ -143,9 +134,9 @@ public function end_render(\Smarty\Template $template)
/**
* Start logging of cache time
*
* @param \Smarty\Template $template cached template
* @param Template $template cached template
*/
public function start_cache(\Smarty\Template $template)
public function start_cache(Template $template)
{
$key = $this->get_key($template);
$this->template_data[ $this->index ][ $key ][ 'start_time' ] = microtime(true);
Expand All @@ -154,9 +145,9 @@ public function start_cache(\Smarty\Template $template)
/**
* End logging of cache time
*
* @param \Smarty\Template $template cached template
* @param Template $template cached template
*/
public function end_cache(\Smarty\Template $template)
public function end_cache(Template $template)
{
$key = $this->get_key($template);
$this->template_data[ $this->index ][ $key ][ 'cache_time' ] +=
Expand All @@ -166,9 +157,9 @@ public function end_cache(\Smarty\Template $template)
/**
* Register template object
*
* @param \Smarty\Template $template cached template
* @param Template $template cached template
*/
public function register_template(\Smarty\Template $template)
public function register_template(Template $template)
{
}

Expand All @@ -184,13 +175,13 @@ public static function register_data(Data $data)
/**
* Opens a window for the Smarty Debugging Console and display the data
*
* @param \Smarty\Template|\Smarty $obj object to debug
* @param Template|Smarty $obj object to debug
* @param bool $full
*
* @throws \Exception
* @throws \Smarty\Exception
* @throws Exception
*/
public function display_debug($obj, $full = false)
public function display_debug($obj, bool $full = false)
{
if (!$full) {
$this->offset++;
Expand All @@ -200,18 +191,18 @@ public function display_debug($obj, $full = false)
$smarty = $obj->getSmarty();
// create fresh instance of smarty for displaying the debug console
// to avoid problems if the application did overload the Smarty class
$debObj = new \Smarty\Smarty();
$debObj = new Smarty();
// copy the working dirs from application
$debObj->setCompileDir($smarty->getCompileDir());
$debObj->compile_check = \Smarty::COMPILECHECK_ON;
$debObj->compile_check = Smarty::COMPILECHECK_ON;
$debObj->security_policy = null;
$debObj->debugging = false;
$debObj->debugging_ctrl = 'NONE';
$debObj->error_reporting = E_ALL & ~E_NOTICE;
$debObj->debug_tpl = $smarty->debug_tpl ?? 'file:' . __DIR__ . '/../debug.tpl';
$debObj->debug_tpl = $smarty->debug_tpl ?? 'file:' . __DIR__ . '/debug.tpl';
$debObj->registered_resources = array();
$debObj->escape_html = true;
$debObj->caching = \Smarty::CACHING_OFF;
$debObj->caching = Smarty::CACHING_OFF;
// prepare information of assigned variables
$ptr = $this->get_debug_vars($obj);
$_assigned_vars = $ptr->tpl_vars;
Expand All @@ -223,7 +214,7 @@ public function display_debug($obj, $full = false)
$displayMode = $debugging === 2 || !$full;
$offset = $this->offset * 50;
$_template = $debObj->doCreateTemplate($debObj->debug_tpl);
if ($obj instanceof \Smarty\Template) {
if ($obj instanceof Template) {
$_template->assign('template_name', $templateName);
} elseif ($obj instanceof Smarty || $full) {
$_template->assign('template_data', $this->template_data[$this->index]);
Expand Down Expand Up @@ -298,11 +289,11 @@ private function get_debug_vars($obj)
/**
* Return key into $template_data for template
*
* @param \Smarty\Template $template template object
* @param Template $template template object
*
* @return string key into $template_data
*/
private function get_key(\Smarty\Template $template)
private function get_key(Template $template)
{
static $_is_stringy = array('string' => true, 'eval' => true);

Expand All @@ -319,19 +310,19 @@ private function get_key(\Smarty\Template $template)
/**
* Ignore template
*
* @param \Smarty\Template $template
* @param Template $template
*/
public function ignore(\Smarty\Template $template)
public function ignore(Template $template)
{
$this->ignore_uid[$template->getSource()->uid] = true;
}

/**
* handle 'URL' debugging mode
*
* @param \Smarty $smarty
* @param Smarty $smarty
*/
public function debugUrl(\Smarty $smarty)
public function debugUrl(Smarty $smarty)
{
if (isset($_SERVER[ 'QUERY_STRING' ])) {
$_query_string = $_SERVER[ 'QUERY_STRING' ];
Expand Down Expand Up @@ -360,12 +351,12 @@ public function debugUrl(\Smarty $smarty)

/**
* @param array $_is_stringy
* @param \Smarty\Template $template
* @param Template $template
* @param string $key
*
* @return void
*/
private function saveTemplateData(array $_is_stringy, \Smarty\Template $template, string $key): void {
private function saveTemplateData(array $_is_stringy, Template $template, string $key): void {
if (isset($_is_stringy[$template->getSource()->type])) {
$this->template_data[$this->index][$key]['name'] =
'\'' . substr($template->getSource()->name, 0, 25) . '...\'';
Expand Down
5 changes: 3 additions & 2 deletions src/Smarty.php
Expand Up @@ -251,9 +251,10 @@ class Smarty extends \Smarty\TemplateBase {

/**
* debug mode
* Setting this to true enables the debug-console.
* Setting this to true enables the debug-console. Setting it to 2 enables individual Debug Console window by
* template name.
*
* @var boolean
* @var boolean|int
*/
public $debugging = false;

Expand Down
6 changes: 3 additions & 3 deletions src/debug.tpl
@@ -1,4 +1,4 @@
{capture name='_smarty_debug' assign=debug_output}
{capture name='_smarty_debug' assign='debug_output'}
<!DOCTYPE html>
<html lang="en">
<head>
Expand Down Expand Up @@ -108,7 +108,7 @@
</head>
<body>
<h1>Smarty {Smarty::SMARTY_VERSION} Debug Console
<h1>Smarty {\Smarty\Smarty::SMARTY_VERSION} Debug Console
- {if isset($template_name)}{$template_name|debug_print_var nofilter} {/if}{if !empty($template_data)}Total Time {$execution_time|string_format:"%.5f"}{/if}</h1>
{if !empty($template_data)}
Expand Down Expand Up @@ -166,7 +166,7 @@
</body>
</html>
{/capture}
<script type="text/javascript">
<script>
_smarty_console = window.open("", "console{$targetWindow}", "width=1024,height=600,left={$offset},top={$offset},resizable,scrollbars=yes");
_smarty_console.document.write("{$debug_output|escape:'javascript' nofilter}");
_smarty_console.document.close();
Expand Down
22 changes: 22 additions & 0 deletions tests/UnitTests/TemplateSource/TagTests/Debug/DebugTest.php
@@ -0,0 +1,22 @@
<?php

namespace UnitTests\TemplateSource\TagTests\Debug;
use PHPUnit_Smarty;
use Smarty\Smarty;

/**
* Smarty PHPunit tests of {debug} tag
*/
class PluginModifierStripTagsTest extends PHPUnit_Smarty {

public function setUp(): void {
$this->setUpSmarty(__DIR__);
}
public function testDefault() {
$tpl = $this->smarty->createTemplate('eval:{debug}');
$output = $this->smarty->fetch($tpl);
$this->assertStringContainsString("<script", $output);
$this->assertStringContainsString("</script>", $output);
}

}

0 comments on commit 08c9066

Please sign in to comment.