mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-14 00:14:04 +00:00
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.
This commit is contained in:
parent
717b834c51
commit
5ed5aea6d6
11 changed files with 253 additions and 15 deletions
|
|
@ -4,11 +4,11 @@ declare(strict_types=1);
|
|||
|
||||
namespace Stancl\Tenancy\Commands;
|
||||
|
||||
use Illuminate\Foundation\Console\StorageLinkCommand;
|
||||
use Illuminate\Console\Command;
|
||||
use Stancl\Tenancy\Concerns\HasATenantsOption;
|
||||
use Stancl\Tenancy\Contracts\Tenant;
|
||||
|
||||
class Link extends StorageLinkCommand
|
||||
class Link extends Command
|
||||
{
|
||||
use HasATenantsOption;
|
||||
|
||||
|
|
@ -20,7 +20,8 @@ class Link extends StorageLinkCommand
|
|||
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}';
|
||||
{--force : Recreate existing symbolic links}
|
||||
{--remove : Remove symbolic links}';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
|
|
@ -29,6 +30,51 @@ class Link extends StorageLinkCommand
|
|||
*/
|
||||
protected $description = 'Create the symbolic links configured for the tenancy applications';
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$relative = $this->option('relative');
|
||||
|
||||
if ($this->option('remove')) {
|
||||
foreach ($this->links() as $link => $target) {
|
||||
if (is_link($link)) {
|
||||
$this->laravel->make('files')->delete($link);
|
||||
|
||||
$this->info("The [$link] link has been removed.");
|
||||
}
|
||||
}
|
||||
|
||||
$this->info('The links have been removed.');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ($this->links() as $link => $target) {
|
||||
if (file_exists($link) && ! $this->isRemovableSymlink($link, $this->option('force'))) {
|
||||
$this->error("The [$link] link already exists.");
|
||||
continue;
|
||||
}
|
||||
|
||||
if (is_link($link)) {
|
||||
$this->laravel->make('files')->delete($link);
|
||||
}
|
||||
|
||||
if ($relative) {
|
||||
$this->laravel->make('files')->relativeLink($target, $link);
|
||||
} else {
|
||||
$this->laravel->make('files')->link($target, $link);
|
||||
}
|
||||
|
||||
$this->info("The [$link] link has been connected to [$target].");
|
||||
}
|
||||
|
||||
$this->info('The links have been created.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the symbolic links that are configured for the application.
|
||||
*
|
||||
|
|
@ -40,16 +86,20 @@ class Link extends StorageLinkCommand
|
|||
$disks = config('tenancy.filesystem.root_override');
|
||||
$suffix_base = config('tenancy.filesystem.suffix_base');
|
||||
|
||||
return $this->getTenants()
|
||||
->map(function (Tenant $tenant) use ($suffix_base, $disk_urls, $disks) {
|
||||
$tenants = $this->option('remove') && filled($this->option('tenants'))
|
||||
? collect($this->option('tenants'))
|
||||
: $this->getTenants()->map(function(Tenant $tenant) { return $tenant->getTenantKey(); });
|
||||
|
||||
return $tenants
|
||||
->map(function ($tenant_key) use ($suffix_base, $disk_urls, $disks) {
|
||||
|
||||
$map = [];
|
||||
|
||||
foreach ($disk_urls as $disk => $public_path) {
|
||||
$storage_path = str_replace('%storage_path%', $suffix_base . $tenant['id'], $disks[$disk]);
|
||||
$storage_path = str_replace('%storage_path%', $suffix_base . $tenant_key, $disks[$disk]);
|
||||
$storage_path = storage_path($storage_path);
|
||||
|
||||
$public_path = str_replace('%tenant_id%', $tenant['id'], $public_path);
|
||||
$public_path = str_replace('%tenant_id%', $tenant_key, $public_path);
|
||||
$public_path = public_path($public_path);
|
||||
|
||||
// make sure storage path exist before we create symlink
|
||||
|
|
@ -63,7 +113,19 @@ class Link extends StorageLinkCommand
|
|||
return $map;
|
||||
|
||||
})->flatten(1)
|
||||
->mapWithKeys(fn ($item) => $item)
|
||||
->mapWithKeys(function ($item) {return $item; })
|
||||
->all();
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the provided path is a symlink that can be removed.
|
||||
*
|
||||
* @param string $link
|
||||
* @param bool $force
|
||||
* @return bool
|
||||
*/
|
||||
protected function isRemovableSymlink(string $link, bool $force): bool
|
||||
{
|
||||
return is_link($link) && $force;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
9
src/Events/CreatingStorageSymlink.php
Normal file
9
src/Events/CreatingStorageSymlink.php
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Stancl\Tenancy\Events;
|
||||
|
||||
class CreatingStorageSymlink extends Contracts\TenantEvent
|
||||
{
|
||||
}
|
||||
9
src/Events/RemovingStorageSymlink.php
Normal file
9
src/Events/RemovingStorageSymlink.php
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Stancl\Tenancy\Events;
|
||||
|
||||
class RemovingStorageSymlink extends Contracts\TenantEvent
|
||||
{
|
||||
}
|
||||
9
src/Events/StorageSymlinkCreated.php
Normal file
9
src/Events/StorageSymlinkCreated.php
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Stancl\Tenancy\Events;
|
||||
|
||||
class StorageSymlinkCreated extends Contracts\TenantEvent
|
||||
{
|
||||
}
|
||||
9
src/Events/StorageSymlinkRemoved.php
Normal file
9
src/Events/StorageSymlinkRemoved.php
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Stancl\Tenancy\Events;
|
||||
|
||||
class StorageSymlinkRemoved extends Contracts\TenantEvent
|
||||
{
|
||||
}
|
||||
|
|
@ -12,6 +12,8 @@ use Illuminate\Queue\InteractsWithQueue;
|
|||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Support\Facades\Artisan;
|
||||
use Stancl\Tenancy\Contracts\Tenant;
|
||||
use Stancl\Tenancy\Events\CreatingStorageSymlink;
|
||||
use Stancl\Tenancy\Events\StorageSymlinkCreated;
|
||||
|
||||
class CreateStorageSymlinks implements ShouldQueue
|
||||
{
|
||||
|
|
@ -39,8 +41,12 @@ class CreateStorageSymlinks implements ShouldQueue
|
|||
*/
|
||||
public function handle()
|
||||
{
|
||||
event(new CreatingStorageSymlink($this->tenant));
|
||||
|
||||
Artisan::call('tenants:link', [
|
||||
'--tenants' => [$this->tenant->getTenantKey()],
|
||||
]);
|
||||
|
||||
event(new StorageSymlinkCreated($this->tenant));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
55
src/Jobs/RemoveStorageSymlinks.php
Normal file
55
src/Jobs/RemoveStorageSymlinks.php
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Stancl\Tenancy\Jobs;
|
||||
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldBeUnique;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Support\Facades\Artisan;
|
||||
use Stancl\Tenancy\Contracts\Tenant;
|
||||
use Stancl\Tenancy\Events\CreatingStorageSymlink;
|
||||
use Stancl\Tenancy\Events\RemovingStorageSymlink;
|
||||
use Stancl\Tenancy\Events\StorageSymlinkCreated;
|
||||
use Stancl\Tenancy\Events\StorageSymlinkRemoved;
|
||||
|
||||
class RemoveStorageSymlinks implements ShouldQueue
|
||||
{
|
||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||
|
||||
/**
|
||||
* @var \Stancl\Tenancy\Contracts\Tenant
|
||||
*/
|
||||
public Tenant $tenant;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(Tenant $tenant)
|
||||
{
|
||||
$this->tenant = $tenant;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
event(new RemovingStorageSymlink($this->tenant));
|
||||
|
||||
Artisan::call('tenants:link', [
|
||||
'--remove' => true,
|
||||
'--tenants' => [$this->tenant->getTenantKey()],
|
||||
]);
|
||||
|
||||
event(new StorageSymlinkRemoved($this->tenant));
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue