PageRenderTime 41ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/casoetan/ServerGroveLiveChat
PHP | 214 lines | 162 code | 14 blank | 38 comment | 5 complexity | f841767a1cd8cfd3c3dc4c24a88b7fac MD5 | raw file
Possible License(s): LGPL-2.1, LGPL-3.0, ISC, BSD-3-Clause
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.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.potencier@symfony-project.com>
  16. */
  17. class CodeHelper extends Helper
  18. {
  19. protected $fileLinkFormat;
  20. protected $rootDir;
  21. /**
  22. * Constructor.
  23. *
  24. * @param string $fileLinkFormat The format for links to source files
  25. * @param string $rootDir The project root directory
  26. */
  27. public function __construct($fileLinkFormat, $rootDir)
  28. {
  29. $this->fileLinkFormat = null !== $fileLinkFormat ? $fileLinkFormat : ini_get('xdebug.file_link_format');
  30. $this->rootDir = str_replace('\\', '/', $rootDir).'/';
  31. }
  32. /**
  33. * Formats an array as a string.
  34. *
  35. * @param array $args The argument array
  36. *
  37. * @return string
  38. */
  39. public function formatArgsAsText($args)
  40. {
  41. $result = array();
  42. foreach ($args as $key => $item) {
  43. if ('object' === $item[0]) {
  44. $formattedValue = sprintf("object(%s)", $item[1]);
  45. } elseif ('array' === $item[0]) {
  46. $formattedValue = sprintf("array(%s)", $this->formatArgsAsText($item[1]));
  47. } elseif ('string' === $item[0]) {
  48. $formattedValue = sprintf("'%s'", $item[1]);
  49. } elseif ('null' === $item[0]) {
  50. $formattedValue = 'null';
  51. } elseif ('boolean' === $item[0]) {
  52. $formattedValue = strtolower(var_export($item[1], true));
  53. } elseif ('resource' === $item[0]) {
  54. $formattedValue = 'resource';
  55. } else {
  56. $formattedValue = str_replace("\n", '', var_export((string) $item[1], true));
  57. }
  58. $result[] = is_int($key) ? $formattedValue : sprintf("'%s' => %s", $key, $formattedValue);
  59. }
  60. return implode(', ', $result);
  61. }
  62. public function abbrClass($class)
  63. {
  64. $parts = explode('\\', $class);
  65. $short = array_pop($parts);
  66. return sprintf("<abbr title=\"%s\">%s</abbr>", $class, $short);
  67. }
  68. public function abbrMethod($method)
  69. {
  70. list($class, $method) = explode('::', $method);
  71. $parts = explode('\\', $class);
  72. $short = array_pop($parts);
  73. return sprintf("<abbr title=\"%s\">%s</abbr>::%s", $class, $short, $method);
  74. }
  75. /**
  76. * Formats an array as a string.
  77. *
  78. * @param array $args The argument array
  79. *
  80. * @return string
  81. */
  82. public function formatArgs($args)
  83. {
  84. $result = array();
  85. foreach ($args as $key => $item) {
  86. if ('object' === $item[0]) {
  87. $parts = explode('\\', $item[1]);
  88. $short = array_pop($parts);
  89. $formattedValue = sprintf("<em>object</em>(<abbr title=\"%s\">%s</abbr>)", $item[1], $short);
  90. } elseif ('array' === $item[0]) {
  91. $formattedValue = sprintf("<em>array</em>(%s)", $this->formatArgs($item[1]));
  92. } elseif ('string' === $item[0]) {
  93. $formattedValue = sprintf("'%s'", htmlspecialchars($item[1], ENT_QUOTES, $this->getCharset()));
  94. } elseif ('null' === $item[0]) {
  95. $formattedValue = '<em>null</em>';
  96. } elseif ('boolean' === $item[0]) {
  97. $formattedValue = '<em>'.strtolower(var_export($item[1], true)).'</em>';
  98. } elseif ('resource' === $item[0]) {
  99. $formattedValue = '<em>resource</em>';
  100. } else {
  101. $formattedValue = str_replace("\n", '', var_export(htmlspecialchars((string) $item[1], ENT_QUOTES, $this->getCharset()), true));
  102. }
  103. $result[] = is_int($key) ? $formattedValue : sprintf("'%s' => %s", $key, $formattedValue);
  104. }
  105. return implode(', ', $result);
  106. }
  107. /**
  108. * Returns an excerpt of a code file around the given line number.
  109. *
  110. * @param string $file A file path
  111. * @param int $line The selected line number
  112. *
  113. * @return string An HTML string
  114. */
  115. public function fileExcerpt($file, $line)
  116. {
  117. if (is_readable($file)) {
  118. $code = highlight_file($file, true);
  119. // remove main code/span tags
  120. $code = preg_replace('#^<code.*?>\s*<span.*?>(.*)</span>\s*</code>#s', '\\1', $code);
  121. $content = preg_split('#<br />#', $code);
  122. $lines = array();
  123. for ($i = max($line - 3, 1), $max = min($line + 3, count($content)); $i <= $max; $i++) {
  124. $lines[] = '<li'.($i == $line ? ' class="selected"' : '').'><code>'.self::fixCodeMarkup($content[$i - 1]).'</code></li>';
  125. }
  126. return '<ol start="'.max($line - 3, 1).'">'.implode("\n", $lines).'</ol>';
  127. }
  128. }
  129. /**
  130. * Formats a file path.
  131. *
  132. * @param string $file An absolute file path
  133. * @param integer $line The line number
  134. * @param string $format The output format (txt or html)
  135. * @param string $text Use this text for the link rather than the file path
  136. *
  137. * @return string
  138. */
  139. public function formatFile($file, $line)
  140. {
  141. $file = trim($file);
  142. $fileStr = $file;
  143. if (0 === strpos($fileStr, $this->rootDir)) {
  144. $fileStr = str_replace($this->rootDir, '', str_replace('\\', '/', $fileStr));
  145. $fileStr = sprintf('<abbr title="%s">kernel.root_dir</abbr>/%s', $this->rootDir, $fileStr);
  146. }
  147. if (!$this->fileLinkFormat) {
  148. return "$file line $line";
  149. }
  150. $link = strtr($this->fileLinkFormat, array('%f' => $file, '%l' => $line));
  151. return sprintf('<a href="%s" title="Click to open this file" class="file_link">%s line %s</a>', $link, $fileStr, $line);
  152. }
  153. public function formatFileFromText($text)
  154. {
  155. $that = $this;
  156. return preg_replace_callback('/in (.*?)(?: on|at)? line (\d+)/', function ($match) use ($that) {
  157. return 'in '.$that->formatFile($match[1], $match[2]);
  158. }, $text);
  159. }
  160. /**
  161. * Returns the canonical name of this helper.
  162. *
  163. * @return string The canonical name
  164. */
  165. public function getName()
  166. {
  167. return 'code';
  168. }
  169. protected static function fixCodeMarkup($line)
  170. {
  171. // </span> ending tag from previous line
  172. $opening = strpos($line, '<span');
  173. $closing = strpos($line, '</span>');
  174. if (false !== $closing && (false === $opening || $closing < $opening)) {
  175. $line = substr_replace($line, '', $closing, 7);
  176. }
  177. // missing </span> tag at the end of line
  178. $opening = strpos($line, '<span');
  179. $closing = strpos($line, '</span>');
  180. if (false !== $opening && (false === $closing || $closing > $opening)) {
  181. $line .= '</span>';
  182. }
  183. return $line;
  184. }
  185. }