Skip to content

Commit 4e0e122

Browse files
committed
[12.x] Add support for APP_LOCAL_SITES_PATH and APP_REMOTE_SITES_PATH to map editor file paths
1 parent fac2d77 commit 4e0e122

File tree

3 files changed

+152
-0
lines changed

3 files changed

+152
-0
lines changed

config/app.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,33 @@
6161

6262
'asset_url' => env('ASSET_URL'),
6363

64+
/*
65+
|--------------------------------------------------------------------------
66+
| Application Remote Path Mapping
67+
|--------------------------------------------------------------------------
68+
|
69+
| If you are using a remote dev server, like Laravel Homestead, Docker, or
70+
| even a remote VPS, it will be necessary to specify your path mapping.
71+
|
72+
| Leaving one, or both of these, empty or null will not trigger the remote
73+
| URL changes and Ignition will treat your editor links as local files.
74+
|
75+
| "remote_sites_path" is an absolute base path for your sites or projects
76+
| in Homestead, Vagrant, Docker, or another remote development server.
77+
|
78+
| Example value: "/home/vagrant/Code"
79+
|
80+
| "local_sites_path" is an absolute base path for your sites or projects
81+
| on your local computer where your IDE or code editor is running on.
82+
|
83+
| Example values: "/Users/<name>/Code", "C:\Users\<name>\Documents\Code"
84+
|
85+
*/
86+
87+
'remote_sites_path' => env('APP_REMOTE_SITES_PATH', base_path()),
88+
89+
'local_sites_path' => env('APP_LOCAL_SITES_PATH', null),
90+
6491
/*
6592
|--------------------------------------------------------------------------
6693
| Application Timezone

src/Illuminate/Foundation/Concerns/ResolvesDumpSource.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@ protected function resolveSourceHref($file, $line)
158158
return;
159159
}
160160

161+
$file = $this->mapRemotePathToLocal($file);
162+
161163
$href = is_array($editor) && isset($editor['href'])
162164
? $editor['href']
163165
: ($this->editorHrefs[$editor['name'] ?? $editor] ?? sprintf('%s://open?file={file}&line={line}', $editor['name'] ?? $editor));
@@ -173,6 +175,31 @@ protected function resolveSourceHref($file, $line)
173175
);
174176
}
175177

178+
/**
179+
* Map a remote path to a local path.
180+
*
181+
* This method is used to convert paths from a remote server to the local environment.
182+
* It uses configuration values for the remote and local site paths.
183+
*
184+
* @param string $path
185+
* @return string
186+
*/
187+
public function mapToLocalPath($path)
188+
{
189+
$remoteRoot = config('app.remote_sites_path');
190+
$hostRoot = config('app.local_sites_path');
191+
192+
if (!$remoteRoot || !$hostRoot || !$path) {
193+
return $path; // Return original path if config is not set
194+
}
195+
196+
if (str_starts_with($path, $remoteRoot)) {
197+
return $hostRoot . substr($path, strlen($remoteRoot));
198+
}
199+
200+
return $path;
201+
}
202+
176203
/**
177204
* Set the resolver that resolves the source of the dump call.
178205
*
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
3+
namespace Illuminate\Tests\Foundation\Configuration;
4+
5+
use Illuminate\Foundation\Concerns\ResolvesDumpSource;
6+
use PHPUnit\Framework\TestCase;
7+
use Illuminate\Config\Repository as Config;
8+
use Illuminate\Auth\AuthManager;
9+
use Illuminate\Container\Container;
10+
11+
class ResolvesDumpSourceTest extends TestCase
12+
{
13+
protected function setUp(): void
14+
{
15+
$container = Container::setInstance(new Container);
16+
17+
$this->auth = new AuthManager($container);
18+
19+
$container->singleton('config', function () {
20+
return $this->createConfig();
21+
});
22+
}
23+
24+
protected function getEnvironmentSetUp($app)
25+
{
26+
// Set config values to simulate Docker path mapping
27+
$app['config']->set('app.local_sites_path', '/path/to/my-app');
28+
$app['config']->set('app.remote_sites_path', '/var/www/html');
29+
}
30+
31+
public function testItMapsRemotePathToLocalPath()
32+
{
33+
$mock = new class {
34+
use ResolvesDumpSource;
35+
36+
public function testMap($path)
37+
{
38+
return $this->mapToLocalPath($path);
39+
}
40+
};
41+
42+
$originalPath = '/var/www/html/app/Http/Controllers/HomeController.php';
43+
$expectedPath = '/path/to/my-app/app/Http/Controllers/HomeController.php';
44+
45+
$mapped = $mock->testMap($originalPath);
46+
$this->assertEquals($expectedPath, $mapped);
47+
}
48+
49+
public function testItReturnsOriginalPathWhenNoConfigIsSet()
50+
{
51+
config()->set('app.local_sites_path', null);
52+
config()->set('app.remote_sites_path', null);
53+
54+
$mock = new class {
55+
use ResolvesDumpSource;
56+
57+
public function testMap($path)
58+
{
59+
return $this->mapToLocalPath($path);
60+
}
61+
};
62+
63+
$path = '/var/www/html/app/Console/Kernel.php';
64+
65+
$this->assertEquals($path, $mock->testMap($path));
66+
}
67+
68+
public function testItReturnsOriginalPathWhenPathDoesNotMatchRemote()
69+
{
70+
$mock = new class {
71+
use ResolvesDumpSource;
72+
73+
public function testMap($path)
74+
{
75+
return $this->mapToLocalPath($path);
76+
}
77+
};
78+
79+
$nonMatchingPath = '/srv/other/path/SomeFile.php';
80+
81+
$this->assertEquals($nonMatchingPath, $mock->testMap($nonMatchingPath));
82+
}
83+
84+
/**
85+
* Create a new config repository instance.
86+
*
87+
* @return \Illuminate\Config\Repository
88+
*/
89+
protected function createConfig()
90+
{
91+
return new Config([
92+
'app' => [
93+
'remote_sites_path' => '/var/www/html',
94+
'local_sites_path' => '/path/to/my-app',
95+
],
96+
]);
97+
}
98+
}

0 commit comments

Comments
 (0)