1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-12 12:54:05 +00:00

[1.8.0] Custom id scheme (#108)

* Add UniqueIdentifierGenerators

* Apply fixes from StyleCI
This commit is contained in:
Samuel Štancl 2019-08-21 20:29:53 +02:00 committed by GitHub
parent 787063ed73
commit f590e2ce2a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 48 additions and 3 deletions

View file

@ -56,4 +56,5 @@ return [
'queue_database_creation' => false,
'queue_database_deletion' => false,
'database_name_key' => null,
'unique_id_generator' => 'Stancl\Tenancy\UUIDGenerator',
];

View file

@ -0,0 +1,15 @@
<?php
namespace Stancl\Tenancy\Interfaces;
interface UniqueIdentifierGenerator
{
/**
* Generate a unique identifier.
*
* @param string $domain
* @param array $data
* @return string
*/
public static function handle(string $domain, array $data = []): string;
}

View file

@ -125,7 +125,9 @@ class TenancyServiceProvider extends ServiceProvider
$this->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) {

View file

@ -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);

13
src/UUIDGenerator.php Normal file
View file

@ -0,0 +1,13 @@
<?php
namespace Stancl\Tenancy;
use Stancl\Tenancy\Interfaces\UniqueIdentifierGenerator;
class UUIDGenerator implements UniqueIdentifierGenerator
{
public static function handle(string $domain, array $data = []): string
{
return (string) \Webpatser\Uuid\Uuid::generate(1, $domain);
}
}