/plugins/sfThemeGeneratorPlugin/lib/util/sfThemeTokenParser.class.php

https://bitbucket.org/Boniboy/portal · PHP · 229 lines · 192 code · 31 blank · 6 comment · 16 complexity · 18f8e4f4b76437486e7efe1eb798372e MD5 · raw file

  1. <?php
  2. /**
  3. *
  4. */
  5. class sfThemeTokenParser
  6. {
  7. protected
  8. $tokenMatches,
  9. $varName;
  10. function __construct($tokenMatches, $varName)
  11. {
  12. $this->setTokenMatches($tokenMatches);
  13. $this->varName = $varName;
  14. }
  15. public function renderHtmlText($text)
  16. {
  17. return $this->replaceTokens($text, 'html');
  18. }
  19. // Render text in a PHP block
  20. public function renderPhpText($text)
  21. {
  22. return $this->replaceTokens($text, 'php');
  23. }
  24. public function renderArray($array)
  25. {
  26. $arrayLines = array();
  27. foreach ($array as $key => $value) {
  28. $line = ' ';
  29. $isAssociative = $this->arrayKeyIsAssociative($key, $array);
  30. if (!$isAssociative) {
  31. if (is_int($key)) {
  32. $line .= $key;
  33. }
  34. elseif (is_bool($key)) {
  35. $line .= $this->asPhp($key);
  36. }
  37. else {
  38. $line .= $this->renderPhpText($key);
  39. }
  40. $line .= ' => ';
  41. }
  42. if (is_int($value)) {
  43. $line .= $value;
  44. }
  45. elseif (is_bool($value)) {
  46. $line .= $this->asPhp($value);
  47. }
  48. elseif(is_array($value)) {
  49. $line .= $this->renderArray($value);
  50. }
  51. else {
  52. $line .= $this->renderPhpText($value);
  53. }
  54. $arrayLines[] = $line;
  55. }
  56. return $this->replacePhpTokens(sprintf("array(%s)", implode(',', $arrayLines)));
  57. }
  58. public function replaceTokens($string, $format = 'html')
  59. {
  60. $tr = array();
  61. $renderTextAsBlock = false;
  62. preg_match_all('/%%([^%]+)%%/', $string, $matches, PREG_PATTERN_ORDER);
  63. if (count($matches[1])) {
  64. $renderTextAsBlock = false;
  65. foreach ($matches[1] as $i => $name)
  66. {
  67. if (isset($this->tokenMatches[$name])) {
  68. $tr[$matches[0][$i]] = $this->tokenMatches[$name];
  69. }
  70. elseif($varName = $this->getVarName()) {
  71. $renderTextAsBlock = true;
  72. $getter = $name == 'to_string' ? '$'.$varName : $this->getColumnGetter($name, $varName);
  73. $tr[$matches[0][$i]] = sprintf("'.%s.'", $getter);
  74. }
  75. }
  76. }
  77. switch ($format) {
  78. case 'html':
  79. if ($renderTextAsBlock) {
  80. $string = $this->renderTextInPhpBlock($this->escapeString($string));
  81. }
  82. break;
  83. case 'php':
  84. $string = $this->asPhp($string);
  85. break;
  86. }
  87. if ($tr) {
  88. $string = strtr($string, $tr);
  89. }
  90. return $this->clearEmptyStrings($string);
  91. }
  92. public function replacePhpTokens($string)
  93. {
  94. $tr = array();
  95. preg_match_all('/\'\|\|(.*?)\|\|\'/', $string, $matches, PREG_PATTERN_ORDER);
  96. if (count($matches[1])) {
  97. foreach ($matches[1] as $i => $name)
  98. {
  99. $tr[$matches[0][$i]] = $this->unescapeString($name);
  100. }
  101. }
  102. if ($tr) {
  103. $string = strtr($string, $tr);
  104. }
  105. return $string;
  106. }
  107. public function wrapPhpToken($string)
  108. {
  109. return sprintf('||%s||', $string);
  110. }
  111. public function clearEmptyStrings($text)
  112. {
  113. // start of string
  114. if (strpos($text, "''.") === 0) {
  115. $text = substr($text, 3);
  116. }
  117. // end of string
  118. if (strpos(strrev($text), "''.") === 0) {
  119. $text = strrev(substr(strrev($text), 3));
  120. }
  121. return strtr($text, array(
  122. ".''." => '.', // middle of string
  123. "(''." => '(', // start of function
  124. ", ''." => ', ', // start array value
  125. "<?php echo ''." => '<?php echo ', // start of php block
  126. ".'')" => ')', // end of function
  127. ".''," => ',', // end of array value
  128. ".'' ?>" => ' ?>', // end of php block
  129. ));
  130. }
  131. public function renderTextInPhpBlock($text)
  132. {
  133. if (strpos($text, '<?php') !== 0) {
  134. $text = sprintf('<?php echo \'%s\' ?>', $text);
  135. }
  136. return $text;
  137. }
  138. public function escapeString($string)
  139. {
  140. return str_replace("'", "\\'", $string);
  141. }
  142. public function unescapeString($string)
  143. {
  144. return str_replace("\\'", "'", $string);
  145. }
  146. public function asPhp($variable)
  147. {
  148. return str_replace(array("\n", 'array ('), array('', 'array('), var_export($variable, true));
  149. }
  150. public function getColumnGetter($column, $varName = null)
  151. {
  152. $getter = 'get'.sfInflector::camelize($column);
  153. if ($varName)
  154. {
  155. $getter = sprintf('$%s->%s()', $this->getVarName(), $getter);
  156. }
  157. return $getter;
  158. }
  159. public function getVarName()
  160. {
  161. return $this->varName;
  162. }
  163. public function setTokenMatches($tokenMatches)
  164. {
  165. $this->tokenMatches = $tokenMatches;
  166. }
  167. public function getTokenMatches($tokenMatches)
  168. {
  169. return $this->tokenMatches;
  170. }
  171. protected function arrayKeyIsAssociative($key, $array)
  172. {
  173. if (!is_int($key) || !isset($array[$key]) || count($array) < $key) {
  174. return false;
  175. }
  176. $i = 0;
  177. foreach ($array as $value) {
  178. if ($i == $key) {
  179. return true;
  180. }
  181. if ($i > $key) {
  182. return false;
  183. }
  184. $i++;
  185. }
  186. return false;
  187. }
  188. }