CodeExtension.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Bridge\Twig\Extension;
  11. if (!defined('ENT_SUBSTITUTE')) {
  12. define('ENT_SUBSTITUTE', 8);
  13. }
  14. /**
  15. * Twig extension relate to PHP code and used by the profiler and the default exception templates.
  16. *
  17. * @author Fabien Potencier <fabien@symfony.com>
  18. */
  19. class CodeExtension extends \Twig_Extension
  20. {
  21. private $fileLinkFormat;
  22. private $rootDir;
  23. private $charset;
  24. /**
  25. * Constructor.
  26. *
  27. * @param string $fileLinkFormat The format for links to source files
  28. * @param string $rootDir The project root directory
  29. * @param string $charset The charset
  30. */
  31. public function __construct($fileLinkFormat, $rootDir, $charset)
  32. {
  33. $this->fileLinkFormat = empty($fileLinkFormat) ? ini_get('xdebug.file_link_format') : $fileLinkFormat;
  34. $this->rootDir = str_replace('\\', '/', $rootDir).'/';
  35. $this->charset = $charset;
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function getFilters()
  41. {
  42. return array(
  43. new \Twig_SimpleFilter('abbr_class', array($this, 'abbrClass'), array('is_safe' => array('html'))),
  44. new \Twig_SimpleFilter('abbr_method', array($this, 'abbrMethod'), array('is_safe' => array('html'))),
  45. new \Twig_SimpleFilter('format_args', array($this, 'formatArgs'), array('is_safe' => array('html'))),
  46. new \Twig_SimpleFilter('format_args_as_text', array($this, 'formatArgsAsText')),
  47. new \Twig_SimpleFilter('file_excerpt', array($this, 'fileExcerpt'), array('is_safe' => array('html'))),
  48. new \Twig_SimpleFilter('format_file', array($this, 'formatFile'), array('is_safe' => array('html'))),
  49. new \Twig_SimpleFilter('format_file_from_text', array($this, 'formatFileFromText'), array('is_safe' => array('html'))),
  50. new \Twig_SimpleFilter('file_link', array($this, 'getFileLink'), array('is_safe' => array('html'))),
  51. );
  52. }
  53. public function abbrClass($class)
  54. {
  55. $parts = explode('\\', $class);
  56. $short = array_pop($parts);
  57. return sprintf("<abbr title=\"%s\">%s</abbr>", $class, $short);
  58. }
  59. public function abbrMethod($method)
  60. {
  61. if (false !== strpos($method, '::')) {
  62. list($class, $method) = explode('::', $method, 2);
  63. $result = sprintf("%s::%s()", $this->abbrClass($class), $method);
  64. } elseif ('Closure' === $method) {
  65. $result = sprintf("<abbr title=\"%s\">%s</abbr>", $method, $method);
  66. } else {
  67. $result = sprintf("<abbr title=\"%s\">%s</abbr>()", $method, $method);
  68. }
  69. return $result;
  70. }
  71. /**
  72. * Formats an array as a string.
  73. *
  74. * @param array $args The argument array
  75. *
  76. * @return string
  77. */
  78. public function formatArgs($args)
  79. {
  80. $result = array();
  81. foreach ($args as $key => $item) {
  82. if ('object' === $item[0]) {
  83. $parts = explode('\\', $item[1]);
  84. $short = array_pop($parts);
  85. $formattedValue = sprintf("<em>object</em>(<abbr title=\"%s\">%s</abbr>)", $item[1], $short);
  86. } elseif ('array' === $item[0]) {
  87. $formattedValue = sprintf("<em>array</em>(%s)", is_array($item[1]) ? $this->formatArgs($item[1]) : $item[1]);
  88. } elseif ('string' === $item[0]) {
  89. $formattedValue = sprintf("'%s'", htmlspecialchars($item[1], ENT_QUOTES, $this->charset));
  90. } elseif ('null' === $item[0]) {
  91. $formattedValue = '<em>null</em>';
  92. } elseif ('boolean' === $item[0]) {
  93. $formattedValue = '<em>'.strtolower(var_export($item[1], true)).'</em>';
  94. } elseif ('resource' === $item[0]) {
  95. $formattedValue = '<em>resource</em>';
  96. } else {
  97. $formattedValue = str_replace("\n", '', var_export(htmlspecialchars((string) $item[1], ENT_QUOTES, $this->charset), true));
  98. }
  99. $result[] = is_int($key) ? $formattedValue : sprintf("'%s' => %s", $key, $formattedValue);
  100. }
  101. return implode(', ', $result);
  102. }
  103. /**
  104. * Formats an array as a string.
  105. *
  106. * @param array $args The argument array
  107. *
  108. * @return string
  109. */
  110. public function formatArgsAsText($args)
  111. {
  112. return strip_tags($this->formatArgs($args));
  113. }
  114. /**
  115. * Returns an excerpt of a code file around the given line number.
  116. *
  117. * @param string $file A file path
  118. * @param int $line The selected line number
  119. *
  120. * @return string An HTML string
  121. */
  122. public function fileExcerpt($file, $line)
  123. {
  124. if (is_readable($file)) {
  125. // highlight_file could throw warnings
  126. // see https://bugs.php.net/bug.php?id=25725
  127. $code = @highlight_file($file, true);
  128. // remove main code/span tags
  129. $code = preg_replace('#^<code.*?>\s*<span.*?>(.*)</span>\s*</code>#s', '\\1', $code);
  130. $content = preg_split('#<br />#', $code);
  131. $lines = array();
  132. for ($i = max($line - 3, 1), $max = min($line + 3, count($content)); $i <= $max; $i++) {
  133. $lines[] = '<li'.($i == $line ? ' class="selected"' : '').'><code>'.self::fixCodeMarkup($content[$i - 1]).'</code></li>';
  134. }
  135. return '<ol start="'.max($line - 3, 1).'">'.implode("\n", $lines).'</ol>';
  136. }
  137. }
  138. /**
  139. * Formats a file path.
  140. *
  141. * @param string $file An absolute file path
  142. * @param integer $line The line number
  143. * @param string $text Use this text for the link rather than the file path
  144. *
  145. * @return string
  146. */
  147. public function formatFile($file, $line, $text = null)
  148. {
  149. if (null === $text) {
  150. $file = trim($file);
  151. $text = $file;
  152. if (0 === strpos($text, $this->rootDir)) {
  153. $text = str_replace($this->rootDir, '', str_replace('\\', '/', $text));
  154. $text = sprintf('<abbr title="%s">kernel.root_dir</abbr>/%s', $this->rootDir, $text);
  155. }
  156. }
  157. $text = "$text at line $line";
  158. if (false !== $link = $this->getFileLink($file, $line)) {
  159. return sprintf('<a href="%s" title="Click to open this file" class="file_link">%s</a>', htmlspecialchars($link, ENT_QUOTES | ENT_SUBSTITUTE, $this->charset), $text);
  160. }
  161. return $text;
  162. }
  163. /**
  164. * Returns the link for a given file/line pair.
  165. *
  166. * @param string $file An absolute file path
  167. * @param integer $line The line number
  168. *
  169. * @return string A link of false
  170. */
  171. public function getFileLink($file, $line)
  172. {
  173. if ($this->fileLinkFormat && is_file($file)) {
  174. return strtr($this->fileLinkFormat, array('%f' => $file, '%l' => $line));
  175. }
  176. return false;
  177. }
  178. public function formatFileFromText($text)
  179. {
  180. $that = $this;
  181. return preg_replace_callback('/in ("|&quot;)?(.+?)\1(?: +(?:on|at))? +line (\d+)/s', function ($match) use ($that) {
  182. return 'in '.$that->formatFile($match[2], $match[3]);
  183. }, $text);
  184. }
  185. public function getName()
  186. {
  187. return 'code';
  188. }
  189. protected static function fixCodeMarkup($line)
  190. {
  191. // </span> ending tag from previous line
  192. $opening = strpos($line, '<span');
  193. $closing = strpos($line, '</span>');
  194. if (false !== $closing && (false === $opening || $closing < $opening)) {
  195. $line = substr_replace($line, '', $closing, 7);
  196. }
  197. // missing </span> tag at the end of line
  198. $opening = strpos($line, '<span');
  199. $closing = strpos($line, '</span>');
  200. if (false !== $opening && (false === $closing || $closing > $opening)) {
  201. $line .= '</span>';
  202. }
  203. return $line;
  204. }
  205. }