4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Bridge\Twig\Extension
;
14 if (!defined('ENT_SUBSTITUTE')) {
15 define('ENT_SUBSTITUTE', 8);
19 * Twig extension relate to PHP code and used by the profiler and the default exception templates.
21 * @author Fabien Potencier <fabien@symfony.com>
23 class CodeExtension
extends \Twig_Extension
25 private $fileLinkFormat;
32 * @param string $fileLinkFormat The format for links to source files
33 * @param string $rootDir The project root directory
34 * @param string $charset The charset
36 public function __construct($fileLinkFormat, $rootDir, $charset)
38 $this->fileLinkFormat
= empty($fileLinkFormat) ? ini_get('xdebug.file_link_format') : $fileLinkFormat;
39 $this->rootDir
= str_replace('\\', '/', $rootDir).'/';
40 $this->charset
= $charset;
46 public function getFilters()
49 'abbr_class' => new \
Twig_Filter_Method($this, 'abbrClass', array('is_safe' => array('html'))),
50 'abbr_method' => new \
Twig_Filter_Method($this, 'abbrMethod', array('is_safe' => array('html'))),
51 'format_args' => new \
Twig_Filter_Method($this, 'formatArgs', array('is_safe' => array('html'))),
52 'format_args_as_text' => new \
Twig_Filter_Method($this, 'formatArgsAsText'),
53 'file_excerpt' => new \
Twig_Filter_Method($this, 'fileExcerpt', array('is_safe' => array('html'))),
54 'format_file' => new \
Twig_Filter_Method($this, 'formatFile', array('is_safe' => array('html'))),
55 'format_file_from_text' => new \
Twig_Filter_Method($this, 'formatFileFromText', array('is_safe' => array('html'))),
56 'file_link' => new \
Twig_Filter_Method($this, 'getFileLink', array('is_safe' => array('html'))),
60 public function abbrClass($class)
62 $parts = explode('\\', $class);
63 $short = array_pop($parts);
65 return sprintf("<abbr title=\"%s\">%s</abbr>", $class, $short);
68 public function abbrMethod($method)
70 if (false !== strpos($method, '::')) {
71 list($class, $method) = explode('::', $method, 2);
72 $result = sprintf("%s::%s()", $this->abbrClass($class), $method);
73 } elseif ('Closure' === $method) {
74 $result = sprintf("<abbr title=\"%s\">%s</abbr>", $method, $method);
76 $result = sprintf("<abbr title=\"%s\">%s</abbr>()", $method, $method);
83 * Formats an array as a string.
85 * @param array $args The argument array
89 public function formatArgs($args)
92 foreach ($args as $key => $item) {
93 if ('object' === $item[0]) {
94 $parts = explode('\\', $item[1]);
95 $short = array_pop($parts);
96 $formattedValue = sprintf("<em>object</em>(<abbr title=\"%s\">%s</abbr>)", $item[1], $short);
97 } elseif ('array' === $item[0]) {
98 $formattedValue = sprintf("<em>array</em>(%s)", is_array($item[1]) ? $this->formatArgs($item[1]) : $item[1]);
99 } elseif ('string' === $item[0]) {
100 $formattedValue = sprintf("'%s'", htmlspecialchars($item[1], ENT_QUOTES
, $this->charset
));
101 } elseif ('null' === $item[0]) {
102 $formattedValue = '<em>null</em>';
103 } elseif ('boolean' === $item[0]) {
104 $formattedValue = '<em>'.strtolower(var_export($item[1], true)).'</em>';
105 } elseif ('resource' === $item[0]) {
106 $formattedValue = '<em>resource</em>';
108 $formattedValue = str_replace("\n", '', var_export(htmlspecialchars((string) $item[1], ENT_QUOTES
, $this->charset
), true));
111 $result[] = is_int($key) ? $formattedValue : sprintf("'%s' => %s", $key, $formattedValue);
114 return implode(', ', $result);
118 * Formats an array as a string.
120 * @param array $args The argument array
124 public function formatArgsAsText($args)
126 return strip_tags($this->formatArgs($args));
130 * Returns an excerpt of a code file around the given line number.
132 * @param string $file A file path
133 * @param int $line The selected line number
135 * @return string An HTML string
137 public function fileExcerpt($file, $line)
139 if (is_readable($file)) {
140 $code = highlight_file($file, true);
141 // remove main code/span tags
142 $code = preg_replace('#^<code.*?>\s*<span.*?>(.*)</span>\s*</code>#s', '\\1', $code);
143 $content = preg_split('#<br />#', $code);
146 for ($i = max($line - 3, 1), $max = min($line +
3, count($content)); $i <= $max; $i++
) {
147 $lines[] = '<li'.($i == $line ? ' class="selected"' : '').'><code>'.self
::fixCodeMarkup($content[$i - 1]).'</code></li>';
150 return '<ol start="'.max($line - 3, 1).'">'.implode("\n", $lines).'</ol>';
155 * Formats a file path.
157 * @param string $file An absolute file path
158 * @param integer $line The line number
159 * @param string $text Use this text for the link rather than the file path
163 public function formatFile($file, $line, $text = null)
165 if (null === $text) {
168 if (0 === strpos($text, $this->rootDir
)) {
169 $text = str_replace($this->rootDir
, '', str_replace('\\', '/', $text));
170 $text = sprintf('<abbr title="%s">kernel.root_dir</abbr>/%s', $this->rootDir
, $text);
174 $text = "$text at line $line";
176 if (false !== $link = $this->getFileLink($file, $line)) {
177 return sprintf('<a href="%s
" title="Click to open this file
" class="file_link
">%s</a>', htmlspecialchars($link, ENT_QUOTES | ENT_SUBSTITUTE, $this->charset), $text);
184 * Returns the link for a given file/line pair.
186 * @param string $file An absolute file path
187 * @param integer $line The line number
189 * @return string A link of false
191 public function getFileLink($file, $line)
193 if ($this->fileLinkFormat && is_file($file)) {
194 return strtr($this->fileLinkFormat, array('%f' => $file, '%l' => $line));
200 public function formatFileFromText($text)
204 return preg_replace_callback('/in ("|"
;)?(.+
?)\
1(?: +
(?:on
|at
))? +
line (\d+
)/s
', function ($match) use ($that) {
205 return 'in
'.$that->formatFile($match[2], $match[3]);
209 public function getName()
214 protected static function fixCodeMarkup($line)
216 // </span> ending tag from previous line
217 $opening = strpos($line, '<span
');
218 $closing = strpos($line, '</span
>');
219 if (false !== $closing && (false === $opening || $closing < $opening)) {
220 $line = substr_replace($line, '', $closing, 7);
223 // missing </span> tag at the end of line
224 $opening = strpos($line, '<span
');
225 $closing = strpos($line, '</span
>');
226 if (false !== $opening && (false === $closing || $closing > $opening)) {