-
-
Notifications
You must be signed in to change notification settings - Fork 444
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* This adds support for tenancy aware Storage::url() method * Trigger CI build * Fixed Link command for Laravel v6, added StorageLink Events, more StorageLink tests, added RemoveStorageSymlinks Job, added Storage Jobs to TenancyServiceProvider stub, renamed misleading config example. * Fix typo * Fix code style (php-cs-fixer) * Update config comments * Format code in Link command, make writing more concise * Change "symLinks" to "symlinks" * Refactor Link command * Fix test name typo * Test fetching files using the public URL * Extract Link command logic into actions * Fix code style (php-cs-fixer) * Check if closure is null in CreateStorageSymlinksAction * Stop using command terminology in CreateStorageSymlinksAction * Separate the Storage::url() test cases * Update url_override comments * Remove afterLink closures, add types, move actions, add usage explanation to the symlink trait * Fix code style (php-cs-fixer) * Update public storage URL test * Fix issue with using str() * Improve url_override comment, add todos * add todo comment * fix docblock style * Add link command tests back * Add types to $tenants in the action handle() methods * Fix typo, update variable name formatting * Add tests for the symlink actions * Change possibleTenantSymlinks not to prefix the paths twice while tenancy is initialized * Fix code style (php-cs-fixer) * Stop testing storage directory existence in symlink test * Don't specify full namespace for Tenant model annotation * Don't specify full namespace in ActionTest * Remove "change to DI" todo * Remove possibleTenantSymlinks return annotation * Remove symlink-related jobs, instantiate and use actions * Revert "Remove symlink-related jobs, instantiate and use actions" This reverts commit 547440c. * Add a comment line about the possible tenant symlinks * Correct storagePath and publicPath variables * Revert "Correct storagePath and publicPath variables" This reverts commit e3aa8e2. * add a todo Co-authored-by: Martin Vlcek <martin@dontfreakout.eu> Co-authored-by: lukinovec <lukinovec@gmail.com> Co-authored-by: PHP CS Fixer <phpcsfixer@example.com>
- Loading branch information
1 parent
b78320b
commit 7bacc50
Showing
18 changed files
with
622 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Stancl\Tenancy\Actions; | ||
|
||
use Exception; | ||
use Illuminate\Support\Collection; | ||
use Illuminate\Support\LazyCollection; | ||
use Stancl\Tenancy\Concerns\DealsWithTenantSymlinks; | ||
use Stancl\Tenancy\Database\Models\Tenant; | ||
use Stancl\Tenancy\Events\CreatingStorageSymlink; | ||
use Stancl\Tenancy\Events\StorageSymlinkCreated; | ||
|
||
class CreateStorageSymlinksAction | ||
{ | ||
use DealsWithTenantSymlinks; | ||
|
||
public static function handle(Tenant|Collection|LazyCollection $tenants, bool $relativeLink = false, bool $force = false): void | ||
{ | ||
$tenants = $tenants instanceof Tenant ? collect([$tenants]) : $tenants; | ||
|
||
/** @var Tenant $tenant */ | ||
foreach ($tenants as $tenant) { | ||
foreach (static::possibleTenantSymlinks($tenant) as $publicPath => $storagePath) { | ||
static::createLink($publicPath, $storagePath, $tenant, $relativeLink, $force); | ||
} | ||
} | ||
} | ||
|
||
protected static function createLink(string $publicPath, string $storagePath, Tenant $tenant, bool $relativeLink, bool $force): void | ||
{ | ||
event(new CreatingStorageSymlink($tenant)); | ||
|
||
if (static::symlinkExists($publicPath)) { | ||
// If $force isn't passed, don't overwrite the existing symlink | ||
throw_if(! $force, new Exception("The [$publicPath] link already exists.")); | ||
|
||
app()->make('files')->delete($publicPath); | ||
} | ||
|
||
// Make sure the storage path exists before we create a symlink | ||
if (! is_dir($storagePath)) { | ||
mkdir($storagePath, 0777, true); | ||
} | ||
|
||
if ($relativeLink) { | ||
app()->make('files')->relativeLink($storagePath, $publicPath); | ||
} else { | ||
app()->make('files')->link($storagePath, $publicPath); | ||
} | ||
|
||
event((new StorageSymlinkCreated($tenant))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Stancl\Tenancy\Actions; | ||
|
||
use Illuminate\Support\Collection; | ||
use Illuminate\Support\LazyCollection; | ||
use Stancl\Tenancy\Concerns\DealsWithTenantSymlinks; | ||
use Stancl\Tenancy\Database\Models\Tenant; | ||
use Stancl\Tenancy\Events\RemovingStorageSymlink; | ||
use Stancl\Tenancy\Events\StorageSymlinkRemoved; | ||
|
||
class RemoveStorageSymlinksAction | ||
{ | ||
use DealsWithTenantSymlinks; | ||
|
||
public static function handle(Tenant|Collection|LazyCollection $tenants): void | ||
{ | ||
$tenants = $tenants instanceof Tenant ? collect([$tenants]) : $tenants; | ||
|
||
/** @var Tenant $tenant */ | ||
foreach ($tenants as $tenant) { | ||
foreach (static::possibleTenantSymlinks($tenant) as $publicPath => $storagePath) { | ||
static::removeLink($publicPath, $tenant); | ||
} | ||
} | ||
} | ||
|
||
protected static function removeLink(string $publicPath, Tenant $tenant): void | ||
{ | ||
if (static::symlinkExists($publicPath)) { | ||
event(new RemovingStorageSymlink($tenant)); | ||
|
||
app()->make('files')->delete($publicPath); | ||
|
||
event(new StorageSymlinkRemoved($tenant)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Stancl\Tenancy\Commands; | ||
|
||
use Exception; | ||
use Illuminate\Console\Command; | ||
use Illuminate\Support\LazyCollection; | ||
use Stancl\Tenancy\Actions\CreateStorageSymlinksAction; | ||
use Stancl\Tenancy\Actions\RemoveStorageSymlinksAction; | ||
use Stancl\Tenancy\Concerns\HasATenantsOption; | ||
|
||
class Link extends Command | ||
{ | ||
use HasATenantsOption; | ||
|
||
/** | ||
* The console command signature. | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = 'tenants:link | ||
{--tenants=* : The tenant(s) to run the command for. Default: all} | ||
{--relative : Create the symbolic link using relative paths} | ||
{--force : Recreate existing symbolic links} | ||
{--remove : Remove symbolic links}'; | ||
|
||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Create or remove tenant symbolic links.'; | ||
|
||
/** | ||
* Execute the console command. | ||
* | ||
* @return void | ||
*/ | ||
public function handle() | ||
{ | ||
$tenants = $this->getTenants(); | ||
|
||
try { | ||
if ($this->option('remove')) { | ||
$this->removeLinks($tenants); | ||
} else { | ||
$this->createLinks($tenants); | ||
} | ||
} catch (Exception $exception) { | ||
$this->error($exception->getMessage()); | ||
} | ||
} | ||
|
||
protected function removeLinks(LazyCollection $tenants): void | ||
{ | ||
RemoveStorageSymlinksAction::handle($tenants); | ||
|
||
$this->info('The links have been removed.'); | ||
} | ||
|
||
protected function createLinks(LazyCollection $tenants): void | ||
{ | ||
CreateStorageSymlinksAction::handle( | ||
$tenants, | ||
$this->option('relative') ?? false, | ||
$this->option('force') ?? false, | ||
); | ||
|
||
$this->info('The links have been created.'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Stancl\Tenancy\Concerns; | ||
|
||
use Illuminate\Support\Collection; | ||
use Stancl\Tenancy\Database\Models\Tenant; | ||
|
||
trait DealsWithTenantSymlinks | ||
{ | ||
/** | ||
* Get all possible tenant symlinks, existing or not (array of ['public path' => 'storage path']). | ||
* | ||
* Tenants can have a symlink for each disk registered in the tenancy.filesystem.url_override config. | ||
* | ||
* This is used for creating all possible tenant symlinks and removing all existing tenant symlinks. | ||
*/ | ||
protected static function possibleTenantSymlinks(Tenant $tenant): Collection | ||
{ | ||
$diskUrls = config('tenancy.filesystem.url_override'); | ||
$disks = config('tenancy.filesystem.root_override'); | ||
$suffixBase = config('tenancy.filesystem.suffix_base'); | ||
$symlinks = collect(); | ||
$tenantKey = $tenant->getTenantKey(); | ||
|
||
foreach ($diskUrls as $disk => $publicPath) { | ||
$storagePath = str_replace('%storage_path%', $suffixBase . $tenantKey, $disks[$disk]); | ||
$publicPath = str_replace('%tenant_id%', $tenantKey, $publicPath); | ||
|
||
tenancy()->central(function () use ($symlinks, $publicPath, $storagePath) { | ||
$symlinks->push([public_path($publicPath) => storage_path($storagePath)]); | ||
}); | ||
} | ||
|
||
return $symlinks->mapWithKeys(fn ($item) => $item); | ||
} | ||
|
||
/** Determine if the provided path is an existing symlink. */ | ||
protected static function symlinkExists(string $link): bool | ||
{ | ||
return file_exists($link) && is_link($link); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Stancl\Tenancy\Events; | ||
|
||
class CreatingStorageSymlink extends Contracts\TenantEvent | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Stancl\Tenancy\Events; | ||
|
||
class RemovingStorageSymlink extends Contracts\TenantEvent | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Stancl\Tenancy\Events; | ||
|
||
class StorageSymlinkCreated extends Contracts\TenantEvent | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Stancl\Tenancy\Events; | ||
|
||
class StorageSymlinkRemoved extends Contracts\TenantEvent | ||
{ | ||
} |
Oops, something went wrong.