mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-13 09:44:03 +00:00
get down to 59 phpstan errors
This commit is contained in:
parent
193e044777
commit
a94227a19c
26 changed files with 130 additions and 130 deletions
|
|
@ -10,6 +10,7 @@ use Stancl\Tenancy\Resolvers\Contracts\CachedTenantResolver;
|
|||
|
||||
trait InvalidatesResolverCache
|
||||
{
|
||||
/** @var array<class-string<CachedTenantResolver>> */
|
||||
public static $resolvers = [
|
||||
Resolvers\DomainTenantResolver::class,
|
||||
Resolvers\PathTenantResolver::class,
|
||||
|
|
|
|||
|
|
@ -13,7 +13,8 @@ use Stancl\Tenancy\Resolvers\Contracts\CachedTenantResolver;
|
|||
*/
|
||||
trait InvalidatesTenantsResolverCache
|
||||
{
|
||||
public static $resolvers = [
|
||||
/** @var array<class-string<CachedTenantResolver>> */
|
||||
public static array $resolvers = [ // todo single source of truth for this here and in InvalidatesResolverCache
|
||||
Resolvers\DomainTenantResolver::class,
|
||||
Resolvers\PathTenantResolver::class,
|
||||
Resolvers\RequestDataTenantResolver::class,
|
||||
|
|
|
|||
|
|
@ -26,20 +26,20 @@ class DatabaseConfig
|
|||
|
||||
public static function __constructStatic(): void
|
||||
{
|
||||
static::$usernameGenerator = static::$usernameGenerator ?? function (Tenant $tenant) {
|
||||
static::$usernameGenerator = static::$usernameGenerator ?? function (Model&Tenant $tenant) {
|
||||
return Str::random(16);
|
||||
};
|
||||
|
||||
static::$passwordGenerator = static::$passwordGenerator ?? function (Tenant $tenant) {
|
||||
static::$passwordGenerator = static::$passwordGenerator ?? function (Model&Tenant $tenant) {
|
||||
return Hash::make(Str::random(32));
|
||||
};
|
||||
|
||||
static::$databaseNameGenerator = static::$databaseNameGenerator ?? function (Tenant $tenant) {
|
||||
static::$databaseNameGenerator = static::$databaseNameGenerator ?? function (Model&Tenant $tenant) {
|
||||
return config('tenancy.database.prefix') . $tenant->getTenantKey() . config('tenancy.database.suffix');
|
||||
};
|
||||
}
|
||||
|
||||
public function __construct(Tenant $tenant)
|
||||
public function __construct(Model&Tenant $tenant)
|
||||
{
|
||||
static::__constructStatic();
|
||||
|
||||
|
|
@ -61,7 +61,7 @@ class DatabaseConfig
|
|||
static::$passwordGenerator = $passwordGenerator;
|
||||
}
|
||||
|
||||
public function getName(): ?string
|
||||
public function getName(): string
|
||||
{
|
||||
return $this->tenant->getInternal('db_name') ?? (static::$databaseNameGenerator)($this->tenant);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,25 +15,13 @@ use Stancl\Tenancy\Database\Contracts\TenantWithDatabase;
|
|||
*/
|
||||
class DatabaseManager
|
||||
{
|
||||
/** @var Application */
|
||||
protected $app;
|
||||
public function __construct(
|
||||
protected Application $app,
|
||||
protected BaseDatabaseManager $database,
|
||||
protected Repository $config,
|
||||
) {}
|
||||
|
||||
/** @var BaseDatabaseManager */
|
||||
protected $database;
|
||||
|
||||
/** @var Repository */
|
||||
protected $config;
|
||||
|
||||
public function __construct(Application $app, BaseDatabaseManager $database, Repository $config)
|
||||
{
|
||||
$this->app = $app;
|
||||
$this->database = $database;
|
||||
$this->config = $config;
|
||||
}
|
||||
|
||||
/**
|
||||
* Connect to a tenant's database.
|
||||
*/
|
||||
/** Connect to a tenant's database. */
|
||||
public function connectToTenant(TenantWithDatabase $tenant): void
|
||||
{
|
||||
$this->purgeTenantConnection();
|
||||
|
|
@ -41,35 +29,27 @@ class DatabaseManager
|
|||
$this->setDefaultConnection('tenant');
|
||||
}
|
||||
|
||||
/**
|
||||
* Reconnect to the default non-tenant connection.
|
||||
*/
|
||||
/** Reconnect to the default non-tenant connection. */
|
||||
public function reconnectToCentral(): void
|
||||
{
|
||||
$this->purgeTenantConnection();
|
||||
$this->setDefaultConnection($this->config->get('tenancy.database.central_connection'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Change the default database connection config.
|
||||
*/
|
||||
/** Change the default database connection config. */
|
||||
public function setDefaultConnection(string $connection): void
|
||||
{
|
||||
$this->config['database.default'] = $connection;
|
||||
$this->database->setDefaultConnection($connection);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create the tenant database connection.
|
||||
*/
|
||||
/** Create the tenant database connection. */
|
||||
public function createTenantConnection(TenantWithDatabase $tenant): void
|
||||
{
|
||||
$this->config['database.connections.tenant'] = $tenant->database()->connection();
|
||||
}
|
||||
|
||||
/**
|
||||
* Purge the tenant database connection.
|
||||
*/
|
||||
/** Purge the tenant database connection. */
|
||||
public function purgeTenantConnection(): void
|
||||
{
|
||||
if (array_key_exists('tenant', $this->database->getConnections())) {
|
||||
|
|
@ -83,8 +63,8 @@ class DatabaseManager
|
|||
* Check if a tenant can be created.
|
||||
*
|
||||
* @throws TenantCannotBeCreatedException
|
||||
* @throws DatabaseManagerNotRegisteredException
|
||||
* @throws TenantDatabaseAlreadyExistsException
|
||||
* @throws Exceptions\DatabaseManagerNotRegisteredException
|
||||
* @throws Exceptions\TenantDatabaseAlreadyExistsException
|
||||
*/
|
||||
public function ensureTenantCanBeCreated(TenantWithDatabase $tenant): void
|
||||
{
|
||||
|
|
@ -94,8 +74,13 @@ class DatabaseManager
|
|||
throw new Exceptions\TenantDatabaseAlreadyExistsException($database);
|
||||
}
|
||||
|
||||
if ($manager instanceof Contracts\ManagesDatabaseUsers && $manager->userExists($username = $tenant->database()->getUsername())) {
|
||||
throw new Exceptions\TenantDatabaseUserAlreadyExistsException($username);
|
||||
if ($manager instanceof Contracts\ManagesDatabaseUsers) {
|
||||
/** @var string $username */
|
||||
$username = $tenant->database()->getUsername();
|
||||
|
||||
if ($manager->userExists($username)) {
|
||||
throw new Exceptions\TenantDatabaseUserAlreadyExistsException($username);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ class ParentModelScope implements Scope
|
|||
$builder->whereHas($builder->getModel()->getRelationshipToPrimaryModel());
|
||||
}
|
||||
|
||||
public function extend(Builder $builder)
|
||||
public function extend(Builder $builder): void
|
||||
{
|
||||
$builder->macro('withoutParentModel', function (Builder $builder) {
|
||||
return $builder->withoutGlobalScope($this);
|
||||
|
|
|
|||
|
|
@ -12,7 +12,8 @@ class PermissionControlledMySQLDatabaseManager extends MySQLDatabaseManager impl
|
|||
{
|
||||
use CreatesDatabaseUsers;
|
||||
|
||||
public static $grants = [
|
||||
/** @var string[] */
|
||||
public static array $grants = [
|
||||
'ALTER', 'ALTER ROUTINE', 'CREATE', 'CREATE ROUTINE', 'CREATE TEMPORARY TABLES', 'CREATE VIEW',
|
||||
'DELETE', 'DROP', 'EVENT', 'EXECUTE', 'INDEX', 'INSERT', 'LOCK TABLES', 'REFERENCES', 'SELECT',
|
||||
'SHOW VIEW', 'TRIGGER', 'UPDATE',
|
||||
|
|
|
|||
|
|
@ -20,7 +20,7 @@ class TenantScope implements Scope
|
|||
$builder->where($model->qualifyColumn(BelongsToTenant::$tenantIdColumn), tenant()->getTenantKey());
|
||||
}
|
||||
|
||||
public function extend(Builder $builder)
|
||||
public function extend(Builder $builder): void
|
||||
{
|
||||
$builder->macro('withoutTenancy', function (Builder $builder) {
|
||||
return $builder->withoutGlobalScope($this);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue