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

minor code updates

This commit is contained in:
Samuel Štancl 2022-08-02 04:21:03 +02:00
parent d0baabbc9d
commit e9b6de78b2
3 changed files with 19 additions and 27 deletions

View file

@ -26,7 +26,7 @@ class CacheManager extends BaseCacheManager
} }
$names = $parameters[0]; $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)); return $this->store()->tags(array_merge($tags, $names));
} }

View file

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Stancl\Tenancy; namespace Stancl\Tenancy;
use Closure;
use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Traits\Macroable; use Illuminate\Support\Traits\Macroable;
@ -16,20 +17,17 @@ class Tenancy
{ {
use Macroable, Debuggable; use Macroable, Debuggable;
/** @var Tenant|Model|null */ /** The current tenant. */
public $tenant; public Tenant&Model $tenant;
/** @var callable|null */ // todo docblock
public $getBootstrappersUsing = null; public ?Closure $getBootstrappersUsing = null;
/** @var bool */ /** Is tenancy fully initialized? */
public $initialized = false; public $initialized = false; // todo document the difference between $tenant being set and $initialized being true (e.g. end of initialize() method)
/** /** Initialize tenancy for the passed tenant. */
* Initializes the tenant. public function initialize(Tenant|int|string $tenant): void
* @param Tenant|int|string $tenant
*/
public function initialize($tenant): void
{ {
if (! is_object($tenant)) { if (! is_object($tenant)) {
$tenantId = $tenant; $tenantId = $tenant;
@ -85,29 +83,28 @@ class Tenancy
return array_map('app', $resolve($this->tenant)); 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 static function model(): Tenant&Model
public function model()
{ {
$class = config('tenancy.tenant_model'); $class = config('tenancy.tenant_model');
return new $class; 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. * Run a callback in the central context.
* Atomic, safely reverts to previous context. * Atomic, safely reverts to previous context.
*/ */
public function central(callable $callback) public function central(Closure $callback)
{ {
$previousTenant = $this->tenant; $previousTenant = $this->tenant;
@ -129,9 +126,8 @@ class Tenancy
* More performant than running $tenant->run() one by one. * More performant than running $tenant->run() one by one.
* *
* @param Tenant[]|\Traversable|string[]|null $tenants * @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 // Convert null to all tenants
$tenants = is_null($tenants) ? $this->model()->cursor() : $tenants; $tenants = is_null($tenants) ? $this->model()->cursor() : $tenants;

View file

@ -16,9 +16,7 @@ use Stancl\Tenancy\Resolvers\DomainTenantResolver;
class TenancyServiceProvider extends ServiceProvider class TenancyServiceProvider extends ServiceProvider
{ {
/** /* Register services. */
* Register services.
*/
public function register(): void public function register(): void
{ {
$this->mergeConfigFrom(__DIR__ . '/../assets/config.php', 'tenancy'); $this->mergeConfigFrom(__DIR__ . '/../assets/config.php', 'tenancy');
@ -75,9 +73,7 @@ class TenancyServiceProvider extends ServiceProvider
}); });
} }
/** /* Bootstrap services. */
* Bootstrap services.
*/
public function boot(): void public function boot(): void
{ {
$this->commands([ $this->commands([