mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 14:34:04 +00:00
Make asset tenancy optional (#300)
This commit is contained in:
parent
98ce0ee294
commit
292b7acd16
3 changed files with 35 additions and 10 deletions
|
|
@ -43,6 +43,7 @@ return [
|
||||||
'filesystem' => [ // https://tenancy.samuelstancl.me/docs/v2/filesystem-tenancy/
|
'filesystem' => [ // https://tenancy.samuelstancl.me/docs/v2/filesystem-tenancy/
|
||||||
'suffix_base' => 'tenant',
|
'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.
|
'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 which should be suffixed with the suffix_base + tenant id.
|
||||||
'disks' => [
|
'disks' => [
|
||||||
'local',
|
'local',
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,8 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace Stancl\Tenancy\TenancyBootstrappers;
|
namespace Stancl\Tenancy\TenancyBootstrappers;
|
||||||
|
|
||||||
use Illuminate\Contracts\Foundation\Application;
|
use Illuminate\Filesystem\FilesystemAdapter;
|
||||||
|
use Illuminate\Foundation\Application;
|
||||||
use Illuminate\Support\Facades\Storage;
|
use Illuminate\Support\Facades\Storage;
|
||||||
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
|
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
|
||||||
use Stancl\Tenancy\Tenant;
|
use Stancl\Tenancy\Tenant;
|
||||||
|
|
@ -43,23 +44,27 @@ class FilesystemTenancyBootstrapper implements TenancyBootstrapper
|
||||||
}
|
}
|
||||||
|
|
||||||
// asset()
|
// asset()
|
||||||
|
if ($this->app['config']['tenancy.filesystem.asset_helper_tenancy'] ?? true) {
|
||||||
if ($this->originalPaths['asset_url']) {
|
if ($this->originalPaths['asset_url']) {
|
||||||
$this->app['config']['app.asset_url'] = ($this->originalPaths['asset_url'] ?? $this->app['config']['app.url']) . "/$suffix";
|
$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']);
|
$this->app['url']->setAssetRoot($this->app['config']['app.asset_url']);
|
||||||
} else {
|
} else {
|
||||||
$this->app['url']->setAssetRoot($this->app['url']->route('stancl.tenancy.asset', ['path' => '']));
|
$this->app['url']->setAssetRoot($this->app['url']->route('stancl.tenancy.asset', ['path' => '']));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Storage facade
|
// Storage facade
|
||||||
foreach ($this->app['config']['tenancy.filesystem.disks'] as $disk) {
|
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}"])) {
|
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 {
|
} else {
|
||||||
$root = $this->app['config']["filesystems.disks.{$disk}.root"];
|
$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
|
// Storage facade
|
||||||
foreach ($this->app['config']['tenancy.filesystem.disks'] as $disk) {
|
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]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -68,4 +68,20 @@ class TenantAssetTest extends TestCase
|
||||||
|
|
||||||
$this->assertSame($original, global_asset('foobar'));
|
$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'));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue