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

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