Skip to content

Commit ace5829

Browse files
committed
[12.x] Add support for APP_LOCAL_SITES_PATH and APP_REMOTE_SITES_PATH to map editor file paths
Update ResolvesDumpSource.php Update ResolvesDumpSourceTest.php Update ResolvesDumpSource.php Update ResolvesDumpSource.php Update ResolvesDumpSourceTest.php
1 parent fac2d77 commit ace5829

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->mapToLocalPath($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\Config\Repository as Config;
6+
use Illuminate\Container\Container;
7+
use Illuminate\Foundation\Concerns\ResolvesDumpSource;
8+
use PHPUnit\Framework\TestCase;
9+
10+
class ResolvesDumpSourceTest extends TestCase
11+
{
12+
protected function setUp(): void
13+
{
14+
$container = Container::setInstance(new Container);
15+
16+
$container->singleton('config', function () {
17+
return $this->createConfig();
18+
});
19+
}
20+
21+
protected function getEnvironmentSetUp($app)
22+
{
23+
// Set config values to simulate Docker path mapping
24+
$app['config']->set('app.local_sites_path', '/path/to/my-app');
25+
$app['config']->set('app.remote_sites_path', '/var/www/html');
26+
}
27+
28+
public function testItMapsRemotePathToLocalPath()
29+
{
30+
$mock = new class
31+
{
32+
use ResolvesDumpSource;
33+
34+
public function testMap($path)
35+
{
36+
return $this->mapToLocalPath($path);
37+
}
38+
};
39+
40+
$originalPath = '/var/www/html/app/Http/Controllers/HomeController.php';
41+
$expectedPath = '/path/to/my-app/app/Http/Controllers/HomeController.php';
42+
43+
$mapped = $mock->testMap($originalPath);
44+
$this->assertEquals($expectedPath, $mapped);
45+
}
46+
47+
public function testItReturnsOriginalPathWhenNoConfigIsSet()
48+
{
49+
config()->set('app.local_sites_path', null);
50+
config()->set('app.remote_sites_path', null);
51+
52+
$mock = new class
53+
{
54+
use ResolvesDumpSource;
55+
56+
public function testMap($path)
57+
{
58+
return $this->mapToLocalPath($path);
59+
}
60+
};
61+
62+
$path = '/var/www/html/app/Console/Kernel.php';
63+
64+
$this->assertEquals($path, $mock->testMap($path));
65+
}
66+
67+
public function testItReturnsOriginalPathWhenPathDoesNotMatchRemote()
68+
{
69+
$mock = new class
70+
{
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)