mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-06 08:04:03 +00:00
Merge branch 'master' of https://github.com/archtechx/tenancy into add-skip-failing-options-to-migrate
This commit is contained in:
commit
5c7a2c26ad
24 changed files with 828 additions and 35 deletions
55
src/Actions/CreateStorageSymlinksAction.php
Normal file
55
src/Actions/CreateStorageSymlinksAction.php
Normal file
|
|
@ -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)));
|
||||
}
|
||||
}
|
||||
40
src/Actions/RemoveStorageSymlinksAction.php
Normal file
40
src/Actions/RemoveStorageSymlinksAction.php
Normal file
|
|
@ -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));
|
||||
}
|
||||
}
|
||||
}
|
||||
41
src/Bootstrappers/BatchTenancyBootstrapper.php
Normal file
41
src/Bootstrappers/BatchTenancyBootstrapper.php
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Stancl\Tenancy\Bootstrappers;
|
||||
|
||||
use Illuminate\Bus\DatabaseBatchRepository;
|
||||
use Illuminate\Database\Connection;
|
||||
use Illuminate\Database\DatabaseManager;
|
||||
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
|
||||
use Stancl\Tenancy\Contracts\Tenant;
|
||||
|
||||
class BatchTenancyBootstrapper implements TenancyBootstrapper
|
||||
{
|
||||
/**
|
||||
* The previous database connection instance.
|
||||
*/
|
||||
protected ?Connection $previousConnection = null;
|
||||
|
||||
public function __construct(
|
||||
protected DatabaseBatchRepository $batchRepository,
|
||||
protected DatabaseManager $databaseManager
|
||||
) {
|
||||
}
|
||||
|
||||
public function bootstrap(Tenant $tenant)
|
||||
{
|
||||
// Update batch repository connection to use the tenant connection
|
||||
$this->previousConnection = $this->batchRepository->getConnection();
|
||||
$this->batchRepository->setConnection($this->databaseManager->connection('tenant'));
|
||||
}
|
||||
|
||||
public function revert()
|
||||
{
|
||||
if ($this->previousConnection) {
|
||||
// Replace batch repository connection with the previously replaced one
|
||||
$this->batchRepository->setConnection($this->previousConnection);
|
||||
$this->previousConnection = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -57,9 +57,10 @@ class FilesystemTenancyBootstrapper implements TenancyBootstrapper
|
|||
|
||||
foreach ($this->app['config']['tenancy.filesystem.disks'] as $disk) {
|
||||
// todo@v4 \League\Flysystem\PathPrefixer is making this a lot more painful in flysystem v2
|
||||
$diskConfig = $this->app['config']["filesystems.disks.{$disk}"];
|
||||
$originalRoot = $diskConfig['root'] ?? null;
|
||||
|
||||
$originalRoot = $this->app['config']["filesystems.disks.{$disk}.root"];
|
||||
$this->originalPaths['disks'][$disk] = $originalRoot;
|
||||
$this->originalPaths['disks']['path'][$disk] = $originalRoot;
|
||||
|
||||
$finalPrefix = str_replace(
|
||||
['%storage_path%', '%tenant%'],
|
||||
|
|
@ -74,6 +75,19 @@ class FilesystemTenancyBootstrapper implements TenancyBootstrapper
|
|||
}
|
||||
|
||||
$this->app['config']["filesystems.disks.{$disk}.root"] = $finalPrefix;
|
||||
|
||||
// Storage Url
|
||||
if ($diskConfig['driver'] === 'local') {
|
||||
$this->originalPaths['disks']['url'][$disk] = $diskConfig['url'] ?? null;
|
||||
|
||||
if ($url = str_replace(
|
||||
'%tenant_id%',
|
||||
$tenant->getTenantKey(),
|
||||
$this->app['config']["tenancy.filesystem.url_override.{$disk}"] ?? ''
|
||||
)) {
|
||||
$this->app['config']["filesystems.disks.{$disk}.url"] = url($url);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -88,8 +102,16 @@ class FilesystemTenancyBootstrapper implements TenancyBootstrapper
|
|||
|
||||
// Storage facade
|
||||
Storage::forgetDisk($this->app['config']['tenancy.filesystem.disks']);
|
||||
foreach ($this->app['config']['tenancy.filesystem.disks'] as $disk) {
|
||||
$this->app['config']["filesystems.disks.{$disk}.root"] = $this->originalPaths['disks'][$disk];
|
||||
foreach ($this->app['config']['tenancy.filesystem.disks'] as $diskName) {
|
||||
$this->app['config']["filesystems.disks.$diskName.root"] = $this->originalPaths['disks']['path'][$diskName];
|
||||
$diskConfig = $this->app['config']['filesystems.disks.' . $diskName];
|
||||
|
||||
// Storage Url
|
||||
$url = $this->originalPaths['disks.url.' . $diskName] ?? null;
|
||||
|
||||
if ($diskConfig['driver'] === 'local' && ! is_null($url)) {
|
||||
$$this->app['config']["filesystems.disks.$diskName.url"] = $url;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
73
src/Commands/Link.php
Normal file
73
src/Commands/Link.php
Normal file
|
|
@ -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.');
|
||||
}
|
||||
}
|
||||
44
src/Concerns/DealsWithTenantSymlinks.php
Normal file
44
src/Concerns/DealsWithTenantSymlinks.php
Normal file
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
@ -10,10 +10,15 @@ use Throwable;
|
|||
|
||||
class SQLiteDatabaseManager implements TenantDatabaseManager
|
||||
{
|
||||
/**
|
||||
* SQLite Database path without ending slash.
|
||||
*/
|
||||
public static string|null $path = null;
|
||||
|
||||
public function createDatabase(TenantWithDatabase $tenant): bool
|
||||
{
|
||||
try {
|
||||
return file_put_contents(database_path($tenant->database()->getName()), '');
|
||||
return (bool) file_put_contents($this->getPath($tenant->database()->getName()), '');
|
||||
} catch (Throwable) {
|
||||
return false;
|
||||
}
|
||||
|
|
@ -22,7 +27,7 @@ class SQLiteDatabaseManager implements TenantDatabaseManager
|
|||
public function deleteDatabase(TenantWithDatabase $tenant): bool
|
||||
{
|
||||
try {
|
||||
return unlink(database_path($tenant->database()->getName()));
|
||||
return unlink($this->getPath($tenant->database()->getName()));
|
||||
} catch (Throwable) {
|
||||
return false;
|
||||
}
|
||||
|
|
@ -30,7 +35,7 @@ class SQLiteDatabaseManager implements TenantDatabaseManager
|
|||
|
||||
public function databaseExists(string $name): bool
|
||||
{
|
||||
return file_exists(database_path($name));
|
||||
return file_exists($this->getPath($name));
|
||||
}
|
||||
|
||||
public function makeConnectionConfig(array $baseConfig, string $databaseName): array
|
||||
|
|
@ -44,4 +49,13 @@ class SQLiteDatabaseManager implements TenantDatabaseManager
|
|||
{
|
||||
//
|
||||
}
|
||||
|
||||
public function getPath(string $name): string
|
||||
{
|
||||
if (static::$path) {
|
||||
return static::$path . DIRECTORY_SEPARATOR . $name;
|
||||
}
|
||||
|
||||
return database_path($name);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
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
|
||||
{
|
||||
}
|
||||
40
src/Jobs/CreateStorageSymlinks.php
Normal file
40
src/Jobs/CreateStorageSymlinks.php
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Stancl\Tenancy\Jobs;
|
||||
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Stancl\Tenancy\Actions\CreateStorageSymlinksAction;
|
||||
use Stancl\Tenancy\Contracts\Tenant;
|
||||
|
||||
class CreateStorageSymlinks implements ShouldQueue
|
||||
{
|
||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||
|
||||
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()
|
||||
{
|
||||
CreateStorageSymlinksAction::handle($this->tenant);
|
||||
}
|
||||
}
|
||||
40
src/Jobs/RemoveStorageSymlinks.php
Normal file
40
src/Jobs/RemoveStorageSymlinks.php
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Stancl\Tenancy\Jobs;
|
||||
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Stancl\Tenancy\Actions\RemoveStorageSymlinksAction;
|
||||
use Stancl\Tenancy\Contracts\Tenant;
|
||||
|
||||
class RemoveStorageSymlinks implements ShouldQueue
|
||||
{
|
||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||
|
||||
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()
|
||||
{
|
||||
RemoveStorageSymlinksAction::handle($this->tenant);
|
||||
}
|
||||
}
|
||||
16
src/Listeners/DeleteTenantStorage.php
Normal file
16
src/Listeners/DeleteTenantStorage.php
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Stancl\Tenancy\Listeners;
|
||||
|
||||
use Illuminate\Support\Facades\File;
|
||||
use Stancl\Tenancy\Events\DeletingTenant;
|
||||
|
||||
class DeleteTenantStorage
|
||||
{
|
||||
public function handle(DeletingTenant $event): void
|
||||
{
|
||||
File::deleteDirectory($event->tenant->run(fn () => storage_path()));
|
||||
}
|
||||
}
|
||||
|
|
@ -78,6 +78,7 @@ class TenancyServiceProvider extends ServiceProvider
|
|||
{
|
||||
$this->commands([
|
||||
Commands\Run::class,
|
||||
Commands\Link::class,
|
||||
Commands\Seed::class,
|
||||
Commands\Install::class,
|
||||
Commands\Migrate::class,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue