mirror of
https://github.com/Combodo/iTop.git
synced 2026-05-01 06:28:46 +02:00
migration symfony 5 4 (#300)
* symfony 5.4 (diff dev) * symfony 5.4 (working) * symfony 5.4 (update autoload) * symfony 5.4 (remove swiftmailer mailer implementation) * symfony 5.4 (php doc and split Global accessor class) ### Impacted packages: composer require php:">=7.2.5 <8.0.0" symfony/console:5.4.* symfony/dotenv:5.4.* symfony/framework-bundle:5.4.* symfony/twig-bundle:5.4.* symfony/yaml:5.4.* --update-with-dependencies composer require symfony/stopwatch:5.4.* symfony/web-profiler-bundle:5.4.* --dev --update-with-dependencies
This commit is contained in:
@@ -20,28 +20,26 @@ use Twig\TwigFilter;
|
||||
*
|
||||
* @author Fabien Potencier <fabien@symfony.com>
|
||||
*/
|
||||
class CodeExtension extends AbstractExtension
|
||||
final class CodeExtension extends AbstractExtension
|
||||
{
|
||||
private $fileLinkFormat;
|
||||
private $rootDir;
|
||||
private $charset;
|
||||
private $projectDir;
|
||||
|
||||
/**
|
||||
* @param string|FileLinkFormatter $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)
|
||||
public function __construct($fileLinkFormat, string $projectDir, string $charset)
|
||||
{
|
||||
$this->fileLinkFormat = $fileLinkFormat ?: ini_get('xdebug.file_link_format') ?: get_cfg_var('xdebug.file_link_format');
|
||||
$this->rootDir = str_replace('/', \DIRECTORY_SEPARATOR, \dirname($rootDir)).\DIRECTORY_SEPARATOR;
|
||||
$this->projectDir = str_replace('\\', '/', $projectDir).'/';
|
||||
$this->charset = $charset;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getFilters()
|
||||
public function getFilters(): array
|
||||
{
|
||||
return [
|
||||
new TwigFilter('abbr_class', [$this, 'abbrClass'], ['is_safe' => ['html']]),
|
||||
@@ -53,10 +51,11 @@ class CodeExtension extends AbstractExtension
|
||||
new TwigFilter('format_file_from_text', [$this, 'formatFileFromText'], ['is_safe' => ['html']]),
|
||||
new TwigFilter('format_log_message', [$this, 'formatLogMessage'], ['is_safe' => ['html']]),
|
||||
new TwigFilter('file_link', [$this, 'getFileLink']),
|
||||
new TwigFilter('file_relative', [$this, 'getFileRelative']),
|
||||
];
|
||||
}
|
||||
|
||||
public function abbrClass($class)
|
||||
public function abbrClass(string $class): string
|
||||
{
|
||||
$parts = explode('\\', $class);
|
||||
$short = array_pop($parts);
|
||||
@@ -64,10 +63,10 @@ class CodeExtension extends AbstractExtension
|
||||
return sprintf('<abbr title="%s">%s</abbr>', $class, $short);
|
||||
}
|
||||
|
||||
public function abbrMethod($method)
|
||||
public function abbrMethod(string $method): string
|
||||
{
|
||||
if (false !== strpos($method, '::')) {
|
||||
list($class, $method) = explode('::', $method, 2);
|
||||
if (str_contains($method, '::')) {
|
||||
[$class, $method] = explode('::', $method, 2);
|
||||
$result = sprintf('%s::%s()', $this->abbrClass($class), $method);
|
||||
} elseif ('Closure' === $method) {
|
||||
$result = sprintf('<abbr title="%s">%1$s</abbr>', $method);
|
||||
@@ -80,12 +79,8 @@ class CodeExtension extends AbstractExtension
|
||||
|
||||
/**
|
||||
* Formats an array as a string.
|
||||
*
|
||||
* @param array $args The argument array
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function formatArgs($args)
|
||||
public function formatArgs(array $args): string
|
||||
{
|
||||
$result = [];
|
||||
foreach ($args as $key => $item) {
|
||||
@@ -113,26 +108,16 @@ class CodeExtension extends AbstractExtension
|
||||
|
||||
/**
|
||||
* Formats an array as a string.
|
||||
*
|
||||
* @param array $args The argument array
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function formatArgsAsText($args)
|
||||
public function formatArgsAsText(array $args): string
|
||||
{
|
||||
return strip_tags($this->formatArgs($args));
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @param int $srcContext The number of displayed lines around or -1 for the whole file
|
||||
*
|
||||
* @return string An HTML string
|
||||
*/
|
||||
public function fileExcerpt($file, $line, $srcContext = 3)
|
||||
public function fileExcerpt(string $file, int $line, int $srcContext = 3): ?string
|
||||
{
|
||||
if (is_file($file) && is_readable($file)) {
|
||||
// highlight_file could throw warnings
|
||||
@@ -152,7 +137,7 @@ class CodeExtension extends AbstractExtension
|
||||
}
|
||||
|
||||
for ($i = max($line - $srcContext, 1), $max = min($line + $srcContext, \count($content)); $i <= $max; ++$i) {
|
||||
$lines[] = '<li'.($i == $line ? ' class="selected"' : '').'><a class="anchor" name="line'.$i.'"></a><code>'.self::fixCodeMarkup($content[$i - 1]).'</code></li>';
|
||||
$lines[] = '<li'.($i == $line ? ' class="selected"' : '').'><a class="anchor" id="line'.$i.'"></a><code>'.self::fixCodeMarkup($content[$i - 1]).'</code></li>';
|
||||
}
|
||||
|
||||
return '<ol start="'.max($line - $srcContext, 1).'">'.implode("\n", $lines).'</ol>';
|
||||
@@ -163,23 +148,16 @@ class CodeExtension extends AbstractExtension
|
||||
|
||||
/**
|
||||
* Formats a file path.
|
||||
*
|
||||
* @param string $file An absolute file path
|
||||
* @param int $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)
|
||||
public function formatFile(string $file, int $line, string $text = null): string
|
||||
{
|
||||
$file = trim($file);
|
||||
|
||||
if (null === $text) {
|
||||
$text = str_replace('/', \DIRECTORY_SEPARATOR, $file);
|
||||
if (0 === strpos($text, $this->rootDir)) {
|
||||
$text = substr($text, \strlen($this->rootDir));
|
||||
$text = explode(\DIRECTORY_SEPARATOR, $text, 2);
|
||||
$text = sprintf('<abbr title="%s%2$s">%s</abbr>%s', $this->rootDir, $text[0], isset($text[1]) ? \DIRECTORY_SEPARATOR.$text[1] : '');
|
||||
$text = $file;
|
||||
if (null !== $rel = $this->getFileRelative($text)) {
|
||||
$rel = explode('/', $rel, 2);
|
||||
$text = sprintf('<abbr title="%s%2$s">%s</abbr>%s', $this->projectDir, $rel[0], '/'.($rel[1] ?? ''));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -197,12 +175,9 @@ class CodeExtension extends AbstractExtension
|
||||
/**
|
||||
* Returns the link for a given file/line pair.
|
||||
*
|
||||
* @param string $file An absolute file path
|
||||
* @param int $line The line number
|
||||
*
|
||||
* @return string|false A link or false
|
||||
* @return string|false
|
||||
*/
|
||||
public function getFileLink($file, $line)
|
||||
public function getFileLink(string $file, int $line)
|
||||
{
|
||||
if ($fmt = $this->fileLinkFormat) {
|
||||
return \is_string($fmt) ? strtr($fmt, ['%f' => $file, '%l' => $line]) : $fmt->format($file, $line);
|
||||
@@ -211,7 +186,18 @@ class CodeExtension extends AbstractExtension
|
||||
return false;
|
||||
}
|
||||
|
||||
public function formatFileFromText($text)
|
||||
public function getFileRelative(string $file): ?string
|
||||
{
|
||||
$file = str_replace('\\', '/', $file);
|
||||
|
||||
if (null !== $this->projectDir && str_starts_with($file, $this->projectDir)) {
|
||||
return ltrim(substr($file, \strlen($this->projectDir)), '/');
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function formatFileFromText(string $text): string
|
||||
{
|
||||
return preg_replace_callback('/in ("|")?(.+?)\1(?: +(?:on|at))? +line (\d+)/s', function ($match) {
|
||||
return 'in '.$this->formatFile($match[2], $match[3]);
|
||||
@@ -221,9 +207,9 @@ class CodeExtension extends AbstractExtension
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
public function formatLogMessage($message, array $context)
|
||||
public function formatLogMessage(string $message, array $context): string
|
||||
{
|
||||
if ($context && false !== strpos($message, '{')) {
|
||||
if ($context && str_contains($message, '{')) {
|
||||
$replacements = [];
|
||||
foreach ($context as $key => $val) {
|
||||
if (is_scalar($val)) {
|
||||
@@ -239,15 +225,7 @@ class CodeExtension extends AbstractExtension
|
||||
return htmlspecialchars($message, \ENT_COMPAT | \ENT_SUBSTITUTE, $this->charset);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return 'code';
|
||||
}
|
||||
|
||||
protected static function fixCodeMarkup($line)
|
||||
protected static function fixCodeMarkup(string $line): string
|
||||
{
|
||||
// </span> ending tag from previous line
|
||||
$opening = strpos($line, '<span');
|
||||
|
||||
Reference in New Issue
Block a user