Skip to content

Commit

Permalink
fix : adds support for handling URLs of local storage disks in `Files…
Browse files Browse the repository at this point in the history
…ystemSuffixedTask::makeCurrent`.
  • Loading branch information
andrextor committed Jan 30, 2025
1 parent 2dd8a36 commit 63780ca
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [3.0.5 (2025-01-31)](https://github.com/placetopay-org/cerberus/compare/3.0.5...3.0.4)

### Fixed:

- Adds support for handling URLs of local storage disks in `FilesystemSuffixedTask::makeCurrent`.

## [3.0.4 (2024-07-30)](https://github.com/placetopay-org/cerberus/compare/3.0.3...3.0.4)

### Fixed
Expand Down
14 changes: 12 additions & 2 deletions src/Tasks/FilesystemSuffixedTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Foundation\Application;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Spatie\Multitenancy\Models\Tenant;
use Spatie\Multitenancy\Tasks\SwitchTenantTask;

Expand Down Expand Up @@ -49,10 +50,19 @@ public function makeCurrent(Tenant $tenant): void
$this->originalPaths['disks'][$disk] = $originalRoot;

$finalPrefix = $originalRoot
? rtrim($originalRoot, '/').'/'.$suffix
: $suffix;
? rtrim($originalRoot, '/').'/'.$suffix
: $suffix;

$this->app['config']["filesystems.disks.$disk.root"] = $finalPrefix;

if ($originalDiskUrl = config("filesystems.disks.$disk.url")) {
$tenantConfigUrl = $tenant->getConfig()['app']['url'] ?? $this->app['config']['app']['url'];

$originalDiskUrl = Str::finish(ltrim(parse_url($originalDiskUrl, PHP_URL_PATH), '/'), '/');
$url = Str::finish($tenantConfigUrl, '/').$originalDiskUrl.$suffix;

$this->app['config']["filesystems.disks.$disk.url"] = $url;
}
}
}

Expand Down
37 changes: 37 additions & 0 deletions tests/Tasks/FilesystemSuffixedTaskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,41 @@ public function it_overwrite_storage_path()
$this->assertEquals($originalStoragePath, storage_path());
$this->assertStringNotContainsString($this->tenant->name, $originalStoragePath);
}

/**
* @test
* @dataProvider filesystemDisksUrlDataProvider
*/
public function it_overwrite_filesystem_disks_url($appUrl, $storageUrl, $expectedUrl)
{
$config = array_merge($this->tenant->config, ['app' => ['url' => $appUrl]]);
$this->tenant->update(['config' => $config]);

$disks = [
'local' => ['driver' => 'local', 'url' => $storageUrl, 'root' => 'fake/storage'],
'public' => ['driver' => 'local', 'url' => $storageUrl, 'root' => 'fake/storage'],
];

config()->set('filesystems.disks', $disks);
$this->tenant->makeCurrent();

$this->assertSame($expectedUrl, Storage::disk('local')->url(''));
$this->assertSame($expectedUrl, Storage::disk('public')->url(''));
}

public static function filesystemDisksUrlDataProvider(): array
{
return [
'tenant_without_trailing_slash' => [
'app_url' => 'https://tenant.test',
'storage_url' => 'https://tenant_1.test/storage',
'expected_url' => 'https://tenant.test/storage/tenants/tenant_1/',
],
'tenant_with_trailing_slash' => [
'app_url' => 'https://tenant_2.test/',
'storage_url' => 'https://tenant_2.test/storage/',
'expected_url' => 'https://tenant_2.test/storage/tenants/tenant_1/',
],
];
}
}

0 comments on commit 63780ca

Please sign in to comment.