diff --git a/src/Traits/BootstrapsTenancy.php b/src/Traits/BootstrapsTenancy.php deleted file mode 100644 index 84e2f724..00000000 --- a/src/Traits/BootstrapsTenancy.php +++ /dev/null @@ -1,165 +0,0 @@ -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]); - } - } -} diff --git a/src/Traits/TenantManagerEvents.php b/src/Traits/TenantManagerEvents.php deleted file mode 100644 index d10ece26..00000000 --- a/src/Traits/TenantManagerEvents.php +++ /dev/null @@ -1,129 +0,0 @@ - [], - '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]; - } -}