forked from archtechx/tenancy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tenant contract, Tenancy bootstrappers, drop predis
- Loading branch information
Showing
13 changed files
with
158 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?php | ||
|
||
namespace Stancl\Tenancy\Contracts; | ||
|
||
/** Empty interface implemented by Stancl\Tenancy\Tenant have a dependency-injectable contract for the current tenant. */ | ||
interface Tenant {} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace Stancl\Tenancy\TenancyBoostrappers; | ||
|
||
use Stancl\Tenancy\Contracts\TenancyBootstrapper; | ||
|
||
class CacheTenancyBootstrapped implements TenancyBootstrapper | ||
{ | ||
/** @var \Illuminate\Cache\CacheManager */ | ||
protected $originalCache; | ||
|
||
public function start() | ||
{ | ||
$this->originalCache = $this->originalCache ?? $this->app['cache']; | ||
$this->app->extend('cache', function () { | ||
return new CacheManager($this->app); | ||
}); | ||
} | ||
|
||
public function end() | ||
{ | ||
$this->app->extend('cache', function () { | ||
return $this->originalCache; | ||
}); | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
src/TenancyBootstrappers/FilesystemTenancyBootstrapper.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
namespace Stancl\Tenancy\TenancyBoostrappers; | ||
|
||
use Stancl\Tenancy\Contracts\TenancyBootstrapper; | ||
|
||
// todo better solution than tenant_asset? | ||
class FilesystemTenancyBootstrapper implements TenancyBootstrapper | ||
{ | ||
protected $originalPaths = []; | ||
|
||
/** @var Application */ | ||
protected $app; | ||
|
||
public function __construct(Application $app) | ||
{ | ||
$this->app = $app; | ||
$this->originalPaths = [ | ||
'disks' => [], | ||
'path' => $this->app->storagePath(), | ||
]; | ||
} | ||
|
||
public function start() | ||
{ | ||
// todo revisit this | ||
$suffix = $this->app['config']['tenancy.filesystem.suffix_base'] . tenant('uuid'); | ||
|
||
// storage_path() | ||
$this->app->useStoragePath($this->originalPaths['path'] . "/{$suffix}"); | ||
|
||
// Storage facade | ||
foreach ($this->app['config']['tenancy.filesystem.disks'] as $disk) { | ||
$this->originalPaths['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}"); | ||
} | ||
} | ||
} | ||
|
||
public function end() | ||
{ | ||
// storage_path() | ||
$this->app->useStoragePath($this->originalPaths['path']); | ||
|
||
// Storage facade | ||
foreach ($this->app['config']['tenancy.filesystem.disks'] as $disk) { | ||
Storage::disk($disk)->getAdapter()->setPathPrefix($this->originalPaths['disks'][$disk]); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
namespace Stancl\Tenancy\TenantDatabaseManagers; | ||
|
||
use Stancl\Tenancy\Contracts\TenancyBootstrapper; | ||
|
||
class RedisTenancyBootstrapper implements TenancyBootstrapper | ||
{ | ||
/** @var string[string] Original prefixes of connections */ | ||
protected $originalPrefixes = []; | ||
|
||
/** @var Application */ | ||
protected $app; | ||
|
||
public function __construct(Application $app) | ||
{ | ||
$this->app = $app; | ||
} | ||
|
||
public function start() | ||
{ | ||
foreach ($this->prefixedConnections() as $connection) { | ||
$prefix = $this->app['config']['tenancy.redis.prefix_base'] . $this->tenant['uuid']; | ||
$client = Redis::connection($connection)->client(); | ||
|
||
$this->originalPrefixes[$connection] = $client->getOption($client::OPT_PREFIX); | ||
$client->setOption($client::OPT_PREFIX, $prefix); | ||
} | ||
} | ||
|
||
public function end() | ||
{ | ||
foreach ($this->prefixedConnections() as $connection) { | ||
$client = Redis::connection($connection)->client(); | ||
|
||
$client->setOption($client::OPT_PREFIX, $this->originalPrefixes[$connection]); | ||
} | ||
} | ||
|
||
protected function prefixedConnections() | ||
{ | ||
return config('tenancy.redis.prefixed_connections'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters