PageRenderTime 36ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Templating/Helper/CodeHelper.php

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