mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 20:34:03 +00:00
Remove old classes
This commit is contained in:
parent
ab9400e81c
commit
048a38a308
2 changed files with 0 additions and 294 deletions
|
|
@ -1,165 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Stancl\Tenancy\Traits;
|
||||
|
||||
use Illuminate\Support\Facades\Redis;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Stancl\Tenancy\CacheManager;
|
||||
use Stancl\Tenancy\Exceptions\PhpRedisNotInstalledException;
|
||||
|
||||
trait BootstrapsTenancy
|
||||
{
|
||||
use TenantManagerEvents;
|
||||
|
||||
public $originalSettings = [];
|
||||
/**
|
||||
* Was tenancy initialized/bootstrapped?
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
public $initialized = false;
|
||||
|
||||
public function bootstrap()
|
||||
{
|
||||
$prevented = $this->event('bootstrapping');
|
||||
$this->initialized = true;
|
||||
|
||||
if (! $prevented->contains('database')) {
|
||||
$this->switchDatabaseConnection();
|
||||
}
|
||||
|
||||
if (! $prevented->contains('redis')) {
|
||||
if ($this->app['config']['tenancy.redis.tenancy']) {
|
||||
$this->setPhpRedisPrefix($this->app['config']['tenancy.redis.prefixed_connections']);
|
||||
}
|
||||
}
|
||||
|
||||
if (! $prevented->contains('cache')) {
|
||||
$this->tagCache();
|
||||
}
|
||||
|
||||
if (! $prevented->contains('filesystem')) {
|
||||
$this->suffixFilesystemRootPaths();
|
||||
}
|
||||
|
||||
$this->event('bootstrapped');
|
||||
}
|
||||
|
||||
public function end()
|
||||
{
|
||||
$prevented = $this->event('ending');
|
||||
|
||||
$this->initialized = false;
|
||||
|
||||
if (! $prevented->contains('database')) {
|
||||
$this->disconnectDatabase();
|
||||
}
|
||||
|
||||
if (! $prevented->contains('redis')) {
|
||||
if ($this->app['config']['tenancy.redis.tenancy']) {
|
||||
$this->resetPhpRedisPrefix($this->app['config']['tenancy.redis.prefixed_connections']);
|
||||
}
|
||||
}
|
||||
|
||||
if (! $prevented->contains('cache')) {
|
||||
$this->untagCache();
|
||||
}
|
||||
|
||||
if (! $prevented->contains('filesystem')) {
|
||||
$this->resetFileSystemRootPaths();
|
||||
}
|
||||
|
||||
$this->event('ended');
|
||||
}
|
||||
|
||||
public function switchDatabaseConnection()
|
||||
{
|
||||
$this->database->connect($this->getDatabaseName());
|
||||
}
|
||||
|
||||
public function setPhpRedisPrefix($connections = ['default'])
|
||||
{
|
||||
$this->originalSettings['redis'] = $this->originalSettings['redis'] ?? [];
|
||||
|
||||
foreach ($connections as $connection) {
|
||||
$prefix = $this->app['config']['tenancy.redis.prefix_base'] . $this->tenant['uuid'];
|
||||
$client = Redis::connection($connection)->client();
|
||||
|
||||
try {
|
||||
$this->originalSettings['redis'][$connection] = $client->getOption($client::OPT_PREFIX);
|
||||
$client->setOption($client::OPT_PREFIX, $prefix);
|
||||
} catch (\Throwable $t) {
|
||||
throw new PhpRedisNotInstalledException();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function resetPhpRedisPrefix($connections = ['default'])
|
||||
{
|
||||
foreach ($connections as $connection) {
|
||||
$client = Redis::connection($connection)->client();
|
||||
|
||||
try {
|
||||
$client->setOption($client::OPT_PREFIX, $this->originalSettings['redis'][$connection]);
|
||||
} catch (\Throwable $t) {
|
||||
throw new PhpRedisNotInstalledException();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function tagCache()
|
||||
{
|
||||
$this->originalSettings['cache'] = $this->app['cache'];
|
||||
$this->app->extend('cache', function () {
|
||||
return new CacheManager($this->app);
|
||||
});
|
||||
}
|
||||
|
||||
public function untagCache()
|
||||
{
|
||||
$this->app->extend('cache', function () {
|
||||
return $this->originalSettings['cache'];
|
||||
});
|
||||
}
|
||||
|
||||
public function suffixFilesystemRootPaths()
|
||||
{
|
||||
$old = $this->originalSettings['storage'] ?? [
|
||||
'disks' => [],
|
||||
'path' => $this->app->storagePath(),
|
||||
];
|
||||
|
||||
$suffix = $this->app['config']['tenancy.filesystem.suffix_base'] . tenant('uuid');
|
||||
|
||||
// storage_path()
|
||||
$this->app->useStoragePath($old['path'] . "/{$suffix}");
|
||||
|
||||
// Storage facade
|
||||
foreach ($this->app['config']['tenancy.filesystem.disks'] as $disk) {
|
||||
$old['disks'][$disk] = Storage::disk($disk)->getAdapter()->getPathPrefix();
|
||||
|
||||
if ($root = \str_replace('%storage_path%', storage_path(), $this->app['config']["tenancy.filesystem.root_override.{$disk}"])) {
|
||||
Storage::disk($disk)->getAdapter()->setPathPrefix($root);
|
||||
} else {
|
||||
$root = $this->app['config']["filesystems.disks.{$disk}.root"];
|
||||
|
||||
Storage::disk($disk)->getAdapter()->setPathPrefix($root . "/{$suffix}");
|
||||
}
|
||||
}
|
||||
|
||||
$this->originalSettings['storage'] = $old;
|
||||
}
|
||||
|
||||
public function resetFilesystemRootPaths()
|
||||
{
|
||||
// storage_path()
|
||||
$this->app->useStoragePath($this->originalSettings['storage']['path']);
|
||||
|
||||
// Storage facade
|
||||
foreach ($this->app['config']['tenancy.filesystem.disks'] as $disk) {
|
||||
Storage::disk($disk)->getAdapter()->setPathPrefix($this->originalSettings['storage']['disks'][$disk]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,129 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Stancl\Tenancy\Traits;
|
||||
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
trait TenantManagerEvents
|
||||
{
|
||||
/**
|
||||
* Event listeners.
|
||||
*
|
||||
* @var callable[][]
|
||||
*/
|
||||
protected $listeners = [
|
||||
'bootstrapping' => [],
|
||||
'bootstrapped' => [],
|
||||
'ending' => [],
|
||||
'ended' => [],
|
||||
];
|
||||
|
||||
/**
|
||||
* Integration listeners.
|
||||
*
|
||||
* @var callable[][]
|
||||
*/
|
||||
protected $integrationListeners = [];
|
||||
|
||||
/**
|
||||
* Register a listener that will be executed before tenancy is bootstrapped.
|
||||
*
|
||||
* @param callable $callback
|
||||
* @return self
|
||||
*/
|
||||
public function bootstrapping(callable $callback)
|
||||
{
|
||||
$this->listeners['bootstrapping'][] = $callback;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a listener that will be executed after tenancy is bootstrapped.
|
||||
*
|
||||
* @param callable $callback
|
||||
* @return self
|
||||
*/
|
||||
public function bootstrapped(callable $callback)
|
||||
{
|
||||
$this->listeners['bootstrapped'][] = $callback;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a listener that will be executed before tenancy is ended.
|
||||
*
|
||||
* @param callable $callback
|
||||
* @return self
|
||||
*/
|
||||
public function ending(callable $callback)
|
||||
{
|
||||
$this->listeners['ending'][] = $callback;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a listener that will be executed after tenancy is ended.
|
||||
*
|
||||
* @param callable $callback
|
||||
* @return self
|
||||
*/
|
||||
public function ended(callable $callback)
|
||||
{
|
||||
$this->listeners['ended'][] = $callback;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fire an event.
|
||||
*
|
||||
* @param string $name Event name
|
||||
* @return Collection Prevented events
|
||||
*/
|
||||
public function event(string $name): Collection
|
||||
{
|
||||
return \array_reduce($this->listeners[$name], function ($prevents, $listener) {
|
||||
return $prevents->merge($listener($this) ?? []);
|
||||
}, collect([]));
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a callback for an integration event.
|
||||
*
|
||||
* @param string $name
|
||||
* @param callable $callback
|
||||
* @return void
|
||||
*/
|
||||
public function integrationEvent(string $name, callable $callback)
|
||||
{
|
||||
if (\array_key_exists($name, $this->integrationListeners)) {
|
||||
$this->integrationListeners[$name][] = $callback;
|
||||
} else {
|
||||
$this->integrationListeners[$name] = [$callback];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return callbacks for an integration event.
|
||||
*
|
||||
* @param string $name
|
||||
* @param mixed $arguments,...
|
||||
* @return callable[]
|
||||
*/
|
||||
public function integration(string $name, ...$arguments)
|
||||
{
|
||||
if ($arguments) {
|
||||
// If $arguments are supplied, execute all listeners with arguments.
|
||||
return \array_reduce($this->integrationListeners[$name] ?? [], function ($tags, $listener) use ($arguments) {
|
||||
return \array_merge($tags, $listener(...$arguments));
|
||||
}, []);
|
||||
}
|
||||
|
||||
return $this->integrationListeners[$name];
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue