CodeExtension.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. final class CodeExtension extends AbstractExtension
  20. {
  21. private $fileLinkFormat;
  22. private $charset;
  23. private $projectDir;
  24. /**
  25. * @param string|FileLinkFormatter $fileLinkFormat The format for links to source files
  26. */
  27. public function __construct($fileLinkFormat, string $projectDir, string $charset)
  28. {
  29. $this->fileLinkFormat = $fileLinkFormat ?: ini_get('xdebug.file_link_format') ?: get_cfg_var('xdebug.file_link_format');
  30. $this->projectDir = str_replace('\\', '/', $projectDir).'/';
  31. $this->charset = $charset;
  32. }
  33. /**
  34. * {@inheritdoc}
  35. */
  36. public function getFilters(): array
  37. {
  38. return [
  39. new TwigFilter('abbr_class', [$this, 'abbrClass'], ['is_safe' => ['html']]),
  40. new TwigFilter('abbr_method', [$this, 'abbrMethod'], ['is_safe' => ['html']]),
  41. new TwigFilter('format_args', [$this, 'formatArgs'], ['is_safe' => ['html']]),
  42. new TwigFilter('format_args_as_text', [$this, 'formatArgsAsText']),
  43. new TwigFilter('file_excerpt', [$this, 'fileExcerpt'], ['is_safe' => ['html']]),
  44. new TwigFilter('format_file', [$this, 'formatFile'], ['is_safe' => ['html']]),
  45. new TwigFilter('format_file_from_text', [$this, 'formatFileFromText'], ['is_safe' => ['html']]),
  46. new TwigFilter('format_log_message', [$this, 'formatLogMessage'], ['is_safe' => ['html']]),
  47. new TwigFilter('file_link', [$this, 'getFileLink']),
  48. new TwigFilter('file_relative', [$this, 'getFileRelative']),
  49. ];
  50. }
  51. public function abbrClass(string $class): string
  52. {
  53. $parts = explode('\\', $class);
  54. $short = array_pop($parts);
  55. return sprintf('<abbr title="%s">%s</abbr>', $class, $short);
  56. }
  57. public function abbrMethod(string $method): string
  58. {
  59. if (false !== strpos($method, '::')) {
  60. [$class, $method] = explode('::', $method, 2);
  61. $result = sprintf('%s::%s()', $this->abbrClass($class), $method);
  62. } elseif ('Closure' === $method) {
  63. $result = sprintf('<abbr title="%s">%1$s</abbr>', $method);
  64. } else {
  65. $result = sprintf('<abbr title="%s">%1$s</abbr>()', $method);
  66. }
  67. return $result;
  68. }
  69. /**
  70. * Formats an array as a string.
  71. */
  72. public function formatArgs(array $args): string
  73. {
  74. $result = [];
  75. foreach ($args as $key => $item) {
  76. if ('object' === $item[0]) {
  77. $parts = explode('\\', $item[1]);
  78. $short = array_pop($parts);
  79. $formattedValue = sprintf('<em>object</em>(<abbr title="%s">%s</abbr>)', $item[1], $short);
  80. } elseif ('array' === $item[0]) {
  81. $formattedValue = sprintf('<em>array</em>(%s)', \is_array($item[1]) ? $this->formatArgs($item[1]) : $item[1]);
  82. } elseif ('null' === $item[0]) {
  83. $formattedValue = '<em>null</em>';
  84. } elseif ('boolean' === $item[0]) {
  85. $formattedValue = '<em>'.strtolower(var_export($item[1], true)).'</em>';
  86. } elseif ('resource' === $item[0]) {
  87. $formattedValue = '<em>resource</em>';
  88. } else {
  89. $formattedValue = str_replace("\n", '', htmlspecialchars(var_export($item[1], true), \ENT_COMPAT | \ENT_SUBSTITUTE, $this->charset));
  90. }
  91. $result[] = \is_int($key) ? $formattedValue : sprintf("'%s' => %s", $key, $formattedValue);
  92. }
  93. return implode(', ', $result);
  94. }
  95. /**
  96. * Formats an array as a string.
  97. */
  98. public function formatArgsAsText(array $args): string
  99. {
  100. return strip_tags($this->formatArgs($args));
  101. }
  102. /**
  103. * Returns an excerpt of a code file around the given line number.
  104. */
  105. public function fileExcerpt(string $file, int $line, int $srcContext = 3): ?string
  106. {
  107. if (is_file($file) && is_readable($file)) {
  108. // highlight_file could throw warnings
  109. // see https://bugs.php.net/25725
  110. $code = @highlight_file($file, true);
  111. // remove main code/span tags
  112. $code = preg_replace('#^<code.*?>\s*<span.*?>(.*)</span>\s*</code>#s', '\\1', $code);
  113. // split multiline spans
  114. $code = preg_replace_callback('#<span ([^>]++)>((?:[^<]*+<br \/>)++[^<]*+)</span>#', function ($m) {
  115. return "<span $m[1]>".str_replace('<br />', "</span><br /><span $m[1]>", $m[2]).'</span>';
  116. }, $code);
  117. $content = explode('<br />', $code);
  118. $lines = [];
  119. if (0 > $srcContext) {
  120. $srcContext = \count($content);
  121. }
  122. for ($i = max($line - $srcContext, 1), $max = min($line + $srcContext, \count($content)); $i <= $max; ++$i) {
  123. $lines[] = '<li'.($i == $line ? ' class="selected"' : '').'><a class="anchor" name="line'.$i.'"></a><code>'.self::fixCodeMarkup($content[$i - 1]).'</code></li>';
  124. }
  125. return '<ol start="'.max($line - $srcContext, 1).'">'.implode("\n", $lines).'</ol>';
  126. }
  127. return null;
  128. }
  129. /**
  130. * Formats a file path.
  131. */
  132. public function formatFile(string $file, int $line, string $text = null): string
  133. {
  134. $file = trim($file);
  135. if (null === $text) {
  136. $text = $file;
  137. if (null !== $rel = $this->getFileRelative($text)) {
  138. $rel = explode('/', $rel, 2);
  139. $text = sprintf('<abbr title="%s%2$s">%s</abbr>%s', $this->projectDir, $rel[0], '/'.($rel[1] ?? ''));
  140. }
  141. }
  142. if (0 < $line) {
  143. $text .= ' at line '.$line;
  144. }
  145. if (false !== $link = $this->getFileLink($file, $line)) {
  146. 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);
  147. }
  148. return $text;
  149. }
  150. /**
  151. * Returns the link for a given file/line pair.
  152. *
  153. * @return string|false A link or false
  154. */
  155. public function getFileLink(string $file, int $line)
  156. {
  157. if ($fmt = $this->fileLinkFormat) {
  158. return \is_string($fmt) ? strtr($fmt, ['%f' => $file, '%l' => $line]) : $fmt->format($file, $line);
  159. }
  160. return false;
  161. }
  162. public function getFileRelative(string $file): ?string
  163. {
  164. $file = str_replace('\\', '/', $file);
  165. if (null !== $this->projectDir && 0 === strpos($file, $this->projectDir)) {
  166. return ltrim(substr($file, \strlen($this->projectDir)), '/');
  167. }
  168. return null;
  169. }
  170. public function formatFileFromText(string $text): string
  171. {
  172. return preg_replace_callback('/in ("|&quot;)?(.+?)\1(?: +(?:on|at))? +line (\d+)/s', function ($match) {
  173. return 'in '.$this->formatFile($match[2], $match[3]);
  174. }, $text);
  175. }
  176. /**
  177. * @internal
  178. */
  179. public function formatLogMessage(string $message, array $context): string
  180. {
  181. if ($context && false !== strpos($message, '{')) {
  182. $replacements = [];
  183. foreach ($context as $key => $val) {
  184. if (is_scalar($val)) {
  185. $replacements['{'.$key.'}'] = $val;
  186. }
  187. }
  188. if ($replacements) {
  189. $message = strtr($message, $replacements);
  190. }
  191. }
  192. return htmlspecialchars($message, \ENT_COMPAT | \ENT_SUBSTITUTE, $this->charset);
  193. }
  194. protected static function fixCodeMarkup(string $line): string
  195. {
  196. // </span> ending tag from previous line
  197. $opening = strpos($line, '<span');
  198. $closing = strpos($line, '</span>');
  199. if (false !== $closing && (false === $opening || $closing < $opening)) {
  200. $line = substr_replace($line, '', $closing, 7);
  201. }
  202. // missing </span> tag at the end of line
  203. $opening = strpos($line, '<span');
  204. $closing = strpos($line, '</span>');
  205. if (false !== $opening && (false === $closing || $closing > $opening)) {
  206. $line .= '</span>';
  207. }
  208. return trim($line);
  209. }
  210. }