mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 12:44:02 +00:00
Custom tenant collections
This commit is contained in:
parent
1a8d150f2c
commit
5e99fca070
4 changed files with 86 additions and 1 deletions
|
|
@ -7,7 +7,18 @@ namespace Stancl\Tenancy\Contracts;
|
|||
*/
|
||||
interface Tenant
|
||||
{
|
||||
/** Get the name of the key used for identifying the tenant. */
|
||||
public function getTenantKeyName(): string;
|
||||
|
||||
/** Get the value of the key used for identifying the tenant. */
|
||||
public function getTenantKey(): string;
|
||||
|
||||
/** Get the value of an internal key. */
|
||||
public function getInternal(string $key);
|
||||
|
||||
/** Set the value of an internal key. */
|
||||
public function setInternal(string $key, $value);
|
||||
|
||||
/** Run a callback in this tenant's environment. */
|
||||
public function run(callable $callback);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
23
src/Database/Collections/TenantCollection.php
Normal file
23
src/Database/Collections/TenantCollection.php
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
<?php
|
||||
|
||||
namespace Stancl\Tenancy\Database\Collections;
|
||||
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
use Stancl\Tenancy\Contracts\Tenant;
|
||||
|
||||
/**
|
||||
* @property Tenant[] $items
|
||||
* @method void __construct(Tenant[] $items = [])
|
||||
* @method Tenant[] toArray()
|
||||
* @method Tenant offsetGet($key)
|
||||
* @method Tenant first()
|
||||
*/
|
||||
class TenantCollection extends Collection
|
||||
{
|
||||
public function runForEach(callable $callable): self
|
||||
{
|
||||
tenancy()->runForMultiple($this->items, $callable);
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
|
@ -6,6 +6,7 @@ use Carbon\Carbon;
|
|||
use Illuminate\Database\Eloquent\Model;
|
||||
use Stancl\Tenancy\Events;
|
||||
use Stancl\Tenancy\Contracts;
|
||||
use Stancl\Tenancy\Database\Collections\TenantCollection;
|
||||
use Stancl\Tenancy\Database\Concerns;
|
||||
|
||||
/**
|
||||
|
|
@ -13,6 +14,8 @@ use Stancl\Tenancy\Database\Concerns;
|
|||
* @property Carbon $created_at
|
||||
* @property Carbon $updated_at
|
||||
* @property array $data
|
||||
*
|
||||
* @method TenantCollection all()
|
||||
*/
|
||||
class Tenant extends Model implements Contracts\Tenant
|
||||
{
|
||||
|
|
@ -36,6 +39,11 @@ class Tenant extends Model implements Contracts\Tenant
|
|||
return $this->getAttribute($this->getTenantKeyName());
|
||||
}
|
||||
|
||||
public function newCollection(array $models = []): TenantCollection
|
||||
{
|
||||
return new TenantCollection($models);
|
||||
}
|
||||
|
||||
public function getCasts()
|
||||
{
|
||||
return array_merge($this->dataColumnCasts(), [
|
||||
|
|
|
|||
|
|
@ -19,6 +19,7 @@ use Stancl\Tenancy\Listeners\BootstrapTenancy;
|
|||
use Stancl\Tenancy\Listeners\JobPipeline;
|
||||
use Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper;
|
||||
use Illuminate\Support\Str;
|
||||
use Stancl\Tenancy\Database\Collections\TenantCollection;
|
||||
|
||||
class TenantModelTest extends TestCase
|
||||
{
|
||||
|
|
@ -172,6 +173,38 @@ class TenantModelTest extends TestCase
|
|||
{
|
||||
// todo. tests for registerPriorityListener
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function the_model_uses_TenantCollection()
|
||||
{
|
||||
Tenant::create();
|
||||
Tenant::create();
|
||||
|
||||
$this->assertSame(2, Tenant::count());
|
||||
$this->assertTrue(Tenant::all() instanceof TenantCollection);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function a_command_can_be_run_on_a_collection_of_tenants()
|
||||
{
|
||||
Tenant::create([
|
||||
'id' => 't1',
|
||||
'foo' => 'bar',
|
||||
]);
|
||||
Tenant::create([
|
||||
'id' => 't2',
|
||||
'foo' => 'bar',
|
||||
]);
|
||||
|
||||
Tenant::all()->runForEach(function ($tenant) {
|
||||
$tenant->update([
|
||||
'foo' => 'xyz',
|
||||
]);
|
||||
});
|
||||
|
||||
$this->assertSame('xyz', Tenant::find('t1')->foo);
|
||||
$this->assertSame('xyz', Tenant::find('t2')->foo);
|
||||
}
|
||||
}
|
||||
|
||||
class MyTenant extends Tenant
|
||||
|
|
@ -198,4 +231,14 @@ class AnotherTenant extends Model implements Contracts\Tenant
|
|||
{
|
||||
$callback();
|
||||
}
|
||||
|
||||
public function getInternal(string $key)
|
||||
{
|
||||
return $this->$key;
|
||||
}
|
||||
|
||||
public function setInternal(string $key, $value)
|
||||
{
|
||||
$this->$key = $value;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue