1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-02-05 15:34:04 +00:00
This commit is contained in:
Samuel Štancl 2019-08-15 19:57:55 +02:00
commit 8c74cb4d76
21 changed files with 850 additions and 82 deletions

76
src/Commands/Install.php Normal file
View file

@ -0,0 +1,76 @@
<?php
namespace Stancl\Tenancy\Commands;
use Illuminate\Console\Command;
class Install extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'tenancy:install';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Install stancl/tenancy.';
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$this->comment('Installing stancl/tenancy...');
$this->callSilent('vendor:publish', [
'--provider' => 'Stancl\Tenancy\TenancyServiceProvider',
'--tag' => 'config',
]);
$this->info('✔️ Created config/tenancy.php');
file_put_contents(app_path('Http/Kernel.php'), str_replace(
'protected $middlewarePriority = [',
"protected \$middlewarePriority = [\n \Stancl\Tenancy\Middleware\InitializeTenancy::class,",
file_get_contents(app_path('Http/Kernel.php'))
));
$this->info('✔️ Set middleware priority');
file_put_contents(base_path('routes/tenant.php'),
"<?php
/*
|--------------------------------------------------------------------------
| Tenant Routes
|--------------------------------------------------------------------------
|
| Here is where you can register tenant routes for your application. These
| routes are loaded by the TenantRouteServiceProvider within a group
| which contains the \"InitializeTenancy\" middleware. Good luck!
|
*/
Route::get('/your/application/homepage', function () {
return 'This is your multi-tenant application. The uuid of the current tenant is ' . tenant('uuid');
});
");
$this->info('✔️ Created routes/tenant.php');
$this->line('');
$this->line("This package lets you store data about tenants either in Redis or in a relational database like MySQL. If you're going to use the database storage, you need to create a tenants table.");
if ($this->confirm('Do you want to publish the default database migration?', true)) {
$this->callSilent('vendor:publish', [
'--provider' => 'Stancl\Tenancy\TenancyServiceProvider',
'--tag' => 'migrations',
]);
$this->info('✔️ Created migration.');
}
$this->comment('✨️ stancl/tenancy installed successfully.');
}
}

66
src/Commands/Run.php Normal file
View file

@ -0,0 +1,66 @@
<?php
namespace Stancl\Tenancy\Commands;
use Illuminate\Console\Command;
class Run extends Command
{
/**
* The console command description.
*
* @var string
*/
protected $description = 'Run a command for tenant(s)';
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = "tenants:run {commandname : The command's name.}
{--tenants= : The tenant(s) to run the command for. Default: all}
{--argument=* : The arguments to pass to the command. Default: none}
{--option=* : The options to pass to the command. Default: none}";
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
if ($tenancy_was_initialized = tenancy()->initialized) {
$previous_tenants_domain = tenant('domain');
}
tenant()->all($this->option('tenants'))->each(function ($tenant) {
$this->line("Tenant: {$tenant['uuid']} ({$tenant['domain']})");
tenancy()->init($tenant['domain']);
$callback = function ($prefix = '') {
return function ($arguments, $argument) use ($prefix) {
[$key, $value] = explode('=', $argument, 2);
$arguments[$prefix . $key] = $value;
return $arguments;
};
};
// Turns ['foo=bar', 'abc=xyz=zzz'] into ['foo' => 'bar', 'abc' => 'xyz=zzz']
$arguments = array_reduce($this->option('argument'), $callback(), []);
// Turns ['foo=bar', 'abc=xyz=zzz'] into ['--foo' => 'bar', '--abc' => 'xyz=zzz']
$options = array_reduce($this->option('option'), $callback('--'), []);
// Run command
$this->call($this->argument('commandname'), array_merge($arguments, $options));
tenancy()->end();
});
if ($tenancy_was_initialized) {
tenancy()->init($previous_tenants_domain);
}
}
}

View file

@ -20,16 +20,6 @@ class TenantList extends Command
*/
protected $description = 'List tenants.';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*

View file

@ -6,7 +6,7 @@ use Stancl\Tenancy\Jobs\QueuedTenantDatabaseCreator;
use Stancl\Tenancy\Jobs\QueuedTenantDatabaseDeleter;
use Illuminate\Database\DatabaseManager as BaseDatabaseManager;
class DatabaseManager
final class DatabaseManager
{
public $originalDefaultConnection;
@ -19,8 +19,7 @@ class DatabaseManager
public function connect(string $database)
{
$this->createTenantConnection($database);
$this->database->setDefaultConnection('tenant');
$this->database->reconnect('tenant');
$this->useConnection('tenant');
}
public function connectToTenant($tenant)
@ -105,4 +104,10 @@ class DatabaseManager
$database_name = $this->getDriver() === 'sqlite' ? database_path($database_name) : $database_name;
config()->set(['database.connections.tenant.database' => $database_name]);
}
public function useConnection(string $connection)
{
$this->database->setDefaultConnection($connection);
$this->database->reconnect($connection);
}
}

View file

@ -0,0 +1,8 @@
<?php
namespace Stancl\Tenancy\Exceptions;
class CannotChangeUuidOrDomainException extends \Exception
{
protected $message = 'Uuid and domain cannot be changed.';
}

View file

@ -9,7 +9,7 @@ class InitializeTenancy
{
public function __construct(Closure $onFail = null)
{
$this->onFail = $onFail ?: function ($e) {
$this->onFail = $onFail ?? function ($e) {
throw $e;
};
}

View file

@ -2,8 +2,10 @@
namespace Stancl\Tenancy;
use Stancl\Tenancy\Commands\Run;
use Stancl\Tenancy\Commands\Seed;
use Illuminate\Cache\CacheManager;
use Stancl\Tenancy\Commands\Install;
use Stancl\Tenancy\Commands\Migrate;
use Illuminate\Support\Facades\Route;
use Stancl\Tenancy\Commands\Rollback;
@ -21,14 +23,14 @@ class TenancyServiceProvider extends ServiceProvider
*/
public function boot()
{
if ($this->app->runningInConsole()) {
$this->commands([
Migrate::class,
Rollback::class,
Seed::class,
TenantList::class,
]);
}
$this->commands([
Run::class,
Seed::class,
Install::class,
Migrate::class,
Rollback::class,
TenantList::class,
]);
$this->publishes([
__DIR__ . '/config/tenancy.php' => config_path('tenancy.php'),

View file

@ -5,8 +5,9 @@ namespace Stancl\Tenancy;
use Stancl\Tenancy\Interfaces\StorageDriver;
use Stancl\Tenancy\Traits\BootstrapsTenancy;
use Illuminate\Contracts\Foundation\Application;
use Stancl\Tenancy\Exceptions\CannotChangeUuidOrDomainException;
class TenantManager
final class TenantManager
{
use BootstrapsTenancy;
@ -29,7 +30,7 @@ class TenantManager
*
* @var DatabaseManager
*/
protected $database;
public $database;
/**
* Current tenant.
@ -70,7 +71,14 @@ class TenantManager
return $tenant;
}
public function create(string $domain = null): array
/**
* Create a tenant.
*
* @param string $domain
* @param array $data
* @return array
*/
public function create(string $domain = null, array $data = []): array
{
$domain = $domain ?: $this->currentDomain();
@ -79,6 +87,13 @@ class TenantManager
}
$tenant = $this->jsonDecodeArrayValues($this->storage->createTenant($domain, (string) \Webpatser\Uuid\Uuid::generate(1, $domain)));
if ($data) {
$this->put($data, null, $tenant['uuid']);
$tenant = array_merge($tenant, $data);
}
$this->database->create($this->getDatabaseName($tenant));
return $tenant;
@ -168,6 +183,12 @@ class TenantManager
{
$tenant = $tenant ?: $this->tenant;
if ($key = $this->app['config']['tenancy.database_name_key']) {
if (isset($tenant[$key])) {
return $tenant[$key];
}
}
return $this->app['config']['tenancy.database.prefix'] . $tenant['uuid'] . $this->app['config']['tenancy.database.suffix'];
}
@ -254,6 +275,15 @@ class TenantManager
*/
public function put($key, $value = null, string $uuid = null)
{
if (in_array($key, ['uuid', 'domain'], true) || (
is_array($key) && (
in_array('uuid', array_keys($key), true) ||
in_array('domain', array_keys($key), true)
)
)) {
throw new CannotChangeUuidOrDomainException;
}
if (\is_null($uuid)) {
if (! isset($this->tenant['uuid'])) {
throw new \Exception('No UUID supplied (and no tenant is currently identified).');

View file

@ -9,6 +9,8 @@ use Stancl\Tenancy\Exceptions\PhpRedisNotInstalledException;
trait BootstrapsTenancy
{
use TenantManagerEvents;
public $originalSettings = [];
/**
* Was tenancy initialized/bootstrapped?
@ -19,26 +21,55 @@ trait BootstrapsTenancy
public function bootstrap()
{
$prevented = $this->event('bootstrapping');
$this->initialized = true;
$this->switchDatabaseConnection();
if ($this->app['config']['tenancy.redis.tenancy']) {
$this->setPhpRedisPrefix($this->app['config']['tenancy.redis.prefixed_connections']);
if (! $prevented->contains('database')) {
$this->switchDatabaseConnection();
}
$this->tagCache();
$this->suffixFilesystemRootPaths();
if (! $prevented->contains('redis')) {
if ($this->app['config']['tenancy.redis.tenancy']) {
$this->setPhpRedisPrefix($this->app['config']['tenancy.redis.prefixed_connections']);
}
}
if (! $prevented->contains('cache')) {
$this->tagCache();
}
if (! $prevented->contains('filesystem')) {
$this->suffixFilesystemRootPaths();
}
$this->event('bootstrapped');
}
public function end()
{
$prevented = $this->event('ending');
$this->initialized = false;
$this->disconnectDatabase();
if ($this->app['config']['tenancy.redis.tenancy']) {
$this->resetPhpRedisPrefix($this->app['config']['tenancy.redis.prefixed_connections']);
if (! $prevented->contains('database')) {
$this->disconnectDatabase();
}
$this->untagCache();
$this->resetFileSystemRootPaths();
if (! $prevented->contains('redis')) {
if ($this->app['config']['tenancy.redis.tenancy']) {
$this->resetPhpRedisPrefix($this->app['config']['tenancy.redis.prefixed_connections']);
}
}
if (! $prevented->contains('cache')) {
$this->untagCache();
}
if (! $prevented->contains('filesystem')) {
$this->resetFileSystemRootPaths();
}
$this->event('ended');
}
public function switchDatabaseConnection()

View file

@ -0,0 +1,85 @@
<?php
namespace Stancl\Tenancy\Traits;
use Illuminate\Support\Collection;
trait TenantManagerEvents
{
/**
* Event listeners.
*
* @var callable[][]
*/
protected $listeners = [
'bootstrapping' => [],
'bootstrapped' => [],
'ending' => [],
'ended' => [],
];
/**
* Register a listener that will be executed before tenancy is bootstrapped.
*
* @param callable $callback
* @return self
*/
public function bootstrapping(callable $callback)
{
$this->listeners['bootstrapping'][] = $callback;
return $this;
}
/**
* Register a listener that will be executed after tenancy is bootstrapped.
*
* @param callable $callback
* @return self
*/
public function bootstrapped(callable $callback)
{
$this->listeners['bootstrapped'][] = $callback;
return $this;
}
/**
* Register a listener that will be executed before tenancy is ended.
*
* @param callable $callback
* @return self
*/
public function ending(callable $callback)
{
$this->listeners['ending'][] = $callback;
return $this;
}
/**
* Register a listener that will be executed after tenancy is ended.
*
* @param callable $callback
* @return self
*/
public function ended(callable $callback)
{
$this->listeners['ended'][] = $callback;
return $this;
}
/**
* Fire an event.
*
* @param string $name Event name
* @return Collection Prevented events
*/
public function event(string $name): Collection
{
return array_reduce($this->listeners[$name], function ($prevents, $listener) {
return $prevents->merge($listener($this) ?? []);
}, collect([]));
}
}

View file

@ -43,4 +43,5 @@ return [
],
'queue_database_creation' => false,
'queue_database_deletion' => false,
'database_name_key' => null,
];