* * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Symfony\Bundle\FrameworkBundle\Templating\Helper; use Symfony\Component\Templating\Helper\Helper; if (!defined('ENT_SUBSTITUTE')) { define('ENT_SUBSTITUTE', 8); } /** * CodeHelper. * * @author Fabien Potencier */ class CodeHelper extends Helper { protected $fileLinkFormat; protected $rootDir; protected $charset; /** * Constructor. * * @param string $fileLinkFormat The format for links to source files * @param string $rootDir The project root directory * @param string $charset The charset */ public function __construct($fileLinkFormat, $rootDir, $charset) { $this->fileLinkFormat = empty($fileLinkFormat) ? ini_get('xdebug.file_link_format') : $fileLinkFormat; $this->rootDir = str_replace('\\', '/', $rootDir).'/'; $this->charset = $charset; } /** * Formats an array as a string. * * @param array $args The argument array * * @return string */ public function formatArgsAsText(array $args) { return strip_tags($this->formatArgs($args)); } public function abbrClass($class) { $parts = explode('\\', $class); $short = array_pop($parts); return sprintf("%s", $class, $short); } public function abbrMethod($method) { if (false !== strpos($method, '::')) { list($class, $method) = explode('::', $method, 2); $result = sprintf("%s::%s()", $this->abbrClass($class), $method); } elseif ('Closure' === $method) { $result = sprintf("%s", $method, $method); } else { $result = sprintf("%s()", $method, $method); } return $result; } /** * Formats an array as a string. * * @param array $args The argument array * * @return string */ public function formatArgs(array $args) { $result = array(); foreach ($args as $key => $item) { if ('object' === $item[0]) { $parts = explode('\\', $item[1]); $short = array_pop($parts); $formattedValue = sprintf("object(%s)", $item[1], $short); } elseif ('array' === $item[0]) { $formattedValue = sprintf("array(%s)", is_array($item[1]) ? $this->formatArgs($item[1]) : $item[1]); } elseif ('string' === $item[0]) { $formattedValue = sprintf("'%s'", htmlspecialchars($item[1], ENT_QUOTES, $this->getCharset())); } elseif ('null' === $item[0]) { $formattedValue = 'null'; } elseif ('boolean' === $item[0]) { $formattedValue = ''.strtolower(var_export($item[1], true)).''; } elseif ('resource' === $item[0]) { $formattedValue = 'resource'; } else { $formattedValue = str_replace("\n", '', var_export(htmlspecialchars((string) $item[1], ENT_QUOTES, $this->getCharset()), true)); } $result[] = is_int($key) ? $formattedValue : sprintf("'%s' => %s", $key, $formattedValue); } return implode(', ', $result); } /** * Returns an excerpt of a code file around the given line number. * * @param string $file A file path * @param int $line The selected line number * * @return string An HTML string */ public function fileExcerpt($file, $line) { if (is_readable($file)) { $code = highlight_file($file, true); // remove main code/span tags $code = preg_replace('#^\s*(.*)\s*#s', '\\1', $code); $content = preg_split('#
#', $code); $lines = array(); for ($i = max($line - 3, 1), $max = min($line + 3, count($content)); $i <= $max; $i++) { $lines[] = ''.self::fixCodeMarkup($content[$i - 1]).''; } return '
    '.implode("\n", $lines).'
'; } } /** * Formats a file path. * * @param string $file An absolute file path * @param integer $line The line number * @param string $text Use this text for the link rather than the file path * * @return string */ public function formatFile($file, $line, $text = null) { if (null === $text) { $file = trim($file); $fileStr = $file; if (0 === strpos($fileStr, $this->rootDir)) { $fileStr = str_replace($this->rootDir, '', str_replace('\\', '/', $fileStr)); $fileStr = sprintf('kernel.root_dir/%s', $this->rootDir, $fileStr); } $text = "$fileStr at line $line"; } if (false !== $link = $this->getFileLink($file, $line)) { return sprintf('%s', htmlspecialchars($link, ENT_QUOTES | ENT_SUBSTITUTE, $this->charset), $text); } return $text; } /** * Returns the link for a given file/line pair. * * @param string $file An absolute file path * @param integer $line The line number * * @return string A link of false */ public function getFileLink($file, $line) { if ($this->fileLinkFormat && is_file($file)) { return strtr($this->fileLinkFormat, array('%f' => $file, '%l' => $line)); } return false; } public function formatFileFromText($text) { $that = $this; return preg_replace_callback('/in ("|")?(.+?)\1(?: +(?:on|at))? +line (\d+)/s', function ($match) use ($that) { return 'in '.$that->formatFile($match[2], $match[3]); }, $text); } /** * Returns the canonical name of this helper. * * @return string The canonical name */ public function getName() { return 'code'; } protected static function fixCodeMarkup($line) { // ending tag from previous line $opening = strpos($line, ''); if (false !== $closing && (false === $opening || $closing < $opening)) { $line = substr_replace($line, '', $closing, 7); } // missing tag at the end of line $opening = strpos($line, ''); if (false !== $opening && (false === $closing || $closing > $opening)) { $line .= ''; } return $line; } }