PageRenderTime 43ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/RestAPI/vendor/symfony/twig-bridge/Extension/CodeExtension.php

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