Skip to content

[12.x] Add support for APP_LOCAL_SITES_PATH and APP_REMOTE_SITES_PATH to map to editor file paths #56264

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

Open
wants to merge 1 commit into
base: 12.x
Choose a base branch
from
Open
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
27 changes: 27 additions & 0 deletions config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,33 @@

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

/*
|--------------------------------------------------------------------------
| Application Remote Path Mapping
|--------------------------------------------------------------------------
|
| If you are using a remote dev server, like Laravel Homestead, Docker, or
| even a remote VPS, it will be necessary to specify your path mapping.
|
| Leaving one, or both of these, empty or null will not trigger the remote
| URL changes and Ignition will treat your editor links as local files.
|
| "remote_sites_path" is an absolute base path for your sites or projects
| in Homestead, Vagrant, Docker, or another remote development server.
|
| Example value: "/home/vagrant/Code"
|
| "local_sites_path" is an absolute base path for your sites or projects
| on your local computer where your IDE or code editor is running on.
|
| Example values: "/Users/<name>/Code", "C:\Users\<name>\Documents\Code"
|
*/

'remote_sites_path' => env('APP_REMOTE_SITES_PATH', base_path()),

'local_sites_path' => env('APP_LOCAL_SITES_PATH', null),

/*
|--------------------------------------------------------------------------
| Application Timezone
Expand Down
27 changes: 27 additions & 0 deletions src/Illuminate/Foundation/Concerns/ResolvesDumpSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ protected function resolveSourceHref($file, $line)
return;
}

$file = $this->mapToLocalPath($file);

$href = is_array($editor) && isset($editor['href'])
? $editor['href']
: ($this->editorHrefs[$editor['name'] ?? $editor] ?? sprintf('%s://open?file={file}&line={line}', $editor['name'] ?? $editor));
Expand All @@ -173,6 +175,31 @@ protected function resolveSourceHref($file, $line)
);
}

/**
* Map a remote path to a local path.
*
* This method is used to convert paths from a remote server to the local environment.
* It uses configuration values for the remote and local site paths.
*
* @param string $path
* @return string
*/
public function mapToLocalPath($path)
{
$remoteRoot = config('app.remote_sites_path');
$hostRoot = config('app.local_sites_path');

if (! $remoteRoot || ! $hostRoot || ! $path) {
return $path; // Return original path if config is not set
}

if (str_starts_with($path, $remoteRoot)) {
return $hostRoot.substr($path, strlen($remoteRoot));
}

return $path;
}

/**
* Set the resolver that resolves the source of the dump call.
*
Expand Down
98 changes: 98 additions & 0 deletions tests/Foundation/Concerns/ResolvesDumpSourceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

namespace Illuminate\Tests\Foundation\Configuration;

use Illuminate\Config\Repository as Config;
use Illuminate\Container\Container;
use Illuminate\Foundation\Concerns\ResolvesDumpSource;
use PHPUnit\Framework\TestCase;

class ResolvesDumpSourceTest extends TestCase
{
protected function setUp(): void
{
$container = Container::setInstance(new Container);

$container->singleton('config', function () {
return $this->createConfig();
});
}

protected function getEnvironmentSetUp($app)
{
// Set config values to simulate Docker path mapping
$app['config']->set('app.local_sites_path', '/path/to/my-app');
$app['config']->set('app.remote_sites_path', '/var/www/html');
}

public function testItMapsRemotePathToLocalPath()
{
$mock = new class
{
use ResolvesDumpSource;

public function testMap($path)
{
return $this->mapToLocalPath($path);
}
};

$originalPath = '/var/www/html/app/Http/Controllers/HomeController.php';
$expectedPath = '/path/to/my-app/app/Http/Controllers/HomeController.php';

$mapped = $mock->testMap($originalPath);
$this->assertEquals($expectedPath, $mapped);
}

public function testItReturnsOriginalPathWhenNoConfigIsSet()
{
config()->set('app.local_sites_path', null);
config()->set('app.remote_sites_path', null);

$mock = new class
{
use ResolvesDumpSource;

public function testMap($path)
{
return $this->mapToLocalPath($path);
}
};

$path = '/var/www/html/app/Console/Kernel.php';

$this->assertEquals($path, $mock->testMap($path));
}

public function testItReturnsOriginalPathWhenPathDoesNotMatchRemote()
{
$mock = new class
{
use ResolvesDumpSource;

public function testMap($path)
{
return $this->mapToLocalPath($path);
}
};

$nonMatchingPath = '/srv/other/path/SomeFile.php';

$this->assertEquals($nonMatchingPath, $mock->testMap($nonMatchingPath));
}

/**
* Create a new config repository instance.
*
* @return \Illuminate\Config\Repository
*/
protected function createConfig()
{
return new Config([
'app' => [
'remote_sites_path' => '/var/www/html',
'local_sites_path' => '/path/to/my-app',
],
]);
}
}