PageRenderTime 64ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Whoops/Util/TemplateHelper.php

https://gitlab.com/lighty/whoops
PHP | 196 lines | 93 code | 21 blank | 82 comment | 6 complexity | 5c9e63333b9f0bbcff9bcf792d31dc1a MD5 | raw file
  1. <?php
  2. /**
  3. * Whoops - php errors for cool kids
  4. * @author Filipe Dobreira <http://github.com/filp>
  5. */
  6. namespace Whoops\Util;
  7. use Symfony\Component\VarDumper\Cloner\VarCloner;
  8. use Symfony\Component\VarDumper\Dumper\CliDumper;
  9. use Symfony\Component\VarDumper\Dumper\HtmlDumper;
  10. /**
  11. * Exposes useful tools for working with/in templates
  12. */
  13. class TemplateHelper
  14. {
  15. /**
  16. * An array of variables to be passed to all templates
  17. * @var array
  18. */
  19. private $variables = array();
  20. /**
  21. * Escapes a string for output in an HTML document
  22. *
  23. * @param string $raw
  24. * @return string
  25. */
  26. public function escape($raw)
  27. {
  28. $flags = ENT_QUOTES;
  29. // HHVM has all constants defined, but only ENT_IGNORE
  30. // works at the moment
  31. if (defined("ENT_SUBSTITUTE") && !defined("HHVM_VERSION")) {
  32. $flags |= ENT_SUBSTITUTE;
  33. } else {
  34. // This is for 5.3.
  35. // The documentation warns of a potential security issue,
  36. // but it seems it does not apply in our case, because
  37. // we do not blacklist anything anywhere.
  38. $flags |= ENT_IGNORE;
  39. }
  40. return htmlspecialchars($raw, $flags, "UTF-8");
  41. }
  42. /**
  43. * Escapes a string for output in an HTML document, but preserves
  44. * URIs within it, and converts them to clickable anchor elements.
  45. *
  46. * @param string $raw
  47. * @return string
  48. */
  49. public function escapeButPreserveUris($raw)
  50. {
  51. $escaped = $this->escape($raw);
  52. return preg_replace(
  53. "@([A-z]+?://([-\w\.]+[-\w])+(:\d+)?(/([\w/_\.#-]*(\?\S+)?[^\.\s])?)?)@",
  54. "<a href=\"$1\" target=\"_blank\">$1</a>", $escaped
  55. );
  56. }
  57. /**
  58. * Format the given value into a human readable string.
  59. *
  60. * @param mixed $value
  61. * @return string
  62. */
  63. public function dump($value)
  64. {
  65. if (class_exists('Symfony\Component\VarDumper\Cloner\VarCloner')) {
  66. static $dumper = null;
  67. // re-use the same var-dumper instance, so it won't re-render the global styles/scripts on each dump.
  68. if (!$dumper) {
  69. $dumper = new HtmlDumper();
  70. $styles = array(
  71. 'default' => '',
  72. 'num' => '',
  73. 'const' => '',
  74. 'str' => '',
  75. 'note' => '',
  76. 'ref' => '',
  77. 'public' => '',
  78. 'protected' => '',
  79. 'private' => '',
  80. 'meta' => '',
  81. 'key' => '',
  82. 'index' => '',
  83. );
  84. $dumper->setStyles($styles);
  85. }
  86. $cloner = new VarCloner();
  87. return $dumper->dump($cloner->cloneVar($value));
  88. }
  89. return print_r($value, true);
  90. }
  91. /**
  92. * Convert a string to a slug version of itself
  93. *
  94. * @param string $original
  95. * @return string
  96. */
  97. public function slug($original)
  98. {
  99. $slug = str_replace(" ", "-", $original);
  100. $slug = preg_replace('/[^\w\d\-\_]/i', '', $slug);
  101. return strtolower($slug);
  102. }
  103. /**
  104. * Given a template path, render it within its own scope. This
  105. * method also accepts an array of additional variables to be
  106. * passed to the template.
  107. *
  108. * @param string $template
  109. * @param array $additionalVariables
  110. */
  111. public function render($template, array $additionalVariables = null)
  112. {
  113. $variables = $this->getVariables();
  114. // Pass the helper to the template:
  115. $variables["tpl"] = $this;
  116. if ($additionalVariables !== null) {
  117. $variables = array_replace($variables, $additionalVariables);
  118. }
  119. call_user_func(function () {
  120. extract(func_get_arg(1));
  121. require func_get_arg(0);
  122. }, $template, $variables);
  123. }
  124. /**
  125. * Sets the variables to be passed to all templates rendered
  126. * by this template helper.
  127. *
  128. * @param array $variables
  129. */
  130. public function setVariables(array $variables)
  131. {
  132. $this->variables = $variables;
  133. }
  134. /**
  135. * Sets a single template variable, by its name:
  136. *
  137. * @param string $variableName
  138. * @param mixd $variableValue
  139. */
  140. public function setVariable($variableName, $variableValue)
  141. {
  142. $this->variables[$variableName] = $variableValue;
  143. }
  144. /**
  145. * Gets a single template variable, by its name, or
  146. * $defaultValue if the variable does not exist
  147. *
  148. * @param string $variableName
  149. * @param mixed $defaultValue
  150. * @return mixed
  151. */
  152. public function getVariable($variableName, $defaultValue = null)
  153. {
  154. return isset($this->variables[$variableName]) ?
  155. $this->variables[$variableName] : $defaultValue;
  156. }
  157. /**
  158. * Unsets a single template variable, by its name
  159. *
  160. * @param string $variableName
  161. */
  162. public function delVariable($variableName)
  163. {
  164. unset($this->variables[$variableName]);
  165. }
  166. /**
  167. * Returns all variables for this helper
  168. *
  169. * @return array
  170. */
  171. public function getVariables()
  172. {
  173. return $this->variables;
  174. }
  175. }