mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 19:14:04 +00:00
Merge branch '1.x' of github.com:stancl/tenancy into 1.x
This commit is contained in:
commit
60bfbce97b
26 changed files with 87 additions and 68 deletions
4
.styleci.yml
Normal file
4
.styleci.yml
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
preset: laravel
|
||||
disabled:
|
||||
- concat_without_spaces
|
||||
- ternary_operator_spaces
|
||||
|
|
@ -10,7 +10,7 @@ class CacheManager extends BaseCacheManager
|
|||
{
|
||||
$tags = [config('tenancy.cache.tag_base') . tenant('uuid')];
|
||||
|
||||
if ($method === "tags") {
|
||||
if ($method === 'tags') {
|
||||
if (\count($parameters) !== 1) {
|
||||
throw new \Exception("Method tags() takes exactly 1 argument. {count($parameters)} passed.");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -37,7 +37,7 @@ class TenantList extends Command
|
|||
*/
|
||||
public function handle()
|
||||
{
|
||||
$this->info("Listing all tenants.");
|
||||
$this->info('Listing all tenants.');
|
||||
tenancy()->all()->each(function ($tenant) {
|
||||
$this->line("[Tenant] uuid: {$tenant['uuid']} @ {$tenant['domain']}");
|
||||
});
|
||||
|
|
|
|||
|
|
@ -90,7 +90,7 @@ class DatabaseManager
|
|||
|
||||
public function getDriver(): ?string
|
||||
{
|
||||
return config("database.connections.tenant.driver");
|
||||
return config('database.connections.tenant.driver');
|
||||
}
|
||||
|
||||
public function createTenantConnection(string $database_name)
|
||||
|
|
@ -98,11 +98,11 @@ class DatabaseManager
|
|||
// Create the `tenancy` database connection.
|
||||
$based_on = config('tenancy.database.based_on') ?: config('database.default');
|
||||
config()->set([
|
||||
'database.connections.tenant' => config('database.connections.' . $based_on)
|
||||
'database.connections.tenant' => config('database.connections.' . $based_on),
|
||||
]);
|
||||
|
||||
// Change DB name
|
||||
$database_name = $this->getDriver() === "sqlite" ? database_path($database_name) : $database_name;
|
||||
$database_name = $this->getDriver() === 'sqlite' ? database_path($database_name) : $database_name;
|
||||
config()->set(['database.connections.tenant.database' => $database_name]);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,13 +5,22 @@ namespace Stancl\Tenancy\Interfaces;
|
|||
interface StorageDriver
|
||||
{
|
||||
public function identifyTenant(string $domain): array;
|
||||
|
||||
public function getAllTenants(array $uuids = []): array;
|
||||
|
||||
public function getTenantById(string $uuid, array $fields = []): array;
|
||||
|
||||
public function getTenantIdByDomain(string $domain): ?string;
|
||||
|
||||
public function createTenant(string $domain, string $uuid): array;
|
||||
|
||||
public function deleteTenant(string $uuid): bool;
|
||||
|
||||
public function get(string $uuid, string $key);
|
||||
|
||||
public function getMany(string $uuid, array $keys): array;
|
||||
|
||||
public function put(string $uuid, string $key, $value);
|
||||
|
||||
public function putMany(string $uuid, array $values): array;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ interface TenantDatabaseManager
|
|||
* Create a database.
|
||||
*
|
||||
* @param string $name Name of the database.
|
||||
* @return boolean
|
||||
* @return bool
|
||||
*/
|
||||
public function createDatabase(string $name): bool;
|
||||
|
||||
|
|
@ -16,7 +16,7 @@ interface TenantDatabaseManager
|
|||
* Delete a database.
|
||||
*
|
||||
* @param string $name Name of the database.
|
||||
* @return boolean
|
||||
* @return bool
|
||||
*/
|
||||
public function deleteDatabase(string $name): bool;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,8 +5,8 @@ namespace Stancl\Tenancy\Jobs;
|
|||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
use Stancl\Tenancy\Interfaces\TenantDatabaseManager;
|
||||
|
||||
class QueuedTenantDatabaseCreator implements ShouldQueue
|
||||
|
|
|
|||
|
|
@ -5,8 +5,8 @@ namespace Stancl\Tenancy\Jobs;
|
|||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
use Stancl\Tenancy\Interfaces\TenantDatabaseManager;
|
||||
|
||||
class QueuedTenantDatabaseDeleter implements ShouldQueue
|
||||
|
|
|
|||
|
|
@ -20,6 +20,7 @@ class RedisStorageDriver implements StorageDriver
|
|||
if (! $id) {
|
||||
throw new \Exception("Tenant could not be identified on domain {$domain}");
|
||||
}
|
||||
|
||||
return $this->getTenantById($id);
|
||||
}
|
||||
|
||||
|
|
@ -50,14 +51,15 @@ class RedisStorageDriver implements StorageDriver
|
|||
{
|
||||
$this->redis->hmset("domains:$domain", 'tenant_id', $uuid);
|
||||
$this->redis->hmset("tenants:$uuid", 'uuid', json_encode($uuid), 'domain', json_encode($domain));
|
||||
|
||||
return $this->redis->hgetall("tenants:$uuid");
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @param string $id
|
||||
* @return boolean
|
||||
* @return bool
|
||||
* @todo Make tenant & domain deletion atomic.
|
||||
*/
|
||||
public function deleteTenant(string $id): bool
|
||||
|
|
@ -69,6 +71,7 @@ class RedisStorageDriver implements StorageDriver
|
|||
}
|
||||
|
||||
$this->redis->del("domains:$domain");
|
||||
|
||||
return (bool) $this->redis->del("tenants:$id");
|
||||
}
|
||||
|
||||
|
|
@ -110,12 +113,14 @@ class RedisStorageDriver implements StorageDriver
|
|||
public function put(string $uuid, string $key, $value)
|
||||
{
|
||||
$this->redis->hset("tenants:$uuid", $key, $value);
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
public function putMany(string $uuid, array $values): array
|
||||
{
|
||||
$this->redis->hmset("tenants:$uuid", $values);
|
||||
|
||||
return $values;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,16 +3,14 @@
|
|||
namespace Stancl\Tenancy;
|
||||
|
||||
use Stancl\Tenancy\Commands\Seed;
|
||||
use Stancl\Tenancy\TenantManager;
|
||||
use Stancl\Tenancy\DatabaseManager;
|
||||
use Illuminate\Cache\CacheManager;
|
||||
use Stancl\Tenancy\Commands\Migrate;
|
||||
use Stancl\Tenancy\Commands\Rollback;
|
||||
use Illuminate\Support\Facades\Route;
|
||||
use Stancl\Tenancy\Commands\Rollback;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use Stancl\Tenancy\Commands\TenantList;
|
||||
use Stancl\Tenancy\Interfaces\StorageDriver;
|
||||
use Stancl\Tenancy\Interfaces\ServerConfigManager;
|
||||
use Illuminate\Cache\CacheManager;
|
||||
|
||||
class TenancyServiceProvider extends ServiceProvider
|
||||
{
|
||||
|
|
@ -39,7 +37,7 @@ class TenancyServiceProvider extends ServiceProvider
|
|||
$this->loadRoutesFrom(__DIR__ . '/routes.php');
|
||||
|
||||
Route::middlewareGroup('tenancy', [
|
||||
\Stancl\Tenancy\Middleware\InitializeTenancy::class
|
||||
\Stancl\Tenancy\Middleware\InitializeTenancy::class,
|
||||
]);
|
||||
|
||||
$this->app->register(TenantRouteServiceProvider::class);
|
||||
|
|
|
|||
|
|
@ -49,6 +49,7 @@ class TenantManager
|
|||
{
|
||||
$this->setTenant($this->identify($domain));
|
||||
$this->bootstrap();
|
||||
|
||||
return $this->tenant;
|
||||
}
|
||||
|
||||
|
|
@ -57,7 +58,7 @@ class TenantManager
|
|||
$domain = $domain ?: $this->currentDomain();
|
||||
|
||||
if (! $domain) {
|
||||
throw new \Exception("No domain supplied nor detected.");
|
||||
throw new \Exception('No domain supplied nor detected.');
|
||||
}
|
||||
|
||||
$tenant = $this->storage->identifyTenant($domain);
|
||||
|
|
@ -98,6 +99,7 @@ class TenantManager
|
|||
public function getTenantById(string $uuid, $fields = [])
|
||||
{
|
||||
$fields = (array) $fields;
|
||||
|
||||
return $this->jsonDecodeArrayValues($this->storage->getTenantById($uuid, $fields));
|
||||
}
|
||||
|
||||
|
|
@ -165,6 +167,7 @@ class TenantManager
|
|||
public function getDatabaseName($tenant = []): string
|
||||
{
|
||||
$tenant = $tenant ?: $this->tenant;
|
||||
|
||||
return $this->app['config']['tenancy.database.prefix'] . $tenant['uuid'] . $this->app['config']['tenancy.database.suffix'];
|
||||
}
|
||||
|
||||
|
|
@ -219,6 +222,7 @@ class TenantManager
|
|||
{
|
||||
$this->setTenant($this->storage->getTenantById($uuid));
|
||||
$this->bootstrap();
|
||||
|
||||
return $this->tenant;
|
||||
}
|
||||
|
||||
|
|
@ -252,7 +256,7 @@ class TenantManager
|
|||
{
|
||||
if (\is_null($uuid)) {
|
||||
if (! isset($this->tenant['uuid'])) {
|
||||
throw new \Exception("No UUID supplied (and no tenant is currently identified).");
|
||||
throw new \Exception('No UUID supplied (and no tenant is currently identified).');
|
||||
}
|
||||
|
||||
$uuid = $this->tenant['uuid'];
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ trait BootstrapsTenancy
|
|||
/**
|
||||
* Was tenancy initialized/bootstrapped?
|
||||
*
|
||||
* @var boolean
|
||||
* @var bool
|
||||
*/
|
||||
public $initialized = false;
|
||||
|
||||
|
|
|
|||
|
|
@ -9,7 +9,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());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -74,14 +74,14 @@ class BootstrapsTenancyTest extends TestCase
|
|||
$this->initTenancy();
|
||||
|
||||
$new_storage_path = storage_path();
|
||||
$this->assertEquals($old_storage_path . "/" . config('tenancy.filesystem.suffix_base') . tenant('uuid'), $new_storage_path);
|
||||
$this->assertEquals($old_storage_path . '/' . config('tenancy.filesystem.suffix_base') . tenant('uuid'), $new_storage_path);
|
||||
|
||||
foreach (config('tenancy.filesystem.disks') as $disk) {
|
||||
$suffix = config('tenancy.filesystem.suffix_base') . tenant('uuid');
|
||||
$current_path_prefix = \Storage::disk($disk)->getAdapter()->getPathPrefix();
|
||||
|
||||
if ($override = config("tenancy.filesystem.root_override.{$disk}")) {
|
||||
$correct_path_prefix = str_replace("%storage_path%", storage_path(), $override);
|
||||
$correct_path_prefix = str_replace('%storage_path%', storage_path(), $override);
|
||||
} else {
|
||||
if ($base = $old_storage_facade_roots[$disk]) {
|
||||
$correct_path_prefix = $base . "/$suffix/";
|
||||
|
|
|
|||
|
|
@ -2,9 +2,9 @@
|
|||
|
||||
namespace Stancl\Tenancy\Tests;
|
||||
|
||||
use Illuminate\Support\Facades\Artisan;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Illuminate\Support\Facades\Artisan;
|
||||
use Stancl\Tenancy\Tests\Etc\ExampleSeeder;
|
||||
|
||||
class CommandsTest extends TestCase
|
||||
|
|
@ -47,7 +47,7 @@ class CommandsTest extends TestCase
|
|||
{
|
||||
$tenant = tenant()->create('test.localhost');
|
||||
Artisan::call('tenants:migrate', [
|
||||
'--tenants' => [$tenant['uuid']]
|
||||
'--tenants' => [$tenant['uuid']],
|
||||
]);
|
||||
|
||||
$this->assertFalse(Schema::hasTable('users'));
|
||||
|
|
|
|||
|
|
@ -2,11 +2,11 @@
|
|||
|
||||
namespace Stancl\Tenancy\Tests;
|
||||
|
||||
use Illuminate\Support\Facades\Redis;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\Redis;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
class DataSeparationTest extends TestCase
|
||||
{
|
||||
|
|
@ -19,7 +19,7 @@ class DataSeparationTest extends TestCase
|
|||
$tenant1 = tenancy()->create('tenant1.localhost');
|
||||
$tenant2 = tenancy()->create('tenant2.localhost');
|
||||
\Artisan::call('tenants:migrate', [
|
||||
'--tenants' => [$tenant1['uuid'], $tenant2['uuid']]
|
||||
'--tenants' => [$tenant1['uuid'], $tenant2['uuid']],
|
||||
]);
|
||||
|
||||
tenancy()->init('tenant1.localhost');
|
||||
|
|
@ -52,7 +52,7 @@ class DataSeparationTest extends TestCase
|
|||
|
||||
$tenant3 = tenancy()->create('tenant3.localhost');
|
||||
\Artisan::call('tenants:migrate', [
|
||||
'--tenants' => [$tenant1['uuid'], $tenant3['uuid']]
|
||||
'--tenants' => [$tenant1['uuid'], $tenant3['uuid']],
|
||||
]);
|
||||
|
||||
tenancy()->init('tenant3.localhost');
|
||||
|
|
|
|||
|
|
@ -27,7 +27,7 @@ class ReidentificationTest extends TestCase
|
|||
$current_path_prefix = \Storage::disk($disk)->getAdapter()->getPathPrefix();
|
||||
|
||||
if ($override = config("tenancy.filesystem.root_override.{$disk}")) {
|
||||
$correct_path_prefix = str_replace("%storage_path%", storage_path(), $override);
|
||||
$correct_path_prefix = str_replace('%storage_path%', storage_path(), $override);
|
||||
} else {
|
||||
if ($base = $originals[$disk]) {
|
||||
$correct_path_prefix = $base . "/$suffix/";
|
||||
|
|
@ -49,7 +49,6 @@ class ReidentificationTest extends TestCase
|
|||
tenant()->create('second.localhost');
|
||||
tenancy()->init('second.localhost');
|
||||
|
||||
|
||||
$suffix = config('tenancy.filesystem.suffix_base') . tenant('uuid');
|
||||
$this->assertSame($original . "/$suffix", storage_path());
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase
|
|||
}
|
||||
|
||||
/**
|
||||
* Setup the test environment
|
||||
* Setup the test environment.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue