/plugins/BHAML/lib/MtHaml/NodeVisitor/PhpRenderer.php
PHP | 235 lines | 189 code | 41 blank | 5 comment | 29 complexity | 21ad078430908bba6d8dc14ac377f403 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause
- <?php
- namespace MtHaml\NodeVisitor;
- use MtHaml\Node\Insert;
- use MtHaml\Node\Run;
- use MtHaml\Node\InterpolatedString;
- use MtHaml\Node\Tag;
- use MtHaml\Node\ObjectRefClass;
- use MtHaml\Node\NodeAbstract;
- use MtHaml\Node\ObjectRefId;
- use MtHaml\Node\TagAttributeInterpolation;
- use MtHaml\Node\TagAttributeList;
- class PhpRenderer extends RendererAbstract
- {
- protected function escapeLanguage($string)
- {
- return preg_replace('~(^\?|<\?)~', "<?php echo '\\1'; ?>", $string);
- }
- protected function stringLiteral($string)
- {
- return var_export((string)$string, true);
- }
- public function enterInterpolatedString(InterpolatedString $node)
- {
- if (!$this->isEchoMode() && 1 < count($node->getChilds())) {
- $this->raw('(');
- }
- }
- public function betweenInterpolatedStringChilds(InterpolatedString $node)
- {
- if (!$this->isEchoMode()) {
- $this->raw(' . ');
- }
- }
- public function leaveInterpolatedString(InterpolatedString $node)
- {
- if (!$this->isEchoMode() && 1 < count($node->getChilds())) {
- $this->raw(')');
- }
- }
- public function enterInsert(Insert $node)
- {
- $content = $node->getContent();
- $content = $this->trimInlineComments($content);
- if ($this->isEchoMode()) {
- $fmt = '<?php echo %s; ?>';
- //ADDED: FULLERON
- if (!\BApp::i()->get('BHAML/disable_escaping') && $node->getEscaping()->isEnabled()) {
- if ($node->getEscaping()->isOnce()) {
- $fmt = "<?php echo htmlspecialchars(%s,ENT_QUOTES,'%s',false); ?>";
- } else {
- $fmt = "<?php echo htmlspecialchars(%s,ENT_QUOTES,'%s'); ?>";
- }
- }
- $this->addDebugInfos($node);
- $this->raw(sprintf($fmt, $content, $this->charset));
- } else {
- $content = $node->getContent();
- if (!preg_match('~^\$?[a-zA-Z0-9_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$~', $content)) {
- $this->raw('(' . $content . ')');
- } else {
- $this->raw($content);
- }
- }
- }
- public function enterTopBlock(Run $node)
- {
- $this->addDebugInfos($node);
- $content = $this->trimInlineComments($node->getContent());
- if (!$node->isBlock()) {
- if (preg_match('~[:;]\s*$~', $content)) {
- $this->write(sprintf('<?php %s ?>' , $content));
- } else {
- $this->write(sprintf('<?php %s; ?>' , $content));
- }
- } else {
- $this->write(sprintf('<?php %s { ?>' , $content));
- }
- }
- public function enterMidBlock(Run $node)
- {
- $this->addDebugInfos($node);
- $content = $this->trimInlineComments($node->getContent());
- $this->write(sprintf('<?php } %s { ?>' , $content));
- }
- public function leaveTopBlock(Run $node)
- {
- if ($node->isBlock()) {
- $this->write('<?php } ?>');
- }
- }
- public function enterObjectRefClass(ObjectRefClass $node)
- {
- if ($this->isEchoMode()) {
- $this->raw('<?php echo ');
- }
- $this->raw('MtHaml\Runtime::renderObjectRefClass(');
- $this->pushEchoMode(false);
- }
- public function leaveObjectRefClass(ObjectRefClass $node)
- {
- $this->raw(')');
- $this->popEchoMode(true);
- if ($this->isEchoMode()) {
- $this->raw('; ?>');
- }
- }
- public function enterObjectRefId(ObjectRefId $node)
- {
- if ($this->isEchoMode()) {
- $this->raw('<?php echo ');
- }
- $this->raw('MtHaml\Runtime::renderObjectRefId(');
- $this->pushEchoMode(false);
- }
- public function leaveObjectRefId(ObjectRefId $node)
- {
- $this->raw(')');
- $this->popEchoMode(true);
- if ($this->isEchoMode()) {
- $this->raw('; ?>');
- }
- }
- public function enterObjectRefPrefix(NodeAbstract $node)
- {
- $this->raw(', ');
- }
- protected function writeDebugInfos($lineno)
- {
- }
- protected function renderDynamicAttributes(Tag $tag)
- {
- $list = array();
- $n = 0;
- $this->raw(' <?php echo MtHaml\Runtime::renderAttributes(array(');
- $this->setEchoMode(false);
- foreach ($tag->getAttributes() as $attr) {
- if (0 !== $n) {
- $this->raw(', ');
- }
- if ($attr instanceof TagAttributeInterpolation) {
- $this->raw('MtHaml\Runtime\AttributeInterpolation::create(');
- $attr->getValue()->accept($this);
- $this->raw(')');
- } else if ($attr instanceof TagAttributeList) {
- $this->raw('MtHaml\Runtime\AttributeList::create(');
- $attr->getValue()->accept($this);
- $this->raw(')');
- } else {
- $this->raw('array(');
- $attr->getName()->accept($this);
- $this->raw(', ');
- if ($value = $attr->getValue()) {
- $attr->getValue()->accept($this);
- } else {
- $this->raw('TRUE');
- }
- $this->raw(')');
- }
- ++$n;
- }
- $this->raw(')');
- $this->setEchoMode(true);
- $this->raw(', ');
- $this->raw($this->stringLiteral($this->env->getOption('format')));
- $this->raw(', ');
- $this->raw($this->stringLiteral($this->charset));
- $this->raw('); ?>');
- }
- public function trimInlineComments($code)
- {
- // Removes inlines comments ('//' and '#'), while ignoring '//' and '#'
- // embedded in quoted strings.
- $re = "!
- (?P<code>
- (?P<expr>(?:
- # anything except \", ', `
- [^\"'`]
- # double quoted string
- | \"(?: [^\"\\\\]+ | \\\\. )*\"
- # single quoted string
- | '(?: [^'\\\\]+ | \\\\. )*'
- # backticks string
- | `(?: [^`\\\\]+ | \\\\. )*`
- )+?)
- )
- (?P<comment>\s*(?://|\#).*)?
- $!xA";
- return preg_replace($re, '$1', $code);
- }
- }