Classes

File io/template/parser/syntax/FunctionTemplateSyntaxElement.class.php

File io/template/parser/syntax/FunctionTemplateSyntaxElement.class.php

  1:   2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18:  19:  20:  21:  22:  23:  24:  25:  26:  27:  28:  29:  30:  31:  32:  33:  34:  35:  36:  37:  38:  39:  40:  41:  42:  43:  44:  45:  46:  47:  48:  49:  50:  51:  52:  53:  54:  55:  56:  57:  58:  59:  60:  61:  62:  63:  64:  65:  66:  67:  68:  69:  70:  71:  72:  73:  74:  75:  76:  77:  78:  79:  80:  81:  82:  83:  84:  85:  86:  87:  88:  89:  90:  91:  92:  93:  94:  95:  96:  97:  98:  99: 100: 101: 102: 
<?php
/**
 * @package     IO
 * @subpackage  Template\parser\syntax
 * @copyright   &copy; 2005-2019 PHPBoost
 * @license     https://www.gnu.org/licenses/gpl-3.0.html GNU/GPL-3.0
 * @author      Loic ROUCHON <horn@phpboost.com>
 * @version     PHPBoost 5.2 - last update: 2016 10 28
 * @since       PHPBoost 3.0 - 2010 09 04
 * @contributor Julien BRISWALTER <j1.seth@phpboost.com>
 * @contributor Arnaud GENET <elenwii@phpboost.com>
*/

class FunctionTemplateSyntaxElement extends AbstractTemplateSyntaxElement
{
    public static function is_element(StringInputStream $input)
    {
        return $input->assert_next('\s*(?:\w+::)?\w+\(\s*');
    }

    public function parse(TemplateSyntaxParserContext $context, StringInputStream $input, StringOutputStream $output)
    {
        $this->register($context, $input, $output);
        $matches = array();
        if ($input->consume_next('(?:(?P<class>\w+)::)?(?P<method>\w+)\(', '', $matches))
        {
            $class = $matches['class'];
            $method = $matches['method'];
            $this->check_method_call($class, $method);
            $this->write($class, $method);
            $this->parameters();
            $this->end();
        }
        else
        {
            throw new TemplateRenderingException('invalid function call', $input);
        }
    }

    private function check_method_call($class, $method)
    {
        if (empty($class))
        {
            if (!method_exists('TemplateFunctions', $method))
            {
                throw new TemplateRenderingException('Unauthorized method call. Only ' . implode(', ', get_class_methods('TemplateFunctions')) .
                    ' functions calls and static methods calls are allowed', $this->input);
            }
        }
        elseif ($this->is_php_function($class))
        {
            if (!function_exists($method))
            {
                throw new TemplateRenderingException('PHP function ' . $method . '() does not exist', $this->input);
            }
        }
        elseif (!method_exists($class, $method))
        {
            throw new TemplateRenderingException('Static method ' . $class . '::' . $method . '() does not exist', $this->input);
        }
    }

    private function write($class, $method)
    {
        if (!empty($class))
        {
            if (!$this->is_php_function($class))
            {
                $this->output->write($class . '::');
            }
        }
        else
        {
            $this->output->write(TemplateSyntaxElement::FUNCTIONS . '->');
        }
        $this->output->write($method . '(');
    }

    private function parameters()
    {
        $this->parse_elt(new ParametersTemplateSyntaxElement());
    }

    private function end()
    {
        if (!$this->input->consume_next('\)'))
        {
            throw new TemplateRenderingException('invalid function call: missing enclosing parenthesis', $this->input);
        }
        $this->output->write(')');
    }

    private function is_php_function($prefix)
    {
        return TextHelper::strtoupper($prefix) == 'PHP';
    }
}

class InvalidTemplateFunctionCallException extends TemplateRenderingException {}

?>