Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[9.x] Improves dd clickable link on multiple editors and docker environments #44406

Merged
merged 8 commits into from
Oct 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions src/Illuminate/Foundation/Concerns/ResolvesDumpSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,33 @@

namespace Illuminate\Foundation\Concerns;

use Throwable;

trait ResolvesDumpSource
{
/**
* All of the href formats for common editors.
*
* @var array<string, string>
*/
protected $editorHrefs = [
'atom' => 'atom://core/open/file?filename={file}&line={line}',
'emacs' => 'emacs://open?url=file://{file}&line={line}',
'idea' => 'idea://open?file={file}&line={line}',
'macvim' => 'mvim://open/?url=file://{file}&line={line}',
'netbeans' => 'netbeans://open/?f={file}:{line}',
'nova' => 'nova://core/open/file?filename={file}&line={line}',
'phpstorm' => 'phpstorm://open?file={file}&line={line}',
'sublime' => 'subl://open?url=file://{file}&line={line}',
'textmate' => 'txmt://open?url=file://{file}&line={line}',
'vscode' => 'vscode://file/{file}:{line}',
'vscode-insiders' => 'vscode-insiders://file/{file}:{line}',
'vscode-insiders-remote' => 'vscode-insiders://vscode-remote/{file}:{line}',
'vscode-remote' => 'vscode://vscode-remote/{file}:{line}',
'vscodium' => 'vscodium://file/{file}:{line}',
'xdebug' => 'xdebug://{file}@{line}',
];

/**
* The source resolver.
*
Expand Down Expand Up @@ -90,6 +115,42 @@ protected function getOriginalFileForCompiledView($file)
return $file;
}

/**
* Resolve the source href, if possible.
*
* @param string $file
* @param int|null $line
* @return string|null
*/
protected function resolveSourceHref($file, $line)
{
try {
$editor = config('app.editor');
} catch (Throwable $e) {
// ..
}

if (! isset($editor)) {
return;
}

$href = is_array($editor) && isset($editor['href'])
? $editor['href']
: ($this->editorHrefs[$editor['name'] ?? $editor] ?? sprintf('%s://open?file={file}&line={line}', $editor['name'] ?? $editor));

if ($basePath = $editor['base_path'] ?? false) {
$file = str_replace($this->basePath, $basePath, $file);
}

$href = str_replace(
['{file}', '{line}'],
[$file, is_null($line) ? 1 : $line],
$href,
);

return $href;
}

/**
* Set the resolver that resolves the source of the dump call.
*
Expand Down
7 changes: 4 additions & 3 deletions src/Illuminate/Foundation/Console/CliDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,11 @@ protected function getDumpSourceContent()

[$file, $relativeFile, $line] = $dumpSource;

$href = $this->resolveSourceHref($file, $line);

return sprintf(
' <fg=gray>// <fg=gray;href=file://%s%s>%s%s</></>',
$file,
is_null($line) ? '' : "#L$line",
' <fg=gray>// <fg=gray%s>%s%s</></>',
is_null($href) ? '' : ";href=$href",
$relativeFile,
is_null($line) ? '' : ":$line"
);
Expand Down
25 changes: 2 additions & 23 deletions src/Illuminate/Foundation/Http/HtmlDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Symfony\Component\VarDumper\Cloner\VarCloner;
use Symfony\Component\VarDumper\Dumper\HtmlDumper as BaseHtmlDumper;
use Symfony\Component\VarDumper\VarDumper;
use Throwable;

class HtmlDumper extends BaseHtmlDumper
{
Expand Down Expand Up @@ -132,30 +131,10 @@ protected function getDumpSourceContent()

$source = sprintf('%s%s', $relativeFile, is_null($line) ? '' : ":$line");

if ($editor = $this->editor()) {
$source = sprintf(
'<a href="%s://open?file=%s%s">%s</a>',
$editor,
$file,
is_null($line) ? '' : "&line=$line",
$source,
);
if ($href = $this->resolveSourceHref($file, $line)) {
$source = sprintf('<a href="%s">%s</a>', $href, $source);
}

return sprintf('<span style="color: #A0A0A0;"> // %s</span>', $source);
}

/**
* Get the application editor, if applicable.
*
* @return string|null
*/
protected function editor()
{
try {
return config('app.editor');
} catch (Throwable $e) {
// ...
}
}
}
77 changes: 77 additions & 0 deletions tests/Foundation/Http/HtmlDumperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Illuminate\Tests\Foundation\Http;

use Illuminate\Config\Repository;
use Illuminate\Container\Container;
use Illuminate\Foundation\Http\HtmlDumper;
use PHPUnit\Framework\TestCase;
use ReflectionClass;
Expand All @@ -11,6 +13,8 @@

class HtmlDumperTest extends TestCase
{
protected $app;

protected function setUp(): void
{
HtmlDumper::resolveDumpSourceUsing(function () {
Expand All @@ -20,6 +24,8 @@ protected function setUp(): void
18,
];
});

$this->app = Container::getInstance();
}

public function testString()
Expand Down Expand Up @@ -184,6 +190,76 @@ public function testUnresolvableLine()
$this->assertStringContainsString($expected, $output);
}

public function testHref()
{
$dumper = new HtmlDumper(
'/my-work-directory',
'/my-work-directory/storage/framework/views'
);

// Failure...
$href = (fn () => $this->resolveSourceHref(
'/my-work-directory/app/my-file',
10,
))->call($dumper);
$this->assertNull($href);

$config = new Repository();
$this->app->instance('config', $config);
$resolveSourceHref = fn () => (fn () => $this->resolveSourceHref(
'/my-work-directory/app/my-file',
10,
))->call($dumper);

// Empty...
$this->assertNull($resolveSourceHref());

// When editor name is provided...
$config->set('app.editor', 'phpstorm');
$this->assertSame(
'phpstorm://open?file=/my-work-directory/app/my-file&line=10', $resolveSourceHref()
);

// When editor name is provided on array format...
$config->set('app.editor', ['name' => 'phpstorm']);
$this->assertSame(
'phpstorm://open?file=/my-work-directory/app/my-file&line=10', $resolveSourceHref()
);

// When editor name and base path is provided on array format...
$config->set('app.editor', ['name' => 'phpstorm', 'base_path' => '/my-docker-work-directory']);
$this->assertSame(
'phpstorm://open?file=/my-docker-work-directory/app/my-file&line=10', $resolveSourceHref());

// When href is provided on array format...
$config->set('app.editor', ['href' => 'vscode://open?file={file}&line={line}']);
$this->assertSame(
'vscode://open?file=/my-work-directory/app/my-file&line=10', $resolveSourceHref()
);

// When href and base path is provided on array format...
$config->set('app.editor', ['href' => 'vscode://open?file={file}&line={line}', 'base_path' => '/my-docker-work-directory']);
$this->assertSame(
'vscode://open?file=/my-docker-work-directory/app/my-file&line=10', $resolveSourceHref()
);

// When editor name is provided...
$config->set('app.editor', 'sublime');
$this->assertSame('subl://open?url=file:///my-work-directory/app/my-file&line=10', $resolveSourceHref());

// Missing line
$config->set('app.editor', ['name' => 'vscode', 'base_path' => '/my-docker-work-directory']);

$href = (fn () => $this->resolveSourceHref(
'/my-work-directory/app/my-file',
null,
))->call($dumper);
$this->assertSame(
'vscode://file//my-docker-work-directory/app/my-file:1',
$href,
);
}

protected function dump($value)
{
$outputFile = stream_get_meta_data(tmpfile())['uri'];
Expand All @@ -205,5 +281,6 @@ protected function dump($value)
protected function tearDown(): void
{
HtmlDumper::resolveDumpSourceUsing(null);
Container::setInstance(null);
}
}