Skip to content

Commit

Permalink
Throw deprecation notice about unregistered functions without changin…
Browse files Browse the repository at this point in the history
…g the compilation flow (#985)

Fixes #964
  • Loading branch information
wisskid committed Apr 5, 2024
1 parent 42b869e commit 9656f55
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 11 deletions.
23 changes: 12 additions & 11 deletions libs/sysplugins/smarty_internal_templatecompilerbase.php
Expand Up @@ -640,17 +640,18 @@ public function compilePHPFunctionCall($name, $parameter)
return $func_name . '(' . $parameter[ 0 ] . ')';
}
} else {
$first_param = array_shift($parameter);
$modifier = array_merge(array($name), $parameter);
// Now, compile the function call as a modifier
return $this->compileTag(
'private_modifier',
array(),
array(
'modifierlist' => array($modifier),
'value' => $first_param
)
);

if (
!$this->smarty->loadPlugin('smarty_modifiercompiler_' . $name)
&& !isset($this->smarty->registered_plugins[Smarty::PLUGIN_MODIFIER][$name])
&& !in_array($name, ['time', 'join', 'is_array', 'in_array'])
) {
trigger_error('Using unregistered function "' . $name . '" in a template is deprecated and will be ' .
'removed in a future release. Use Smarty::registerPlugin to explicitly register ' .
'a custom modifier.', E_USER_DEPRECATED);
}

return $name . '(' . implode(',', $parameter) . ')';
}
} else {
$this->trigger_template_error("unknown function '{$name}'");
Expand Down
@@ -0,0 +1,67 @@
<?php

class ArgumentMustBePassedByReference961Test extends PHPUnit_Smarty
{

/**
* @group issue961
*/
public function testReset()
{
$smarty = new Smarty();
$smarty->registerPlugin('modifier', 'reset', 'reset');
$templateStr = "string:{reset(\$ar)}";
$smarty->assign('ar', [1,2,3]);
$this->assertEquals(
'1',
$smarty->fetch($templateStr)
);
}

/**
* @group issue961
* @deprecated
*/
public function testResetAsModifier()
{
$smarty = new Smarty();
try {
$templateStr = "string:{\$ar|reset}";
$smarty->assign('ar', [1,2,3]);
$this->assertEquals(
'1',
$smarty->fetch($templateStr)
);
} catch (Exception $e) {
}
}

/**
* @group issue961
*/
public function testResetInExpression()
{
$smarty = new Smarty();
$smarty->registerPlugin('modifier', 'reset', 'reset');
$templateStr = "string:{if reset(\$ar)}ok{/if}";
$smarty->assign('ar', [1,2,3]);
$this->assertEquals(
'ok',
$smarty->fetch($templateStr)
);
}

/**
* @group issue961
*/
public function testMatch()
{
$smarty = new Smarty();
$smarty->registerPlugin('modifier', 'preg_match', 'preg_match');
$templateStr = 'string:{assign var="match" value=null}{if preg_match(\'/([a-z]{4})/\', "a test", $match)}{$match.1}{/if}';
$this->assertEquals(
'test',
$smarty->fetch($templateStr)
);
}
}

0 comments on commit 9656f55

Please sign in to comment.