diff --git a/assets/config.php b/assets/config.php index 9ff317ed..d15fb08d 100644 --- a/assets/config.php +++ b/assets/config.php @@ -43,6 +43,7 @@ return [ 'filesystem' => [ // https://tenancy.samuelstancl.me/docs/v2/filesystem-tenancy/ 'suffix_base' => 'tenant', 'suffix_storage_path' => true, // Note: Disabling this will likely break local disk tenancy. Only disable this if you're using an external file storage service like S3. + 'asset_helper_tenancy' => true, // should asset() be automatically tenant-aware. You may want to disable this if you use tools like Horizon. // Disks which should be suffixed with the suffix_base + tenant id. 'disks' => [ 'local', diff --git a/src/TenancyBootstrappers/FilesystemTenancyBootstrapper.php b/src/TenancyBootstrappers/FilesystemTenancyBootstrapper.php index 5d417f8b..35ea5b2b 100644 --- a/src/TenancyBootstrappers/FilesystemTenancyBootstrapper.php +++ b/src/TenancyBootstrappers/FilesystemTenancyBootstrapper.php @@ -4,7 +4,8 @@ declare(strict_types=1); namespace Stancl\Tenancy\TenancyBootstrappers; -use Illuminate\Contracts\Foundation\Application; +use Illuminate\Filesystem\FilesystemAdapter; +use Illuminate\Foundation\Application; use Illuminate\Support\Facades\Storage; use Stancl\Tenancy\Contracts\TenancyBootstrapper; use Stancl\Tenancy\Tenant; @@ -43,23 +44,27 @@ class FilesystemTenancyBootstrapper implements TenancyBootstrapper } // asset() - if ($this->originalPaths['asset_url']) { - $this->app['config']['app.asset_url'] = ($this->originalPaths['asset_url'] ?? $this->app['config']['app.url']) . "/$suffix"; - $this->app['url']->setAssetRoot($this->app['config']['app.asset_url']); - } else { - $this->app['url']->setAssetRoot($this->app['url']->route('stancl.tenancy.asset', ['path' => ''])); + if ($this->app['config']['tenancy.filesystem.asset_helper_tenancy'] ?? true) { + if ($this->originalPaths['asset_url']) { + $this->app['config']['app.asset_url'] = ($this->originalPaths['asset_url'] ?? $this->app['config']['app.url']) . "/$suffix"; + $this->app['url']->setAssetRoot($this->app['config']['app.asset_url']); + } else { + $this->app['url']->setAssetRoot($this->app['url']->route('stancl.tenancy.asset', ['path' => ''])); + } } // Storage facade foreach ($this->app['config']['tenancy.filesystem.disks'] as $disk) { - $this->originalPaths['disks'][$disk] = Storage::disk($disk)->getAdapter()->getPathPrefix(); + /** @var FilesystemAdapter $filesystemDisk */ + $filesystemDisk = Storage::disk($disk); + $this->originalPaths['disks'][$disk] = $filesystemDisk->getAdapter()->getPathPrefix(); if ($root = str_replace('%storage_path%', storage_path(), $this->app['config']["tenancy.filesystem.root_override.{$disk}"])) { - Storage::disk($disk)->getAdapter()->setPathPrefix($root); + $filesystemDisk->getAdapter()->setPathPrefix($root); } else { $root = $this->app['config']["filesystems.disks.{$disk}.root"]; - Storage::disk($disk)->getAdapter()->setPathPrefix($root . "/{$suffix}"); + $filesystemDisk->getAdapter()->setPathPrefix($root . "/{$suffix}"); } } } @@ -75,7 +80,10 @@ class FilesystemTenancyBootstrapper implements TenancyBootstrapper // Storage facade foreach ($this->app['config']['tenancy.filesystem.disks'] as $disk) { - Storage::disk($disk)->getAdapter()->setPathPrefix($this->originalPaths['disks'][$disk]); + /** @var FilesystemAdapter $filesystemDisk */ + $filesystemDisk = Storage::disk($disk); + + $filesystemDisk->getAdapter()->setPathPrefix($this->originalPaths['disks'][$disk]); } } } diff --git a/tests/TenantAssetTest.php b/tests/TenantAssetTest.php index 248e7703..b9bbabad 100644 --- a/tests/TenantAssetTest.php +++ b/tests/TenantAssetTest.php @@ -68,4 +68,20 @@ class TenantAssetTest extends TestCase $this->assertSame($original, global_asset('foobar')); } + + /** @test */ + public function asset_helper_tenancy_can_be_disabled() + { + $original = asset('foo'); + + config([ + 'app.asset_url' => null, + 'tenancy.filesystem.asset_helper_tenancy' => false, + ]); + + Tenant::create('foo.localhost'); + tenancy()->init('foo.localhost'); + + $this->assertSame($original, asset('foo')); + } }