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

Add docblocks

This commit is contained in:
Samuel Štancl 2019-09-20 20:14:58 +02:00
parent 93fc961b34
commit 509d00f9f3
6 changed files with 184 additions and 6 deletions

View file

@ -8,6 +8,13 @@ use Illuminate\Cache\CacheManager as BaseCacheManager;
class CacheManager extends BaseCacheManager class CacheManager extends BaseCacheManager
{ {
/**
* Add tags and forward the call to the inner cache store.
*
* @param string $method
* @param array $parameters
* @return mixed
*/
public function __call($method, $parameters) public function __call($method, $parameters)
{ {
$tags = [config('tenancy.cache.tag_base') . tenant('id')]; $tags = [config('tenancy.cache.tag_base') . tenant('id')];

View file

@ -6,6 +6,9 @@ namespace Stancl\Tenancy\Contracts;
use Stancl\Tenancy\Tenant; use Stancl\Tenancy\Tenant;
/**
* TenancyBootstrappers are classes that make existing code tenant-aware.
*/
interface TenancyBootstrapper interface TenancyBootstrapper
{ {
public function start(Tenant $tenant); public function start(Tenant $tenant);

View file

@ -81,7 +81,13 @@ class DatabaseManager
return $this->app['config']["database.connections.$connectionName.driver"]; return $this->app['config']["database.connections.$connectionName.driver"];
} }
public function switchConnection($connection) /**
* Switch the application's connection.
*
* @param string $connection
* @return void
*/
public function switchConnection(string $connection)
{ {
$this->app['config']['database.default'] = $connection; $this->app['config']['database.default'] = $connection;
$this->database->purge(); $this->database->purge();
@ -103,6 +109,12 @@ class DatabaseManager
} }
} }
/**
* Create a database for a tenant.
*
* @param Tenant $tenant
* @return void
*/
public function createDatabase(Tenant $tenant) public function createDatabase(Tenant $tenant)
{ {
$database = $tenant->getDatabaseName(); $database = $tenant->getDatabaseName();
@ -115,6 +127,12 @@ class DatabaseManager
} }
} }
/**
* Delete a tenant's database.
*
* @param Tenant $tenant
* @return void
*/
public function deleteDatabase(Tenant $tenant) public function deleteDatabase(Tenant $tenant)
{ {
$database = $tenant->getDatabaseName(); $database = $tenant->getDatabaseName();
@ -127,6 +145,12 @@ class DatabaseManager
} }
} }
/**
* Get the TenantDatabaseManager for a tenant's database connection.
*
* @param Tenant $tenant
* @return TenantDatabaseManager
*/
protected function getTenantDatabaseManager(Tenant $tenant): TenantDatabaseManager protected function getTenantDatabaseManager(Tenant $tenant): TenantDatabaseManager
{ {
// todo2 this shouldn't have to create a connection // todo2 this shouldn't have to create a connection

View file

@ -7,6 +7,7 @@ namespace Stancl\Tenancy;
use ArrayAccess; use ArrayAccess;
use Illuminate\Foundation\Application; use Illuminate\Foundation\Application;
use Illuminate\Support\Str; use Illuminate\Support\Str;
use Illuminate\Support\Traits\ForwardsCalls;
use Stancl\Tenancy\Contracts\StorageDriver; use Stancl\Tenancy\Contracts\StorageDriver;
use Stancl\Tenancy\Contracts\UniqueIdentifierGenerator; use Stancl\Tenancy\Contracts\UniqueIdentifierGenerator;
use Stancl\Tenancy\Exceptions\TenantStorageException; use Stancl\Tenancy\Exceptions\TenantStorageException;
@ -18,7 +19,8 @@ use Stancl\Tenancy\Exceptions\TenantStorageException;
*/ */
class Tenant implements ArrayAccess class Tenant implements ArrayAccess
{ {
use Traits\HasArrayAccess; use Traits\HasArrayAccess,
ForwardsCalls;
/** /**
* Tenant data. A "cache" of tenant storage. * Tenant data. A "cache" of tenant storage.
@ -53,6 +55,14 @@ class Tenant implements ArrayAccess
*/ */
protected $persisted = false; protected $persisted = false;
/**
* Use new() if you don't want to swap dependencies.
*
* @param Application $app
* @param StorageDriver $storage
* @param TenantManager $tenantManager
* @param UniqueIdentifierGenerator $idGenerator
*/
public function __construct(Application $app, StorageDriver $storage, TenantManager $tenantManager, UniqueIdentifierGenerator $idGenerator) public function __construct(Application $app, StorageDriver $storage, TenantManager $tenantManager, UniqueIdentifierGenerator $idGenerator)
{ {
$this->app = $app; $this->app = $app;
@ -61,6 +71,12 @@ class Tenant implements ArrayAccess
$this->idGenerator = $idGenerator; $this->idGenerator = $idGenerator;
} }
/**
* Public constructor.
*
* @param Application $app
* @return self
*/
public static function new(Application $app = null): self public static function new(Application $app = null): self
{ {
$app = $app ?? app(); $app = $app ?? app();
@ -73,11 +89,25 @@ class Tenant implements ArrayAccess
); );
} }
/**
* DO NOT CALL THIS METHOD FROM USERLAND. Used by storage
* drivers to create persisted instances of Tenant.
*
* @param array $data
* @return self
*/
public static function fromStorage(array $data): self public static function fromStorage(array $data): self
{ {
return static::new()->withData($data)->persisted(true); return static::new()->withData($data)->persisted(true);
} }
/**
* Create a tenant in a single command.
*
* @param string|string[] $domains
* @param array $data
* @return self
*/
public static function create($domains, array $data = []): self public static function create($domains, array $data = []): self
{ {
return static::new()->withDomains((array) $domains)->withData($data)->save(); return static::new()->withDomains((array) $domains)->withData($data)->save();
@ -94,6 +124,11 @@ class Tenant implements ArrayAccess
return $this; return $this;
} }
/**
* Does this model exist in the tenant storage.
*
* @return boolean
*/
public function isPersisted(): bool public function isPersisted(): bool
{ {
return $this->persisted; return $this->persisted;
@ -127,6 +162,11 @@ class Tenant implements ArrayAccess
return $this; return $this;
} }
/**
* Unassign all domains from the tenant.
*
* @return self
*/
public function clearDomains(): self public function clearDomains(): self
{ {
$this->domains = []; $this->domains = [];
@ -134,6 +174,12 @@ class Tenant implements ArrayAccess
return $this; return $this;
} }
/**
* Set (overwrite) the tenant's domains.
*
* @param string|string[] $domains
* @return self
*/
public function withDomains($domains): self public function withDomains($domains): self
{ {
$domains = (array) $domains; $domains = (array) $domains;
@ -143,6 +189,12 @@ class Tenant implements ArrayAccess
return $this; return $this;
} }
/**
* Set (overwrite) tenant data.
*
* @param array $data
* @return self
*/
public function withData(array $data): self public function withData(array $data): self
{ {
$this->data = $data; $this->data = $data;
@ -150,11 +202,21 @@ class Tenant implements ArrayAccess
return $this; return $this;
} }
/**
* Generate a random ID.
*
* @return void
*/
public function generateId() public function generateId()
{ {
$this->id = $this->idGenerator->generate($this->domains, $this->data); $this->id = $this->idGenerator->generate($this->domains, $this->data);
} }
/**
* Write the tenant's state to storage.
*
* @return self
*/
public function save(): self public function save(): self
{ {
if (! isset($this->data['id'])) { if (! isset($this->data['id'])) {
@ -188,7 +250,7 @@ class Tenant implements ArrayAccess
} }
/** /**
* Unassign all domains from the tenant. * Unassign all domains from the tenant and write to storage.
* *
* @return self * @return self
*/ */
@ -201,12 +263,22 @@ class Tenant implements ArrayAccess
return $this; return $this;
} }
public function getDatabaseName() /**
* Get the tenant's database's name.
*
* @return string
*/
public function getDatabaseName(): string
{ {
return $this->data['_tenancy_db_name'] ?? ($this->app['config']['tenancy.database.prefix'] . $this->id . $this->app['config']['tenancy.database.suffix']); return $this->data['_tenancy_db_name'] ?? ($this->app['config']['tenancy.database.prefix'] . $this->id . $this->app['config']['tenancy.database.suffix']);
} }
public function getConnectionName() /**
* Get the tenant's database connection's name.
*
* @return string
*/
public function getConnectionName(): string
{ {
return $this->data['_tenancy_db_connection'] ?? 'tenant'; return $this->data['_tenancy_db_connection'] ?? 'tenant';
} }
@ -243,6 +315,13 @@ class Tenant implements ArrayAccess
return $this->data[$key]; return $this->data[$key];
} }
/**
* Set a value and write to storage.
*
* @param string|array<string, mixed> $key
* @param mixed $value
* @return self
*/
public function put($key, $value = null): self public function put($key, $value = null): self
{ {
if ($key === 'id') { if ($key === 'id') {
@ -268,6 +347,13 @@ class Tenant implements ArrayAccess
return $this->put($key, $value); return $this->put($key, $value);
} }
/**
* Set a value.
*
* @param string $key
* @param mixed $value
* @return self
*/
public function with(string $key, $value): self public function with(string $key, $value): self
{ {
$this->data[$key] = $value; $this->data[$key] = $value;
@ -285,6 +371,7 @@ class Tenant implements ArrayAccess
if ($key === 'id' && isset($this->data['id'])) { if ($key === 'id' && isset($this->data['id'])) {
throw new TenantStorageException("Tenant ids can't be changed."); throw new TenantStorageException("Tenant ids can't be changed.");
} }
$this->data[$key] = $value; $this->data[$key] = $value;
} }
@ -294,6 +381,6 @@ class Tenant implements ArrayAccess
return $this->with(Str::snake(substr($method, 4)), $parameters[0]); return $this->with(Str::snake(substr($method, 4)), $parameters[0]);
} }
// todo throw some exception? static::throwBadMethodCallException($method);
} }
} }

View file

@ -50,6 +50,12 @@ class TenantManager
$this->bootstrapFeatures(); $this->bootstrapFeatures();
} }
/**
* Write a new tenant to storage.
*
* @param Tenant $tenant
* @return self
*/
public function createTenant(Tenant $tenant): self public function createTenant(Tenant $tenant): self
{ {
$this->ensureTenantCanBeCreated($tenant); $this->ensureTenantCanBeCreated($tenant);
@ -66,6 +72,12 @@ class TenantManager
return $this; return $this;
} }
/**
* Delete a tenant from storage.
*
* @param Tenant $tenant
* @return self
*/
public function deleteTenant(Tenant $tenant): self public function deleteTenant(Tenant $tenant): self
{ {
$this->storage->deleteTenant($tenant); $this->storage->deleteTenant($tenant);
@ -77,6 +89,13 @@ class TenantManager
return $this; return $this;
} }
/**
* Alias for Stancl\Tenancy\Tenant::create.
*
* @param string|string[] $domains
* @param array $data
* @return Tenant
*/
public static function create($domains, array $data = []): Tenant public static function create($domains, array $data = []): Tenant
{ {
return Tenant::create($domains, $data); return Tenant::create($domains, $data);
@ -95,6 +114,12 @@ class TenantManager
$this->database->ensureTenantCanBeCreated($tenant); $this->database->ensureTenantCanBeCreated($tenant);
} }
/**
* Update an existing tenant in storage.
*
* @param Tenant $tenant
* @return self
*/
public function updateTenant(Tenant $tenant): self public function updateTenant(Tenant $tenant): self
{ {
$this->storage->updateTenant($tenant); $this->storage->updateTenant($tenant);
@ -102,6 +127,12 @@ class TenantManager
return $this; return $this;
} }
/**
* Find tenant by domain & initialize tenancy.
*
* @param string $domain
* @return self
*/
public function init(string $domain = null): self public function init(string $domain = null): self
{ {
$domain = $domain ?? request()->getHost(); $domain = $domain ?? request()->getHost();
@ -110,6 +141,12 @@ class TenantManager
return $this; return $this;
} }
/**
* Find tenant by ID & initialize tenancy.
*
* @param string $id
* @return self
*/
public function initById(string $id): self public function initById(string $id): self
{ {
$this->initializeTenancy($this->find($id)); $this->initializeTenancy($this->find($id));
@ -156,6 +193,12 @@ class TenantManager
return collect($this->storage->all($only)); return collect($this->storage->all($only));
} }
/**
* Initialize tenancy.
*
* @param Tenant $tenant
* @return self
*/
public function initializeTenancy(Tenant $tenant): self public function initializeTenancy(Tenant $tenant): self
{ {
$this->setTenant($tenant); $this->setTenant($tenant);
@ -171,6 +214,12 @@ class TenantManager
return $this->initializeTenancy($tenant); return $this->initializeTenancy($tenant);
} }
/**
* Execute TenancyBootstrappers.
*
* @param Tenant $tenant
* @return self
*/
public function bootstrapTenancy(Tenant $tenant): self public function bootstrapTenancy(Tenant $tenant): self
{ {
$prevented = $this->event('bootstrapping'); $prevented = $this->event('bootstrapping');

View file

@ -86,4 +86,12 @@ class TenantClassTest extends TestCase
$this->assertSame('xyz', $tenant2->foo_bar); $this->assertSame('xyz', $tenant2->foo_bar);
$this->assertArrayHasKey('foo_bar', $tenant2->data); $this->assertArrayHasKey('foo_bar', $tenant2->data);
} }
/** @test */
public function an_exception_is_thrown_when_an_unknown_method_is_called()
{
$tenant = Tenant::new();
$this->expectException(\BadMethodCallException::class);
$tenant->sdjigndfgnjdfgj();
}
} }