Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 50 additions & 28 deletions src/Compile/FunctionCallCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,44 @@
namespace Smarty\Compile;

use Smarty\Compiler\Template;
use Smarty\CompilerException;
use Smarty\FunctionHandler\AttributeFunctionHandlerInterface;

/**
* Smarty Internal Plugin Compile Registered Function Class
*
*/
class FunctionCallCompiler extends Base {
class FunctionCallCompiler extends Base
{
/**
* Array of names of required attribute required by tag
*
* @var array
*/
protected $required_attributes = [];
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this does not change $required_attributes in Base.


/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see BasePlugin
*/
public $optional_attributes = ['_any'];

/**
* Shorttag attribute order defined by its names
*
* @var array
*/
protected $shorttag_order = [];

/**
* Array of names of valid option flags
*
* @var array
*/
protected $option_flags = ['nocache'];

/**
* Compiles code for the execution of a registered function
*
Expand All @@ -29,10 +62,22 @@ class FunctionCallCompiler extends Base {
*/
public function compile($args, Template $compiler, $parameter = [], $tag = null, $function = null): string
{
// Compile arguments to pass on the the functionhandler
$_params = $this->compileArguments($args);

if ($functionHandler = $compiler->getSmarty()->getFunctionHandler($function)) {
// add attributes of the function handler.
if ($functionHandler instanceof AttributeFunctionHandlerInterface) {
$supported_attributes = $functionHandler->getSupportedAttributes();

foreach (['required_attributes', 'optional_attributes', 'shorttag_order', 'option_flags'] as $property) {
$this->$property = $supported_attributes[$property];
}
}

// check and get attributes
$_attr = $this->getAttributes($compiler, $args);
unset($_attr['nocache']);

$_paramsArray = $this->formatParamsArray($_attr);
$_params = 'array(' . implode(',', $_paramsArray) . ')';

// not cacheable?
$compiler->tag_nocache = $compiler->tag_nocache || !$functionHandler->isCacheable();
Expand All @@ -48,27 +93,4 @@ public function compile($args, Template $compiler, $parameter = [], $tag = null,

return $output;
}

/**
* Recursively compile function arguments.
* @param array $args array with attributes from parser
* @return string compiled arguments
*/
private function compileArguments(array $arguments): string
{
$params = '';

foreach ($arguments as $key => $value) {
$params .= var_export($key, true) . "=>";
if (is_array($value)) {
$params .= $this->compileArguments($value);
} else {
$params .= $value;
}

$params .= ',';
}

return '[' . rtrim($params, ',') . ']';
}
}
}
77 changes: 77 additions & 0 deletions src/FunctionHandler/AttributeBase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

namespace Smarty\FunctionHandler;

use Smarty\Template;

/**
* Abstract implementation for function handlers which support custom attributes
*/
abstract class AttributeBase implements AttributeFunctionHandlerInterface
{
/**
* Array of names of required attribute required by tag
*
* @var array
*/
protected array $required_attributes = [];

/**
* Array of names of optional attribute required by tag
* use array('_any') if there is no restriction of attributes names
*
* @var array
*/
protected array $optional_attributes = [];

/**
* Shorttag attribute order defined by its names
*
* @var array
*/
protected array $shorttag_order = [];

/**
* Array of names of valid option flags
*
* @var array
*/
protected array $option_flags = [];

/**
* Return whether the output is cacheable.
* @var bool
*/
protected bool $cacheable = true;

/**
* Return whether the output is cacheable.
* @return bool
*/
public function isCacheable(): bool
{
return $this->cacheable;
}

/**
* Function body
* @param mixed $params The supplied parameters.
* @param Smarty\Template $template
* @return mixed
*/
abstract public function handle($params, Template $template): ?string;

/**
* Return the support attributes for this function.
* @return array<string, array>
*/
public function getSupportedAttributes(): array
{
return [
'required_attributes' => $this->required_attributes,
'optional_attributes' => $this->optional_attributes,
'shorttag_order' => $this->shorttag_order,
'option_flags' => $this->option_flags,
];
}
}
15 changes: 15 additions & 0 deletions src/FunctionHandler/AttributeFunctionHandlerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Smarty\FunctionHandler;

/**
* Function handler interface with support for specifying supported properties
*/
interface AttributeFunctionHandlerInterface extends FunctionHandlerInterface
{
/**
* Returns an array with the supported attributes, flags, and shorttags
* @return array<string, array>
*/
public function getSupportedAttributes(): array;
}
Loading