CodeHelper.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace Symfony\Bundle\FrameworkBundle\Templating\Helper;
  3. use Symfony\Components\Templating\Helper\Helper;
  4. /*
  5. * This file is part of the Symfony framework.
  6. *
  7. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  8. *
  9. * This source file is subject to the MIT license that is bundled
  10. * with this source code in the file LICENSE.
  11. */
  12. /**
  13. * CodeHelper.
  14. *
  15. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  16. */
  17. class CodeHelper extends Helper
  18. {
  19. protected $fileLinkFormat;
  20. /**
  21. * Constructor.
  22. *
  23. * @param string $fileLinkFormat The format for links to source files
  24. */
  25. public function __construct($fileLinkFormat)
  26. {
  27. $this->fileLinkFormat = null !== $fileLinkFormat ? $fileLinkFormat : ini_get('xdebug.file_link_format');
  28. }
  29. /**
  30. * Formats an array as a string.
  31. *
  32. * @param array $args The argument array
  33. *
  34. * @return string
  35. */
  36. public function formatArgs($args)
  37. {
  38. $result = array();
  39. foreach ($args as $key => $value) {
  40. if (is_object($value)) {
  41. $formattedValue = sprintf("object('%s')", get_class($value));
  42. } elseif (is_array($value)) {
  43. $formattedValue = sprintf("array(%s)", $this->formatArgs($value));
  44. } elseif (is_string($value)) {
  45. $formattedValue = sprintf("'%s'", $value);
  46. } elseif (null === $value) {
  47. $formattedValue = 'null';
  48. } else {
  49. $formattedValue = $value;
  50. }
  51. $result[] = is_int($key) ? $formattedValue : sprintf("'%s' => %s", $key, $formattedValue);
  52. }
  53. return implode(', ', $result);
  54. }
  55. /**
  56. * Returns an excerpt of a code file around the given line number.
  57. *
  58. * @param string $file A file path
  59. * @param int $line The selected line number
  60. *
  61. * @return string An HTML string
  62. */
  63. public function fileExcerpt($file, $line)
  64. {
  65. if (is_readable($file)) {
  66. $content = preg_split('#<br />#', highlight_file($file, true));
  67. $lines = array();
  68. for ($i = max($line - 3, 1), $max = min($line + 3, count($content)); $i <= $max; $i++) {
  69. $lines[] = '<li'.($i == $line ? ' class="selected"' : '').'>'.$content[$i - 1].'</li>';
  70. }
  71. return '<ol start="'.max($line - 3, 1).'">'.implode("\n", $lines).'</ol>';
  72. }
  73. }
  74. /**
  75. * Formats a file path.
  76. *
  77. * @param string $file An absolute file path
  78. * @param integer $line The line number
  79. * @param string $format The output format (txt or html)
  80. * @param string $text Use this text for the link rather than the file path
  81. *
  82. * @return string
  83. */
  84. public function formatFile($file, $line)
  85. {
  86. if (!$this->fileLinkFormat) {
  87. return $file;
  88. }
  89. $link = strtr($this->fileLinkFormat, array('%f' => $file, '%l' => $line));
  90. return sprintf('<a href="%s" title="Click to open this file" class="file_link">%s</a>', $link, $file);
  91. }
  92. /**
  93. * Returns the canonical name of this helper.
  94. *
  95. * @return string The canonical name
  96. */
  97. public function getName()
  98. {
  99. return 'code';
  100. }
  101. }