From e9b6de78b2f38203efbd089ac610c891d3758610 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Tue, 2 Aug 2022 04:21:03 +0200 Subject: [PATCH] minor code updates --- src/CacheManager.php | 2 +- src/Tenancy.php | 36 +++++++++++++++------------------- src/TenancyServiceProvider.php | 8 ++------ 3 files changed, 19 insertions(+), 27 deletions(-) diff --git a/src/CacheManager.php b/src/CacheManager.php index 09581201..eb6bac4c 100644 --- a/src/CacheManager.php +++ b/src/CacheManager.php @@ -26,7 +26,7 @@ class CacheManager extends BaseCacheManager } $names = $parameters[0]; - $names = (array) $names; // cache()->tags('foo') https://laravel.com/docs/5.7/cache#removing-tagged-cache-items + $names = (array) $names; // cache()->tags('foo') https://laravel.com/docs/9.x/cache#removing-tagged-cache-items return $this->store()->tags(array_merge($tags, $names)); } diff --git a/src/Tenancy.php b/src/Tenancy.php index 439c34cb..1359fcab 100644 --- a/src/Tenancy.php +++ b/src/Tenancy.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace Stancl\Tenancy; +use Closure; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Traits\Macroable; @@ -16,20 +17,17 @@ class Tenancy { use Macroable, Debuggable; - /** @var Tenant|Model|null */ - public $tenant; + /** The current tenant. */ + public Tenant&Model $tenant; - /** @var callable|null */ - public $getBootstrappersUsing = null; + // todo docblock + public ?Closure $getBootstrappersUsing = null; - /** @var bool */ - public $initialized = false; + /** Is tenancy fully initialized? */ + public $initialized = false; // todo document the difference between $tenant being set and $initialized being true (e.g. end of initialize() method) - /** - * Initializes the tenant. - * @param Tenant|int|string $tenant - */ - public function initialize($tenant): void + /** Initialize tenancy for the passed tenant. */ + public function initialize(Tenant|int|string $tenant): void { if (! is_object($tenant)) { $tenantId = $tenant; @@ -85,29 +83,28 @@ class Tenancy return array_map('app', $resolve($this->tenant)); } - public function query(): Builder + public static function query(): Builder { - return $this->model()->query(); + return static::model()->query(); } - /** @return Tenant|Model */ - public function model() + public static function model(): Tenant&Model { $class = config('tenancy.tenant_model'); return new $class; } - public function find($id): ?Tenant + public static function find(int|string $id): Tenant|null { - return $this->model()->where($this->model()->getTenantKeyName(), $id)->first(); + return static::model()->where(static::model()->getTenantKeyName(), $id)->first(); } /** * Run a callback in the central context. * Atomic, safely reverts to previous context. */ - public function central(callable $callback) + public function central(Closure $callback) { $previousTenant = $this->tenant; @@ -129,9 +126,8 @@ class Tenancy * More performant than running $tenant->run() one by one. * * @param Tenant[]|\Traversable|string[]|null $tenants - * @return void */ - public function runForMultiple($tenants, callable $callback) + public function runForMultiple($tenants, callable $callback): void { // Convert null to all tenants $tenants = is_null($tenants) ? $this->model()->cursor() : $tenants; diff --git a/src/TenancyServiceProvider.php b/src/TenancyServiceProvider.php index debe6b17..3850720c 100644 --- a/src/TenancyServiceProvider.php +++ b/src/TenancyServiceProvider.php @@ -16,9 +16,7 @@ use Stancl\Tenancy\Resolvers\DomainTenantResolver; class TenancyServiceProvider extends ServiceProvider { - /** - * Register services. - */ + /* Register services. */ public function register(): void { $this->mergeConfigFrom(__DIR__ . '/../assets/config.php', 'tenancy'); @@ -75,9 +73,7 @@ class TenancyServiceProvider extends ServiceProvider }); } - /** - * Bootstrap services. - */ + /* Bootstrap services. */ public function boot(): void { $this->commands([