Move IDE file link formats from FrameworkExtension to FileLinkFormatter

This commit is contained in:
MatTheCat 2021-04-30 14:55:51 +02:00
parent 8ff0a3e6f2
commit 42a27b2770
No known key found for this signature in database
GPG Key ID: FC932735DA4875E1
3 changed files with 21 additions and 15 deletions

View File

@ -294,20 +294,7 @@ class FrameworkExtension extends Extension
}
if (!$container->hasParameter('debug.file_link_format')) {
$links = [
'textmate' => 'txmt://open?url=file://%%f&line=%%l',
'macvim' => 'mvim://open?url=file://%%f&line=%%l',
'emacs' => 'emacs://open?url=file://%%f&line=%%l',
'sublime' => 'subl://open?url=file://%%f&line=%%l',
'phpstorm' => 'phpstorm://open?file=%%f&line=%%l',
'atom' => 'atom://core/open/file?filename=%%f&line=%%l',
'vscode' => 'vscode://file/%%f:%%l',
];
$ide = $config['ide'];
// mark any env vars found in the ide setting as used
$container->resolveEnvPlaceholders($ide);
$container->setParameter('debug.file_link_format', str_replace('%', '%%', ini_get('xdebug.file_link_format') ?: get_cfg_var('xdebug.file_link_format')) ?: ($links[$ide] ?? $ide));
$container->setParameter('debug.file_link_format', $config['ide']);
}
if (!empty($config['test'])) {

View File

@ -24,6 +24,16 @@ use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
*/
class FileLinkFormatter
{
private const FORMATS = [
'textmate' => 'txmt://open?url=file://%f&line=%l',
'macvim' => 'mvim://open?url=file://%f&line=%l',
'emacs' => 'emacs://open?url=file://%f&line=%l',
'sublime' => 'subl://open?url=file://%f&line=%l',
'phpstorm' => 'phpstorm://open?file=%f&line=%l',
'atom' => 'atom://core/open/file?filename=%f&line=%l',
'vscode' => 'vscode://file/%f:%l',
];
private $fileLinkFormat;
private $requestStack;
private $baseDir;
@ -34,7 +44,7 @@ class FileLinkFormatter
*/
public function __construct($fileLinkFormat = null, RequestStack $requestStack = null, string $baseDir = null, $urlFormat = null)
{
$fileLinkFormat = $fileLinkFormat ?: ini_get('xdebug.file_link_format') ?: get_cfg_var('xdebug.file_link_format');
$fileLinkFormat = (self::FORMATS[$fileLinkFormat] ?? $fileLinkFormat) ?: ini_get('xdebug.file_link_format') ?: get_cfg_var('xdebug.file_link_format');
if ($fileLinkFormat && !\is_array($fileLinkFormat)) {
$i = strpos($f = $fileLinkFormat, '&', max(strrpos($f, '%f'), strrpos($f, '%l'))) ?: \strlen($f);
$fileLinkFormat = [substr($f, 0, $i)] + preg_split('/&([^>]++)>/', substr($f, $i), -1, \PREG_SPLIT_DELIM_CAPTURE);

View File

@ -51,4 +51,13 @@ class FileLinkFormatterTest extends TestCase
$this->assertSame('http://www.example.org/_profiler/open?file=file.php&line=3#line3', $sut->format($file, 3));
}
public function testIdeFileLinkFormat()
{
$file = __DIR__.\DIRECTORY_SEPARATOR.'file.php';
$sut = new FileLinkFormatter('atom');
$this->assertSame("atom://core/open/file?filename=$file&line=3", $sut->format($file, 3));
}
}