mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 19:14:04 +00:00
Change bootstrappers namespace
This commit is contained in:
parent
b772590479
commit
1a8d150f2c
18 changed files with 26 additions and 26 deletions
57
src/Bootstrappers/CacheTenancyBootstrapper.php
Normal file
57
src/Bootstrappers/CacheTenancyBootstrapper.php
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Stancl\Tenancy\Bootstrappers;
|
||||
|
||||
use Illuminate\Cache\CacheManager;
|
||||
use Illuminate\Contracts\Foundation\Application;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Stancl\Tenancy\CacheManager as TenantCacheManager;
|
||||
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
|
||||
use Stancl\Tenancy\Contracts\Tenant;
|
||||
|
||||
class CacheTenancyBootstrapper implements TenancyBootstrapper
|
||||
{
|
||||
/** @var CacheManager */
|
||||
protected $originalCache;
|
||||
|
||||
/** @var Application */
|
||||
protected $app;
|
||||
|
||||
public function __construct(Application $app)
|
||||
{
|
||||
$this->app = $app;
|
||||
}
|
||||
|
||||
public function bootstrap(Tenant $tenant)
|
||||
{
|
||||
$this->resetFacadeCache();
|
||||
|
||||
$this->originalCache = $this->originalCache ?? $this->app['cache'];
|
||||
$this->app->extend('cache', function () {
|
||||
return new TenantCacheManager($this->app);
|
||||
});
|
||||
}
|
||||
|
||||
public function revert()
|
||||
{
|
||||
$this->resetFacadeCache();
|
||||
|
||||
$this->app->extend('cache', function () {
|
||||
return $this->originalCache;
|
||||
});
|
||||
|
||||
$this->originalCache = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* This wouldn't be necessary, but is needed when a call to the
|
||||
* facade has been made prior to bootstrapping tenancy. The
|
||||
* facade has its own cache, separate from the container.
|
||||
*/
|
||||
public function resetFacadeCache()
|
||||
{
|
||||
Cache::clearResolvedInstances();
|
||||
}
|
||||
}
|
||||
39
src/Bootstrappers/DatabaseTenancyBootstrapper.php
Normal file
39
src/Bootstrappers/DatabaseTenancyBootstrapper.php
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Stancl\Tenancy\Bootstrappers;
|
||||
|
||||
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
|
||||
use Stancl\Tenancy\DatabaseManager;
|
||||
use Stancl\Tenancy\Exceptions\TenantDatabaseDoesNotExistException;
|
||||
use Stancl\Tenancy\Contracts\TenantWithDatabase;
|
||||
use Stancl\Tenancy\Contracts\Tenant;
|
||||
|
||||
class DatabaseTenancyBootstrapper implements TenancyBootstrapper
|
||||
{
|
||||
/** @var DatabaseManager */
|
||||
protected $database;
|
||||
|
||||
public function __construct(DatabaseManager $database)
|
||||
{
|
||||
$this->database = $database;
|
||||
}
|
||||
|
||||
public function bootstrap(Tenant $tenant)
|
||||
{
|
||||
/** @var TenantWithDatabase $tenant */
|
||||
|
||||
$database = $tenant->database()->getName();
|
||||
if (! $tenant->database()->manager()->databaseExists($database)) {
|
||||
throw new TenantDatabaseDoesNotExistException($database);
|
||||
}
|
||||
|
||||
$this->database->connectToTenant($tenant);
|
||||
}
|
||||
|
||||
public function revert()
|
||||
{
|
||||
$this->database->reconnectToCentral();
|
||||
}
|
||||
}
|
||||
89
src/Bootstrappers/FilesystemTenancyBootstrapper.php
Normal file
89
src/Bootstrappers/FilesystemTenancyBootstrapper.php
Normal file
|
|
@ -0,0 +1,89 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Stancl\Tenancy\Bootstrappers;
|
||||
|
||||
use Illuminate\Filesystem\FilesystemAdapter;
|
||||
use Illuminate\Foundation\Application;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
|
||||
use Stancl\Tenancy\Contracts\Tenant;
|
||||
|
||||
class FilesystemTenancyBootstrapper implements TenancyBootstrapper
|
||||
{
|
||||
/** @var Application */
|
||||
protected $app;
|
||||
|
||||
/** @var array */
|
||||
public $originalPaths = [];
|
||||
|
||||
public function __construct(Application $app)
|
||||
{
|
||||
$this->app = $app;
|
||||
$this->originalPaths = [
|
||||
'disks' => [],
|
||||
'storage' => $this->app->storagePath(),
|
||||
'asset_url' => $this->app['config']['app.asset_url'],
|
||||
];
|
||||
|
||||
$this->app['url']->macro('setAssetRoot', function ($root) {
|
||||
$this->assetRoot = $root;
|
||||
|
||||
return $this;
|
||||
});
|
||||
}
|
||||
|
||||
public function bootstrap(Tenant $tenant)
|
||||
{
|
||||
$suffix = $this->app['config']['tenancy.filesystem.suffix_base'] . $tenant->getTenantKey();
|
||||
|
||||
// storage_path()
|
||||
if ($this->app['config']['tenancy.filesystem.suffix_storage_path'] ?? true) {
|
||||
$this->app->useStoragePath($this->originalPaths['storage'] . "/{$suffix}");
|
||||
}
|
||||
|
||||
// asset()
|
||||
if ($this->app['config']['tenancy.filesystem.asset_helper_tenancy'] ?? true) {
|
||||
if ($this->originalPaths['asset_url']) {
|
||||
$this->app['config']['app.asset_url'] = ($this->originalPaths['asset_url'] ?? $this->app['config']['app.url']) . "/$suffix";
|
||||
$this->app['url']->setAssetRoot($this->app['config']['app.asset_url']);
|
||||
} else {
|
||||
$this->app['url']->setAssetRoot($this->app['url']->route('stancl.tenancy.asset', ['path' => '']));
|
||||
}
|
||||
}
|
||||
|
||||
// Storage facade
|
||||
foreach ($this->app['config']['tenancy.filesystem.disks'] as $disk) {
|
||||
/** @var FilesystemAdapter $filesystemDisk */
|
||||
$filesystemDisk = Storage::disk($disk);
|
||||
$this->originalPaths['disks'][$disk] = $filesystemDisk->getAdapter()->getPathPrefix();
|
||||
|
||||
if ($root = str_replace('%storage_path%', storage_path(), $this->app['config']["tenancy.filesystem.root_override.{$disk}"])) {
|
||||
$filesystemDisk->getAdapter()->setPathPrefix($root);
|
||||
} else {
|
||||
$root = $this->app['config']["filesystems.disks.{$disk}.root"];
|
||||
|
||||
$filesystemDisk->getAdapter()->setPathPrefix($root . "/{$suffix}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function revert()
|
||||
{
|
||||
// storage_path()
|
||||
$this->app->useStoragePath($this->originalPaths['storage']);
|
||||
|
||||
// asset()
|
||||
$this->app['config']['app.asset_url'] = $this->originalPaths['asset_url'];
|
||||
$this->app['url']->setAssetRoot($this->app['config']['app.asset_url']);
|
||||
|
||||
// Storage facade
|
||||
foreach ($this->app['config']['tenancy.filesystem.disks'] as $disk) {
|
||||
/** @var FilesystemAdapter $filesystemDisk */
|
||||
$filesystemDisk = Storage::disk($disk);
|
||||
|
||||
$filesystemDisk->getAdapter()->setPathPrefix($this->originalPaths['disks'][$disk]);
|
||||
}
|
||||
}
|
||||
}
|
||||
99
src/Bootstrappers/QueueTenancyBootstrapper.php
Normal file
99
src/Bootstrappers/QueueTenancyBootstrapper.php
Normal file
|
|
@ -0,0 +1,99 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Stancl\Tenancy\Bootstrappers;
|
||||
|
||||
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;
|
||||
|
||||
class QueueTenancyBootstrapper implements TenancyBootstrapper
|
||||
{
|
||||
public $tenancyInitialized = false;
|
||||
|
||||
/** @var Repository */
|
||||
protected $config;
|
||||
|
||||
/** @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 (! $this->queue instanceof QueueFake) {
|
||||
$this->queue->createPayloadUsing(function ($connection) use (&$bootstrapper) {
|
||||
return $bootstrapper->getPayload($connection);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public function bootstrap(Tenant $tenant)
|
||||
{
|
||||
$this->tenancyInitialized = true;
|
||||
}
|
||||
|
||||
public function revert()
|
||||
{
|
||||
$this->tenancyInitialized = false;
|
||||
}
|
||||
|
||||
public function getPayload(string $connection)
|
||||
{
|
||||
if (! $this->tenancyInitialized) {
|
||||
return [];
|
||||
}
|
||||
|
||||
if ($this->config["queue.connections.$connection.central"]) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$id = tenant('id');
|
||||
|
||||
return [
|
||||
'tenant_id' => $id,
|
||||
'tags' => [
|
||||
"tenant:$id",
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
51
src/Bootstrappers/RedisTenancyBootstrapper.php
Normal file
51
src/Bootstrappers/RedisTenancyBootstrapper.php
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Stancl\Tenancy\Bootstrappers;
|
||||
|
||||
use Illuminate\Contracts\Config\Repository;
|
||||
use Illuminate\Support\Facades\Redis;
|
||||
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
|
||||
use Stancl\Tenancy\Contracts\Tenant;
|
||||
|
||||
class RedisTenancyBootstrapper implements TenancyBootstrapper
|
||||
{
|
||||
/** @var array<string, string> Original prefixes of connections */
|
||||
public $originalPrefixes = [];
|
||||
|
||||
/** @var Repository */
|
||||
protected $config;
|
||||
|
||||
public function __construct(Repository $config)
|
||||
{
|
||||
$this->config = $config;
|
||||
}
|
||||
|
||||
public function bootstrap(Tenant $tenant)
|
||||
{
|
||||
foreach ($this->prefixedConnections() as $connection) {
|
||||
$prefix = $this->config['tenancy.redis.prefix_base'] . $tenant->getTenantKey();
|
||||
$client = Redis::connection($connection)->client();
|
||||
|
||||
$this->originalPrefixes[$connection] = $client->getOption($client::OPT_PREFIX);
|
||||
$client->setOption($client::OPT_PREFIX, $prefix);
|
||||
}
|
||||
}
|
||||
|
||||
public function revert()
|
||||
{
|
||||
foreach ($this->prefixedConnections() as $connection) {
|
||||
$client = Redis::connection($connection)->client();
|
||||
|
||||
$client->setOption($client::OPT_PREFIX, $this->originalPrefixes[$connection]);
|
||||
}
|
||||
|
||||
$this->originalPrefixes = [];
|
||||
}
|
||||
|
||||
protected function prefixedConnections()
|
||||
{
|
||||
return $this->config['tenancy.redis.prefixed_connections'];
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue