mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-17 06:04:03 +00:00
Merge branch 'master' into pr/793
This commit is contained in:
commit
3408edda04
79 changed files with 3496 additions and 3526 deletions
|
|
@ -56,6 +56,8 @@ class FilesystemTenancyBootstrapper implements TenancyBootstrapper
|
|||
Storage::forgetDisk($this->app['config']['tenancy.filesystem.disks']);
|
||||
|
||||
foreach ($this->app['config']['tenancy.filesystem.disks'] as $disk) {
|
||||
// todo@v4 \League\Flysystem\PathPrefixer is making this a lot more painful in flysystem v2
|
||||
|
||||
$originalRoot = $this->app['config']["filesystems.disks.{$disk}.root"];
|
||||
$this->originalPaths['disks'][$disk] = $originalRoot;
|
||||
|
||||
|
|
@ -67,7 +69,7 @@ class FilesystemTenancyBootstrapper implements TenancyBootstrapper
|
|||
|
||||
if (! $finalPrefix) {
|
||||
$finalPrefix = $originalRoot
|
||||
? rtrim($originalRoot, '/') . '/'. $suffix
|
||||
? rtrim($originalRoot, '/') . '/' . $suffix
|
||||
: $suffix;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -4,18 +4,17 @@ declare(strict_types=1);
|
|||
|
||||
namespace Stancl\Tenancy\Bootstrappers;
|
||||
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Config\Repository;
|
||||
use Illuminate\Queue\QueueManager;
|
||||
use Stancl\Tenancy\Contracts\Tenant;
|
||||
use Illuminate\Contracts\Events\Dispatcher;
|
||||
use Illuminate\Contracts\Foundation\Application;
|
||||
use Illuminate\Queue\Events\JobFailed;
|
||||
use Illuminate\Queue\Events\JobProcessed;
|
||||
use Illuminate\Queue\Events\JobProcessing;
|
||||
use Illuminate\Contracts\Events\Dispatcher;
|
||||
use Illuminate\Queue\Events\JobRetryRequested;
|
||||
use Illuminate\Queue\QueueManager;
|
||||
use Illuminate\Support\Testing\Fakes\QueueFake;
|
||||
use Illuminate\Contracts\Foundation\Application;
|
||||
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
|
||||
use Stancl\Tenancy\Contracts\Tenant;
|
||||
|
||||
class QueueTenancyBootstrapper implements TenancyBootstrapper
|
||||
{
|
||||
|
|
|
|||
|
|
@ -13,7 +13,6 @@ class CacheManager extends BaseCacheManager
|
|||
*
|
||||
* @param string $method
|
||||
* @param array $parameters
|
||||
* @return mixed
|
||||
*/
|
||||
public function __call($method, $parameters)
|
||||
{
|
||||
|
|
@ -21,7 +20,7 @@ class CacheManager extends BaseCacheManager
|
|||
|
||||
if ($method === 'tags') {
|
||||
$count = count($parameters);
|
||||
|
||||
|
||||
if ($count !== 1) {
|
||||
throw new \Exception("Method tags() takes exactly 1 argument. $count passed.");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -24,8 +24,6 @@ class Install extends Command
|
|||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
|
|
|
|||
|
|
@ -33,8 +33,6 @@ class Migrate extends MigrateCommand
|
|||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
|
|
|
|||
|
|
@ -31,8 +31,6 @@ final class MigrateFresh extends Command
|
|||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
|
|
|
|||
|
|
@ -42,8 +42,6 @@ class Rollback extends RollbackCommand
|
|||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
|
|
|
|||
|
|
@ -27,8 +27,6 @@ class Run extends Command
|
|||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
|
|
|
|||
|
|
@ -35,8 +35,6 @@ class Seed extends SeedCommand
|
|||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
|
|
|
|||
53
src/Commands/TenantDump.php
Normal file
53
src/Commands/TenantDump.php
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Stancl\Tenancy\Commands;
|
||||
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Contracts\Events\Dispatcher;
|
||||
use Illuminate\Database\ConnectionResolverInterface;
|
||||
use Illuminate\Database\Console\DumpCommand;
|
||||
use Stancl\Tenancy\Contracts\Tenant;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
|
||||
class TenantDump extends DumpCommand
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
$this->setName('tenants:dump');
|
||||
$this->specifyParameters();
|
||||
}
|
||||
|
||||
public function handle(ConnectionResolverInterface $connections, Dispatcher $dispatcher): int
|
||||
{
|
||||
$this->tenant()->run(fn () => parent::handle($connections, $dispatcher));
|
||||
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
|
||||
public function tenant(): Tenant
|
||||
{
|
||||
$tenant = $this->option('tenant')
|
||||
?? tenant()
|
||||
?? $this->ask('What tenant do you want to dump the schema for?')
|
||||
?? tenancy()->query()->first();
|
||||
|
||||
if (! $tenant instanceof Tenant) {
|
||||
$tenant = tenancy()->find($tenant);
|
||||
}
|
||||
|
||||
throw_if(! $tenant, 'Could not identify the tenant to use for dumping the schema.');
|
||||
|
||||
return $tenant;
|
||||
}
|
||||
|
||||
protected function getOptions(): array
|
||||
{
|
||||
return array_merge([
|
||||
['tenant', null, InputOption::VALUE_OPTIONAL, '', null],
|
||||
], parent::getOptions());
|
||||
}
|
||||
}
|
||||
|
|
@ -25,8 +25,6 @@ class TenantList extends Command
|
|||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
|
|
|
|||
|
|
@ -1,5 +1,7 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Stancl\Tenancy\Concerns;
|
||||
|
||||
trait ExtendsLaravelCommand
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@ trait HasATenantsOption
|
|||
protected function getOptions()
|
||||
{
|
||||
return array_merge([
|
||||
['tenants', null, InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL, '', null],
|
||||
['tenants', null, InputOption::VALUE_IS_ARRAY|InputOption::VALUE_OPTIONAL, '', null],
|
||||
], parent::getOptions());
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -20,18 +20,11 @@ interface TenantDatabaseManager
|
|||
|
||||
/**
|
||||
* Does a database exist.
|
||||
*
|
||||
* @param string $name
|
||||
* @return bool
|
||||
*/
|
||||
public function databaseExists(string $name): bool;
|
||||
|
||||
/**
|
||||
* Make a DB connection config array.
|
||||
*
|
||||
* @param array $baseConfig
|
||||
* @param string $databaseName
|
||||
* @return array
|
||||
*/
|
||||
public function makeConnectionConfig(array $baseConfig, string $databaseName): array;
|
||||
|
||||
|
|
@ -39,9 +32,6 @@ interface TenantDatabaseManager
|
|||
* Set the DB connection that should be used by the tenant database manager.
|
||||
*
|
||||
* @throws NoConnectionSetException
|
||||
*
|
||||
* @param string $connection
|
||||
* @return void
|
||||
*/
|
||||
public function setConnection(string $connection): void;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,9 +11,6 @@ trait TenantRun
|
|||
/**
|
||||
* Run a callback in this tenant's context.
|
||||
* Atomic, safely reverts to previous context.
|
||||
*
|
||||
* @param callable $callback
|
||||
* @return mixed
|
||||
*/
|
||||
public function run(callable $callback)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -22,10 +22,15 @@ class ImpersonationToken extends Model
|
|||
use CentralConnection;
|
||||
|
||||
protected $guarded = [];
|
||||
|
||||
public $timestamps = false;
|
||||
|
||||
protected $primaryKey = 'token';
|
||||
|
||||
public $incrementing = false;
|
||||
|
||||
protected $table = 'tenant_user_impersonation_tokens';
|
||||
|
||||
protected $dates = [
|
||||
'created_at',
|
||||
];
|
||||
|
|
|
|||
|
|
@ -29,7 +29,9 @@ class Tenant extends Model implements Contracts\Tenant
|
|||
Concerns\InvalidatesResolverCache;
|
||||
|
||||
protected $table = 'tenants';
|
||||
|
||||
protected $primaryKey = 'id';
|
||||
|
||||
protected $guarded = [];
|
||||
|
||||
public function getTenantKeyName(): string
|
||||
|
|
|
|||
|
|
@ -80,8 +80,6 @@ class DatabaseConfig
|
|||
|
||||
/**
|
||||
* Generate DB name, username & password and write them to the tenant model.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function makeCredentials(): void
|
||||
{
|
||||
|
|
@ -113,7 +111,8 @@ class DatabaseConfig
|
|||
$templateConnection = config("database.connections.{$template}");
|
||||
|
||||
return $this->manager()->makeConnectionConfig(
|
||||
array_merge($templateConnection, $this->tenantConfig()), $this->getName()
|
||||
array_merge($templateConnection, $this->tenantConfig()),
|
||||
$this->getName()
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -23,11 +23,17 @@ class UniversalRoutes implements Feature
|
|||
public function bootstrap(Tenancy $tenancy): void
|
||||
{
|
||||
foreach (static::$identificationMiddlewares as $middleware) {
|
||||
$middleware::$onFail = function ($exception, $request, $next) {
|
||||
$originalOnFail = $middleware::$onFail;
|
||||
|
||||
$middleware::$onFail = function ($exception, $request, $next) use ($originalOnFail) {
|
||||
if (static::routeHasMiddleware($request->route(), static::$middlewareGroup)) {
|
||||
return $next($request);
|
||||
}
|
||||
|
||||
if ($originalOnFail) {
|
||||
return $originalOnFail($exception, $request, $next);
|
||||
}
|
||||
|
||||
throw $exception;
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -33,7 +33,6 @@ class UserImpersonation implements Feature
|
|||
*
|
||||
* @param string|ImpersonationToken $token
|
||||
* @param int $ttl
|
||||
* @return RedirectResponse
|
||||
*/
|
||||
public static function makeResponse($token, int $ttl = null): RedirectResponse
|
||||
{
|
||||
|
|
|
|||
29
src/Jobs/DeleteDomains.php
Normal file
29
src/Jobs/DeleteDomains.php
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Stancl\Tenancy\Jobs;
|
||||
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Stancl\Tenancy\Contracts\TenantWithDatabase;
|
||||
|
||||
class DeleteDomains
|
||||
{
|
||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||
|
||||
/** @var TenantWithDatabase */
|
||||
protected $tenant;
|
||||
|
||||
public function __construct(TenantWithDatabase $tenant)
|
||||
{
|
||||
$this->tenant = $tenant;
|
||||
}
|
||||
|
||||
public function handle()
|
||||
{
|
||||
$this->tenant->domains->each->delete();
|
||||
}
|
||||
}
|
||||
|
|
@ -48,8 +48,7 @@ class UpdateSyncedResource extends QueueableListener
|
|||
protected function updateResourceInCentralDatabaseAndGetTenants($event, $syncedAttributes)
|
||||
{
|
||||
/** @var Model|SyncMaster $centralModel */
|
||||
$centralModel = $event->model->getCentralModelName()
|
||||
::where($event->model->getGlobalIdentifierKeyName(), $event->model->getGlobalIdentifierKey())
|
||||
$centralModel = $event->model->getCentralModelName()::where($event->model->getGlobalIdentifierKeyName(), $event->model->getGlobalIdentifierKey())
|
||||
->first();
|
||||
|
||||
// We disable events for this call, to avoid triggering this event & listener again.
|
||||
|
|
|
|||
|
|
@ -5,10 +5,10 @@ declare(strict_types=1);
|
|||
namespace Stancl\Tenancy\Middleware;
|
||||
|
||||
use Closure;
|
||||
use Symfony\Component\HttpKernel\Exception\HttpException;
|
||||
use Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode;
|
||||
use Stancl\Tenancy\Exceptions\TenancyNotInitializedException;
|
||||
use Symfony\Component\HttpFoundation\IpUtils;
|
||||
use Symfony\Component\HttpKernel\Exception\HttpException;
|
||||
|
||||
class CheckTenantForMaintenanceMode extends CheckForMaintenanceMode
|
||||
{
|
||||
|
|
|
|||
|
|
@ -29,13 +29,13 @@ class InitializeTenancyByDomain extends IdentificationMiddleware
|
|||
* Handle an incoming request.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param \Closure $next
|
||||
* @return mixed
|
||||
*/
|
||||
public function handle($request, Closure $next)
|
||||
{
|
||||
return $this->initializeTenancy(
|
||||
$request, $next, $request->getHost()
|
||||
$request,
|
||||
$next,
|
||||
$request->getHost()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,8 +13,6 @@ class InitializeTenancyByDomainOrSubdomain
|
|||
* Handle an incoming request.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param \Closure $next
|
||||
* @return mixed
|
||||
*/
|
||||
public function handle($request, Closure $next)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -38,7 +38,9 @@ class InitializeTenancyByPath extends IdentificationMiddleware
|
|||
// simply injected into some route controller action.
|
||||
if ($route->parameterNames()[0] === PathTenantResolver::$tenantParameterName) {
|
||||
return $this->initializeTenancy(
|
||||
$request, $next, $route
|
||||
$request,
|
||||
$next,
|
||||
$route
|
||||
);
|
||||
} else {
|
||||
throw new RouteIsMissingTenantParameterException;
|
||||
|
|
|
|||
|
|
@ -36,8 +36,6 @@ class InitializeTenancyByRequestData extends IdentificationMiddleware
|
|||
* Handle an incoming request.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param \Closure $next
|
||||
* @return mixed
|
||||
*/
|
||||
public function handle($request, Closure $next)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -28,8 +28,6 @@ class InitializeTenancyBySubdomain extends InitializeTenancyByDomain
|
|||
* Handle an incoming request.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param \Closure $next
|
||||
* @return mixed
|
||||
*/
|
||||
public function handle($request, Closure $next)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -75,7 +75,6 @@ abstract class CachedTenantResolver implements TenantResolver
|
|||
/**
|
||||
* Get all the arg combinations for resolve() that can be used to find this tenant.
|
||||
*
|
||||
* @param Tenant $tenant
|
||||
* @return array[]
|
||||
*/
|
||||
abstract public function getArgsForTenant(Tenant $tenant): array;
|
||||
|
|
|
|||
|
|
@ -27,7 +27,6 @@ class Tenancy
|
|||
/**
|
||||
* Initializes the tenant.
|
||||
* @param Tenant|int|string $tenant
|
||||
* @return void
|
||||
*/
|
||||
public function initialize($tenant): void
|
||||
{
|
||||
|
|
@ -106,9 +105,6 @@ class Tenancy
|
|||
/**
|
||||
* Run a callback in the central context.
|
||||
* Atomic, safely reverts to previous context.
|
||||
*
|
||||
* @param callable $callback
|
||||
* @return mixed
|
||||
*/
|
||||
public function central(callable $callback)
|
||||
{
|
||||
|
|
@ -132,7 +128,6 @@ class Tenancy
|
|||
* More performant than running $tenant->run() one by one.
|
||||
*
|
||||
* @param Tenant[]|\Traversable|string[]|null $tenants
|
||||
* @param callable $callback
|
||||
* @return void
|
||||
*/
|
||||
public function runForMultiple($tenants, callable $callback)
|
||||
|
|
|
|||
|
|
@ -15,8 +15,6 @@ class TenancyServiceProvider extends ServiceProvider
|
|||
{
|
||||
/**
|
||||
* Register services.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register(): void
|
||||
{
|
||||
|
|
@ -76,8 +74,6 @@ class TenancyServiceProvider extends ServiceProvider
|
|||
|
||||
/**
|
||||
* Bootstrap services.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function boot(): void
|
||||
{
|
||||
|
|
@ -88,6 +84,7 @@ class TenancyServiceProvider extends ServiceProvider
|
|||
Commands\Migrate::class,
|
||||
Commands\Rollback::class,
|
||||
Commands\TenantList::class,
|
||||
Commands\TenantDump::class,
|
||||
Commands\MigrateFresh::class,
|
||||
]);
|
||||
|
||||
|
|
|
|||
57
src/TenantDatabaseManagers/MicrosoftSQLDatabaseManager.php
Normal file
57
src/TenantDatabaseManagers/MicrosoftSQLDatabaseManager.php
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Stancl\Tenancy\TenantDatabaseManagers;
|
||||
|
||||
use Illuminate\Database\Connection;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Stancl\Tenancy\Contracts\TenantDatabaseManager;
|
||||
use Stancl\Tenancy\Contracts\TenantWithDatabase;
|
||||
use Stancl\Tenancy\Exceptions\NoConnectionSetException;
|
||||
|
||||
class MicrosoftSQLDatabaseManager implements TenantDatabaseManager
|
||||
{
|
||||
/** @var string */
|
||||
protected $connection;
|
||||
|
||||
protected function database(): Connection
|
||||
{
|
||||
if ($this->connection === null) {
|
||||
throw new NoConnectionSetException(static::class);
|
||||
}
|
||||
|
||||
return DB::connection($this->connection);
|
||||
}
|
||||
|
||||
public function setConnection(string $connection): void
|
||||
{
|
||||
$this->connection = $connection;
|
||||
}
|
||||
|
||||
public function createDatabase(TenantWithDatabase $tenant): bool
|
||||
{
|
||||
$database = $tenant->database()->getName();
|
||||
$charset = $this->database()->getConfig('charset');
|
||||
$collation = $this->database()->getConfig('collation');
|
||||
|
||||
return $this->database()->statement("CREATE DATABASE [{$database}]");
|
||||
}
|
||||
|
||||
public function deleteDatabase(TenantWithDatabase $tenant): bool
|
||||
{
|
||||
return $this->database()->statement("DROP DATABASE [{$tenant->database()->getName()}]");
|
||||
}
|
||||
|
||||
public function databaseExists(string $name): bool
|
||||
{
|
||||
return (bool) $this->database()->select("SELECT name FROM master.sys.databases WHERE name = '$name'");
|
||||
}
|
||||
|
||||
public function makeConnectionConfig(array $baseConfig, string $databaseName): array
|
||||
{
|
||||
$baseConfig['database'] = $databaseName;
|
||||
|
||||
return $baseConfig;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue