PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/BHAML/lib/MtHaml/NodeVisitor/PhpRenderer.php

http://github.com/unirgy/buckyball
PHP | 235 lines | 189 code | 41 blank | 5 comment | 29 complexity | 21ad078430908bba6d8dc14ac377f403 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause
  1. <?php
  2. namespace MtHaml\NodeVisitor;
  3. use MtHaml\Node\Insert;
  4. use MtHaml\Node\Run;
  5. use MtHaml\Node\InterpolatedString;
  6. use MtHaml\Node\Tag;
  7. use MtHaml\Node\ObjectRefClass;
  8. use MtHaml\Node\NodeAbstract;
  9. use MtHaml\Node\ObjectRefId;
  10. use MtHaml\Node\TagAttributeInterpolation;
  11. use MtHaml\Node\TagAttributeList;
  12. class PhpRenderer extends RendererAbstract
  13. {
  14. protected function escapeLanguage($string)
  15. {
  16. return preg_replace('~(^\?|<\?)~', "<?php echo '\\1'; ?>", $string);
  17. }
  18. protected function stringLiteral($string)
  19. {
  20. return var_export((string)$string, true);
  21. }
  22. public function enterInterpolatedString(InterpolatedString $node)
  23. {
  24. if (!$this->isEchoMode() && 1 < count($node->getChilds())) {
  25. $this->raw('(');
  26. }
  27. }
  28. public function betweenInterpolatedStringChilds(InterpolatedString $node)
  29. {
  30. if (!$this->isEchoMode()) {
  31. $this->raw(' . ');
  32. }
  33. }
  34. public function leaveInterpolatedString(InterpolatedString $node)
  35. {
  36. if (!$this->isEchoMode() && 1 < count($node->getChilds())) {
  37. $this->raw(')');
  38. }
  39. }
  40. public function enterInsert(Insert $node)
  41. {
  42. $content = $node->getContent();
  43. $content = $this->trimInlineComments($content);
  44. if ($this->isEchoMode()) {
  45. $fmt = '<?php echo %s; ?>';
  46. //ADDED: FULLERON
  47. if (!\BApp::i()->get('BHAML/disable_escaping') && $node->getEscaping()->isEnabled()) {
  48. if ($node->getEscaping()->isOnce()) {
  49. $fmt = "<?php echo htmlspecialchars(%s,ENT_QUOTES,'%s',false); ?>";
  50. } else {
  51. $fmt = "<?php echo htmlspecialchars(%s,ENT_QUOTES,'%s'); ?>";
  52. }
  53. }
  54. $this->addDebugInfos($node);
  55. $this->raw(sprintf($fmt, $content, $this->charset));
  56. } else {
  57. $content = $node->getContent();
  58. if (!preg_match('~^\$?[a-zA-Z0-9_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$~', $content)) {
  59. $this->raw('(' . $content . ')');
  60. } else {
  61. $this->raw($content);
  62. }
  63. }
  64. }
  65. public function enterTopBlock(Run $node)
  66. {
  67. $this->addDebugInfos($node);
  68. $content = $this->trimInlineComments($node->getContent());
  69. if (!$node->isBlock()) {
  70. if (preg_match('~[:;]\s*$~', $content)) {
  71. $this->write(sprintf('<?php %s ?>' , $content));
  72. } else {
  73. $this->write(sprintf('<?php %s; ?>' , $content));
  74. }
  75. } else {
  76. $this->write(sprintf('<?php %s { ?>' , $content));
  77. }
  78. }
  79. public function enterMidBlock(Run $node)
  80. {
  81. $this->addDebugInfos($node);
  82. $content = $this->trimInlineComments($node->getContent());
  83. $this->write(sprintf('<?php } %s { ?>' , $content));
  84. }
  85. public function leaveTopBlock(Run $node)
  86. {
  87. if ($node->isBlock()) {
  88. $this->write('<?php } ?>');
  89. }
  90. }
  91. public function enterObjectRefClass(ObjectRefClass $node)
  92. {
  93. if ($this->isEchoMode()) {
  94. $this->raw('<?php echo ');
  95. }
  96. $this->raw('MtHaml\Runtime::renderObjectRefClass(');
  97. $this->pushEchoMode(false);
  98. }
  99. public function leaveObjectRefClass(ObjectRefClass $node)
  100. {
  101. $this->raw(')');
  102. $this->popEchoMode(true);
  103. if ($this->isEchoMode()) {
  104. $this->raw('; ?>');
  105. }
  106. }
  107. public function enterObjectRefId(ObjectRefId $node)
  108. {
  109. if ($this->isEchoMode()) {
  110. $this->raw('<?php echo ');
  111. }
  112. $this->raw('MtHaml\Runtime::renderObjectRefId(');
  113. $this->pushEchoMode(false);
  114. }
  115. public function leaveObjectRefId(ObjectRefId $node)
  116. {
  117. $this->raw(')');
  118. $this->popEchoMode(true);
  119. if ($this->isEchoMode()) {
  120. $this->raw('; ?>');
  121. }
  122. }
  123. public function enterObjectRefPrefix(NodeAbstract $node)
  124. {
  125. $this->raw(', ');
  126. }
  127. protected function writeDebugInfos($lineno)
  128. {
  129. }
  130. protected function renderDynamicAttributes(Tag $tag)
  131. {
  132. $list = array();
  133. $n = 0;
  134. $this->raw(' <?php echo MtHaml\Runtime::renderAttributes(array(');
  135. $this->setEchoMode(false);
  136. foreach ($tag->getAttributes() as $attr) {
  137. if (0 !== $n) {
  138. $this->raw(', ');
  139. }
  140. if ($attr instanceof TagAttributeInterpolation) {
  141. $this->raw('MtHaml\Runtime\AttributeInterpolation::create(');
  142. $attr->getValue()->accept($this);
  143. $this->raw(')');
  144. } else if ($attr instanceof TagAttributeList) {
  145. $this->raw('MtHaml\Runtime\AttributeList::create(');
  146. $attr->getValue()->accept($this);
  147. $this->raw(')');
  148. } else {
  149. $this->raw('array(');
  150. $attr->getName()->accept($this);
  151. $this->raw(', ');
  152. if ($value = $attr->getValue()) {
  153. $attr->getValue()->accept($this);
  154. } else {
  155. $this->raw('TRUE');
  156. }
  157. $this->raw(')');
  158. }
  159. ++$n;
  160. }
  161. $this->raw(')');
  162. $this->setEchoMode(true);
  163. $this->raw(', ');
  164. $this->raw($this->stringLiteral($this->env->getOption('format')));
  165. $this->raw(', ');
  166. $this->raw($this->stringLiteral($this->charset));
  167. $this->raw('); ?>');
  168. }
  169. public function trimInlineComments($code)
  170. {
  171. // Removes inlines comments ('//' and '#'), while ignoring '//' and '#'
  172. // embedded in quoted strings.
  173. $re = "!
  174. (?P<code>
  175. (?P<expr>(?:
  176. # anything except \", ', `
  177. [^\"'`]
  178. # double quoted string
  179. | \"(?: [^\"\\\\]+ | \\\\. )*\"
  180. # single quoted string
  181. | '(?: [^'\\\\]+ | \\\\. )*'
  182. # backticks string
  183. | `(?: [^`\\\\]+ | \\\\. )*`
  184. )+?)
  185. )
  186. (?P<comment>\s*(?://|\#).*)?
  187. $!xA";
  188. return preg_replace($re, '$1', $code);
  189. }
  190. }