Classes

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

File io/template/parser/syntax/TextTemplateSyntaxElement.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: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 
<?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: 2014 12 22
 * @since       PHPBoost 3.0 - 2010 07 08
*/

class TextTemplateSyntaxElement extends AbstractTemplateSyntaxElement
{
    private $ended = false;
    private $escaped = false;

    public function parse(TemplateSyntaxParserContext $context, StringInputStream $input, StringOutputStream $output)
    {
        $this->register($context, $input, $output);
        $this->do_parse();
    }

    private function do_parse()
    {
        while ($this->input->has_next() && !$this->ended)
        {
            $current = $this->input->next();
            if (!$this->escaped)
            {
                $this->process_char($current);
            }
            else
            {
                $this->process_escaped_char($current);
            }
        }
    }

    private function process_char($char)
    {
        if ($char == '\\' && $this->input->assert_next('[\\{}$#]'))
        {
            $this->escaped = true;
        }
        else
        {
            $element = $this->parse_text($char);
            if ($element != null)
            {
                $this->parse_elt($element);
            }
            elseif (!$this->ended)
            {
                $this->write($char);
            }
        }
    }

    private function process_escaped_char($char)
    {
        if (!in_array($char, array('\\', '{', '}', '#', '$')))
        {
            $this->write('\\');
        }
        $this->escaped = false;
        $this->write($char);
    }

    private function write($char)
    {
        $this->output->write(addcslashes($char, '\\\''));
    }

    private function parse_text($current)
    {
        if ($current == '{' && $this->input->assert_next('(?:@(?:H\|)?)?(?:\w+\.)*\w+\}'))
        {
            return new VariableExpressionTemplateSyntaxElement();
        }
        elseif ($current == '$' && $this->input->assert_next('\{'))
        {
            return new ExpressionTemplateSyntaxElement();
        }
        elseif ($current == '#' && $this->input->assert_next('\{'))
        {
            return new FunctionCallTemplateSyntaxElement();
        }
        elseif ($current == '#' && $this->input->assert_next('[\s]'))
        {
            return $this->build_statement_elt();
        }
        elseif ($current == '<' && $this->input->assert_next('\?php'))
        {
            return new PHPTemplateSyntaxElement();
        }
        return null;
    }

    private function build_statement_elt()
    {
        $this->input->move(-1);
        if (ConditionTemplateSyntaxElement::is_element($this->input))
        {
            return new ConditionTemplateSyntaxElement();
        }
        elseif (LoopTemplateSyntaxElement::is_element($this->input))
        {
            return new LoopTemplateSyntaxElement();
        }
        elseif (IncludeTemplateSyntaxElement::is_element($this->input))
        {
            return new IncludeTemplateSyntaxElement();
        }
        else
        {
            $this->ended = true;
            return null;
        }
    }
}
?>