From f590e2ce2a798b154757a55eb9ade0b31b7f3bd3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Wed, 21 Aug 2019 20:29:53 +0200 Subject: [PATCH] [1.8.0] Custom id scheme (#108) * Add UniqueIdentifierGenerators * Apply fixes from StyleCI --- assets/config.php | 1 + src/Interfaces/UniqueIdentifierGenerator.php | 15 +++++++++++++++ src/TenancyServiceProvider.php | 4 +++- src/TenantManager.php | 18 ++++++++++++++++-- src/UUIDGenerator.php | 13 +++++++++++++ 5 files changed, 48 insertions(+), 3 deletions(-) create mode 100644 src/Interfaces/UniqueIdentifierGenerator.php create mode 100644 src/UUIDGenerator.php diff --git a/assets/config.php b/assets/config.php index 1904f1dd..0d748ae2 100644 --- a/assets/config.php +++ b/assets/config.php @@ -56,4 +56,5 @@ return [ 'queue_database_creation' => false, 'queue_database_deletion' => false, 'database_name_key' => null, + 'unique_id_generator' => 'Stancl\Tenancy\UUIDGenerator', ]; diff --git a/src/Interfaces/UniqueIdentifierGenerator.php b/src/Interfaces/UniqueIdentifierGenerator.php new file mode 100644 index 00000000..becf19d2 --- /dev/null +++ b/src/Interfaces/UniqueIdentifierGenerator.php @@ -0,0 +1,15 @@ +app->bind(ServerConfigManager::class, $this->app['config']['tenancy.server.manager']); $this->app->singleton(DatabaseManager::class); $this->app->singleton(TenantManager::class, function ($app) { - return new TenantManager($app, $app[StorageDriver::class], $app[DatabaseManager::class]); + return new TenantManager( + $app, $app[StorageDriver::class], $app[DatabaseManager::class], $app[$app['config']['tenancy.unique_id_generator']] + ); }); $this->app->singleton(Migrate::class, function ($app) { diff --git a/src/TenantManager.php b/src/TenantManager.php index 10c7877a..b8909e08 100644 --- a/src/TenantManager.php +++ b/src/TenantManager.php @@ -5,6 +5,7 @@ namespace Stancl\Tenancy; use Stancl\Tenancy\Interfaces\StorageDriver; use Stancl\Tenancy\Traits\BootstrapsTenancy; use Illuminate\Contracts\Foundation\Application; +use Stancl\Tenancy\Interfaces\UniqueIdentifierGenerator; use Stancl\Tenancy\Exceptions\CannotChangeUuidOrDomainException; use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedException; @@ -33,6 +34,13 @@ final class TenantManager */ public $database; + /** + * Unique identifier generator. + * + * @var UniqueIdentifierGenerator + */ + protected $generator; + /** * Current tenant. * @@ -40,11 +48,12 @@ final class TenantManager */ public $tenant = []; - public function __construct(Application $app, StorageDriver $storage, DatabaseManager $database) + public function __construct(Application $app, StorageDriver $storage, DatabaseManager $database, UniqueIdentifierGenerator $generator) { $this->app = $app; $this->storage = $storage; $this->database = $database; + $this->generator = $generator; } public function init(string $domain = null): array @@ -87,7 +96,7 @@ final class TenantManager throw new \Exception("Domain $domain is already occupied by tenant $id."); } - $tenant = $this->storage->createTenant($domain, (string) \Webpatser\Uuid\Uuid::generate(1, $domain)); + $tenant = $this->storage->createTenant($domain, $this->generateUniqueIdentifier($domain, $data)); if ($this->useJson()) { $tenant = $this->jsonDecodeArrayValues($tenant); } @@ -103,6 +112,11 @@ final class TenantManager return $tenant; } + public function generateUniqueIdentifier(string $domain, array $data) + { + return $this->generator->handle($domain, $data); + } + public function delete(string $uuid): bool { return $this->storage->deleteTenant($uuid); diff --git a/src/UUIDGenerator.php b/src/UUIDGenerator.php new file mode 100644 index 00000000..547d0ac5 --- /dev/null +++ b/src/UUIDGenerator.php @@ -0,0 +1,13 @@ +