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

Queue tests

This commit is contained in:
Samuel Štancl 2020-05-11 05:22:55 +02:00
parent 00bb0d06b3
commit 86a98b2bc8
13 changed files with 216 additions and 124 deletions

View file

@ -2,13 +2,15 @@
namespace Stancl\Tenancy\Database\Models\Concerns;
use Stancl\Tenancy\Contracts\UniqueIdentifierGenerator;
trait GeneratesIds
{
public static function bootGeneratesIds()
{
static::creating(function (self $model) {
if (! $model->id && config('tenancy.id_generator')) {
$model->id = app(config('tenancy.id_generator'))->generate($model);
if (! $model->id && app()->bound(UniqueIdentifierGenerator::class)) {
$model->id = app(UniqueIdentifierGenerator::class)->generate($model);
}
});
}

View file

@ -1,21 +0,0 @@
<?php
declare(strict_types=1);
namespace Stancl\Tenancy\Facades;
use Illuminate\Support\Facades\Facade;
use Stancl\Tenancy\Tenant as TenantObject;
class Tenant extends Facade
{
protected static function getFacadeAccessor()
{
return TenantObject::class;
}
public static function create($domains, array $data = []): TenantObject
{
return TenantObject::create($domains, $data);
}
}

View file

@ -63,4 +63,9 @@ class Tenancy
return new $class;
}
public function find($id): ?Tenant
{
return $this->model()->find($id);
}
}

View file

@ -5,28 +5,63 @@ declare(strict_types=1);
namespace Stancl\Tenancy\TenancyBootstrappers;
use Illuminate\Config\Repository;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Queue\Events\JobProcessing;
use Illuminate\Queue\QueueManager;
use Illuminate\Support\Testing\Fakes\QueueFake;
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
use Stancl\Tenancy\Contracts\Tenant;
// todo rewrite this
class QueueTenancyBootstrapper implements TenancyBootstrapper
{
/** @var bool Has tenancy been started. */
public $started = false;
public $tenancyInitialized = false;
/** @var Repository */
protected $config;
public function __construct(Repository $config, QueueManager $queue)
/** @var QueueManager */
protected $queue;
/** @var Dispatcher */
protected $event;
public function __construct(Repository $config, QueueManager $queue, Dispatcher $event)
{
$this->config = $config;
$this->queue = $queue;
$this->event = $event;
$this->setUpJobListener();
$this->setUpPayloadGenerator();
}
protected function setUpJobListener()
{
$this->event->listen(JobProcessing::class, function ($event) {
$tenantId = $event->job->payload()['tenant_id'] ?? null;
// The job is not tenant-aware
if (!$tenantId) {
return;
}
// Tenancy is already initialized for the tenant (e.g. dispatchNow was used)
if (tenancy()->initialized && tenant('id') === $tenantId) {
return;
}
// Tenancy was either not initialized, or initialized for a different tenant.
// Therefore, we initialize it for the correct tenant.
tenancy()->initialize(tenancy()->find($tenantId));
});
}
protected function setUpPayloadGenerator()
{
$bootstrapper = &$this;
if (! $queue instanceof QueueFake) {
$queue->createPayloadUsing(function ($connection) use (&$bootstrapper) {
if (! $this->queue instanceof QueueFake) {
$this->queue->createPayloadUsing(function ($connection) use (&$bootstrapper) {
return $bootstrapper->getPayload($connection);
});
}
@ -34,17 +69,17 @@ class QueueTenancyBootstrapper implements TenancyBootstrapper
public function start(Tenant $tenant)
{
$this->started = true;
$this->tenancyInitialized = true;
}
public function end()
{
$this->started = false;
$this->tenancyInitialized = false;
}
public function getPayload(string $connection)
{
if (! $this->started) {
if (! $this->tenancyInitialized) {
return [];
}

View file

@ -5,10 +5,8 @@ declare(strict_types=1);
namespace Stancl\Tenancy;
use Illuminate\Cache\CacheManager;
use Illuminate\Contracts\Http\Kernel;
use Illuminate\Support\Facades\Route;
use Illuminate\Queue\Events\JobProcessing;
use Illuminate\Support\ServiceProvider;
use Stancl\Tenancy\StorageDrivers\Database\DatabaseStorageDriver;
use Stancl\Tenancy\TenancyBootstrappers\FilesystemTenancyBootstrapper;
use Stancl\Tenancy\Contracts\Tenant;
@ -23,10 +21,7 @@ class TenancyServiceProvider extends ServiceProvider
{
$this->mergeConfigFrom(__DIR__ . '/../assets/config.php', 'tenancy');
$this->app->bind(Contracts\StorageDriver::class, function ($app) {
return $app->make(DatabaseStorageDriver::class);
});
$this->app->bind(Contracts\UniqueIdentifierGenerator::class, $this->app['config']['tenancy.unique_id_generator']);
$this->app->bind(Contracts\UniqueIdentifierGenerator::class, $this->app['config']['tenancy.id_generator']);
$this->app->singleton(DatabaseManager::class);
$this->app->singleton(Tenancy::class);
$this->app->bind(Tenant::class, function ($app) {
@ -80,22 +75,6 @@ class TenancyServiceProvider extends ServiceProvider
__DIR__ . '/../assets/migrations/' => database_path('migrations'),
], 'migrations');
foreach ($this->app['config']['tenancy.global_middleware'] as $middleware) {
$this->app->make(Kernel::class)->prependMiddleware($middleware);
}
/*
* Since tenancy is initialized in the global middleware stack, this
* middleware group acts mostly as a 'flag' for the PreventAccess
* middleware to decide whether the request should be aborted.
*/
Route::middlewareGroup('tenancy', [
/* Prevent access from tenant domains to central routes and vice versa. */
Middleware\PreventAccessFromTenantDomains::class,
]);
Route::middlewareGroup('universal', []);
$this->loadRoutesFrom(__DIR__ . '/routes.php');
$this->app->singleton('globalUrl', function ($app) {
@ -108,24 +87,5 @@ class TenancyServiceProvider extends ServiceProvider
return $instance;
});
// Queue tenancy
$this->app['events']->listen(\Illuminate\Queue\Events\JobProcessing::class, function ($event) {
$tenantId = $event->job->payload()['tenant_id'] ?? null;
// The job is not tenant-aware
if (! $tenantId) {
return;
}
// Tenancy is already initialized for the tenant (e.g. dispatchNow was used)
if (tenancy()->initialized && tenant('id') === $tenantId) {
return;
}
// Tenancy was either not initialized, or initialized for a different tenant.
// Therefore, we initialize it for the correct tenant.
tenancy()->initById($tenantId);
});
}
}