mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 17:44:04 +00:00
[1.8.0] Custom id scheme (#108)
* Add UniqueIdentifierGenerators * Apply fixes from StyleCI
This commit is contained in:
parent
787063ed73
commit
f590e2ce2a
5 changed files with 48 additions and 3 deletions
15
src/Interfaces/UniqueIdentifierGenerator.php
Normal file
15
src/Interfaces/UniqueIdentifierGenerator.php
Normal 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;
|
||||
}
|
||||
|
|
@ -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) {
|
||||
|
|
|
|||
|
|
@ -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
13
src/UUIDGenerator.php
Normal 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);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue