CodeExtension.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. use Symfony\Component\HttpKernel\Debug\FileLinkFormatter;
  12. use Twig\Extension\AbstractExtension;
  13. use Twig\TwigFilter;
  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 AbstractExtension
  20. {
  21. private $fileLinkFormat;
  22. private $rootDir;
  23. private $charset;
  24. /**
  25. * Constructor.
  26. *
  27. * @param string|FileLinkFormatter $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 = $fileLinkFormat ?: ini_get('xdebug.file_link_format') ?: get_cfg_var('xdebug.file_link_format');
  34. $this->rootDir = str_replace('/', DIRECTORY_SEPARATOR, dirname($rootDir)).DIRECTORY_SEPARATOR;
  35. $this->charset = $charset;
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function getFilters()
  41. {
  42. return array(
  43. new TwigFilter('abbr_class', array($this, 'abbrClass'), array('is_safe' => array('html'))),
  44. new TwigFilter('abbr_method', array($this, 'abbrMethod'), array('is_safe' => array('html'))),
  45. new TwigFilter('format_args', array($this, 'formatArgs'), array('is_safe' => array('html'))),
  46. new TwigFilter('format_args_as_text', array($this, 'formatArgsAsText')),
  47. new TwigFilter('file_excerpt', array($this, 'fileExcerpt'), array('is_safe' => array('html'))),
  48. new TwigFilter('format_file', array($this, 'formatFile'), array('is_safe' => array('html'))),
  49. new TwigFilter('format_file_from_text', array($this, 'formatFileFromText'), array('is_safe' => array('html'))),
  50. new TwigFilter('format_log_message', array($this, 'formatLogMessage'), array('is_safe' => array('html'))),
  51. new TwigFilter('file_link', array($this, 'getFileLink')),
  52. );
  53. }
  54. public function abbrClass($class)
  55. {
  56. $parts = explode('\\', $class);
  57. $short = array_pop($parts);
  58. return sprintf('<abbr title="%s">%s</abbr>', $class, $short);
  59. }
  60. public function abbrMethod($method)
  61. {
  62. if (false !== strpos($method, '::')) {
  63. list($class, $method) = explode('::', $method, 2);
  64. $result = sprintf('%s::%s()', $this->abbrClass($class), $method);
  65. } elseif ('Closure' === $method) {
  66. $result = sprintf('<abbr title="%s">%s</abbr>', $method, $method);
  67. } else {
  68. $result = sprintf('<abbr title="%s">%s</abbr>()', $method, $method);
  69. }
  70. return $result;
  71. }
  72. /**
  73. * Formats an array as a string.
  74. *
  75. * @param array $args The argument array
  76. *
  77. * @return string
  78. */
  79. public function formatArgs($args)
  80. {
  81. $result = array();
  82. foreach ($args as $key => $item) {
  83. if ('object' === $item[0]) {
  84. $parts = explode('\\', $item[1]);
  85. $short = array_pop($parts);
  86. $formattedValue = sprintf('<em>object</em>(<abbr title="%s">%s</abbr>)', $item[1], $short);
  87. } elseif ('array' === $item[0]) {
  88. $formattedValue = sprintf('<em>array</em>(%s)', is_array($item[1]) ? $this->formatArgs($item[1]) : $item[1]);
  89. } elseif ('null' === $item[0]) {
  90. $formattedValue = '<em>null</em>';
  91. } elseif ('boolean' === $item[0]) {
  92. $formattedValue = '<em>'.strtolower(var_export($item[1], true)).'</em>';
  93. } elseif ('resource' === $item[0]) {
  94. $formattedValue = '<em>resource</em>';
  95. } else {
  96. $formattedValue = str_replace("\n", '', htmlspecialchars(var_export($item[1], true), ENT_COMPAT | ENT_SUBSTITUTE, $this->charset));
  97. }
  98. $result[] = is_int($key) ? $formattedValue : sprintf("'%s' => %s", $key, $formattedValue);
  99. }
  100. return implode(', ', $result);
  101. }
  102. /**
  103. * Formats an array as a string.
  104. *
  105. * @param array $args The argument array
  106. *
  107. * @return string
  108. */
  109. public function formatArgsAsText($args)
  110. {
  111. return strip_tags($this->formatArgs($args));
  112. }
  113. /**
  114. * Returns an excerpt of a code file around the given line number.
  115. *
  116. * @param string $file A file path
  117. * @param int $line The selected line number
  118. * @param int $srcContext The number of displayed lines around or -1 for the whole file
  119. *
  120. * @return string An HTML string
  121. */
  122. public function fileExcerpt($file, $line, $srcContext = 3)
  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. // split multiline spans
  131. $code = preg_replace_callback('#<span ([^>]++)>((?:[^<]*+<br \/>)++[^<]*+)</span>#', function ($m) {
  132. return "<span $m[1]>".str_replace('<br />', "</span><br /><span $m[1]>", $m[2]).'</span>';
  133. }, $code);
  134. $content = explode('<br />', $code);
  135. $lines = array();
  136. if (0 > $srcContext) {
  137. $srcContext = count($content);
  138. }
  139. for ($i = max($line - $srcContext, 1), $max = min($line + $srcContext, count($content)); $i <= $max; ++$i) {
  140. $lines[] = '<li'.($i == $line ? ' class="selected"' : '').'><a class="anchor" name="line'.$i.'"></a><code>'.self::fixCodeMarkup($content[$i - 1]).'</code></li>';
  141. }
  142. return '<ol start="'.max($line - $srcContext, 1).'">'.implode("\n", $lines).'</ol>';
  143. }
  144. }
  145. /**
  146. * Formats a file path.
  147. *
  148. * @param string $file An absolute file path
  149. * @param int $line The line number
  150. * @param string $text Use this text for the link rather than the file path
  151. *
  152. * @return string
  153. */
  154. public function formatFile($file, $line, $text = null)
  155. {
  156. $file = trim($file);
  157. if (null === $text) {
  158. $text = str_replace('/', DIRECTORY_SEPARATOR, $file);
  159. if (0 === strpos($text, $this->rootDir)) {
  160. $text = substr($text, strlen($this->rootDir));
  161. $text = explode(DIRECTORY_SEPARATOR, $text, 2);
  162. $text = sprintf('<abbr title="%s%2$s">%s</abbr>%s', $this->rootDir, $text[0], isset($text[1]) ? DIRECTORY_SEPARATOR.$text[1] : '');
  163. }
  164. }
  165. $text = "$text at line $line";
  166. if (false !== $link = $this->getFileLink($file, $line)) {
  167. return sprintf('<a href="%s" title="Click to open this file" class="file_link">%s</a>', htmlspecialchars($link, ENT_COMPAT | ENT_SUBSTITUTE, $this->charset), $text);
  168. }
  169. return $text;
  170. }
  171. /**
  172. * Returns the link for a given file/line pair.
  173. *
  174. * @param string $file An absolute file path
  175. * @param int $line The line number
  176. *
  177. * @return string A link of false
  178. */
  179. public function getFileLink($file, $line)
  180. {
  181. if ($fmt = $this->fileLinkFormat) {
  182. return is_string($fmt) ? strtr($fmt, array('%f' => $file, '%l' => $line)) : $fmt->format($file, $line);
  183. }
  184. return false;
  185. }
  186. public function formatFileFromText($text)
  187. {
  188. return preg_replace_callback('/in ("|&quot;)?(.+?)\1(?: +(?:on|at))? +line (\d+)/s', function ($match) {
  189. return 'in '.$this->formatFile($match[2], $match[3]);
  190. }, $text);
  191. }
  192. /**
  193. * @internal
  194. */
  195. public function formatLogMessage($message, array $context)
  196. {
  197. if ($context && false !== strpos($message, '{')) {
  198. $replacements = array();
  199. foreach ($context as $key => $val) {
  200. if (is_scalar($val)) {
  201. $replacements['{'.$key.'}'] = $val;
  202. }
  203. }
  204. if ($replacements) {
  205. $message = strtr($message, $replacements);
  206. }
  207. }
  208. return htmlspecialchars($message, ENT_COMPAT | ENT_SUBSTITUTE, $this->charset);
  209. }
  210. /**
  211. * {@inheritdoc}
  212. */
  213. public function getName()
  214. {
  215. return 'code';
  216. }
  217. protected static function fixCodeMarkup($line)
  218. {
  219. // </span> ending tag from previous line
  220. $opening = strpos($line, '<span');
  221. $closing = strpos($line, '</span>');
  222. if (false !== $closing && (false === $opening || $closing < $opening)) {
  223. $line = substr_replace($line, '', $closing, 7);
  224. }
  225. // missing </span> tag at the end of line
  226. $opening = strpos($line, '<span');
  227. $closing = strpos($line, '</span>');
  228. if (false !== $opening && (false === $closing || $closing > $opening)) {
  229. $line .= '</span>';
  230. }
  231. return $line;
  232. }
  233. }