Skip to content

Commit

Permalink
Tenant contract, Tenancy bootstrappers, drop predis
Browse files Browse the repository at this point in the history
  • Loading branch information
stancl committed Sep 6, 2019
1 parent 98cb49b commit f04ca34
Show file tree
Hide file tree
Showing 13 changed files with 158 additions and 47 deletions.
9 changes: 6 additions & 3 deletions assets/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,10 @@
'suffix' => '',
],
'redis' => [
'tenancy' => false, // to enable Redis tenancy, you must use phpredis
'prefix_base' => 'tenant',
'prefixed_connections' => [
'default',
'cache',
// 'default',
// 'cache',
],
],
'cache' => [
Expand All @@ -51,10 +50,14 @@
],
],
'database_managers' => [
// Tenant database managers handle the creation & deletion of tenant databases.
'sqlite' => 'Stancl\Tenancy\TenantDatabaseManagers\SQLiteDatabaseManager',
'mysql' => 'Stancl\Tenancy\TenantDatabaseManagers\MySQLDatabaseManager',
'pgsql' => 'Stancl\Tenancy\TenantDatabaseManagers\PostgreSQLDatabaseManager',
],
'tenancy_bootstrappers' => [
''
],
'queue_database_creation' => false,
'queue_database_deletion' => false,
'unique_id_generator' => 'Stancl\Tenancy\UUIDGenerator',
Expand Down
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
],
"require": {
"illuminate/support": "5.8.*",
"webpatser/laravel-uuid": "^3.0",
"predis/predis": "^1.1"
"webpatser/laravel-uuid": "^3.0"
},
"require-dev": {
"vlucas/phpdotenv": "^3.3",
Expand Down
6 changes: 6 additions & 0 deletions src/Contracts/Tenant.php
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 {}
10 changes: 0 additions & 10 deletions src/Exceptions/PhpRedisNotInstalledException.php

This file was deleted.

26 changes: 26 additions & 0 deletions src/TenancyBootstrappers/CacheTenancyBoostrapper.php
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 src/TenancyBootstrappers/FilesystemTenancyBootstrapper.php
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]);
}
}
}
44 changes: 44 additions & 0 deletions src/TenancyBootstrappers/RedisTenancyBootstrapper.php
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');
}
}
2 changes: 2 additions & 0 deletions src/TenancyServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ public function register()
);
});

// todo foreach bootstrappers, singleton

$this->app->singleton(Migrate::class, function ($app) {
return new Migrate($app['migrator'], $app[DatabaseManager::class]);
});
Expand Down
6 changes: 2 additions & 4 deletions src/Tenant.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@

use ArrayAccess;

// todo laravel events instead of custom events?
/**
* @internal Class is subject to breaking changes in minor and patch versions.
*/
class Tenant implements ArrayAccess
class Tenant implements ArrayAccess, Contracts\Tenant
{
use Traits\HasArrayAccess;

Expand Down Expand Up @@ -56,7 +54,7 @@ public static function fromStorage(array $data): self
return app(static::class)->withData($data)->persisted();
}

public function persisted()
protected function persisted()
{
$this->persisted = true;

Expand Down
11 changes: 10 additions & 1 deletion src/TenantManagerv2.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,16 @@ public function initializeTenancy(Tenant $tenant): self
public function bootstrapTenancy(Tenant $tenant): self
{
foreach($this->tenancyBootstrappers() as $bootstrapper) {
$bootstrapper::start($tenant);
$this->app[$bootstrapper]->start($tenant);
}

return $this;
}

public function endTenancy(): self
{
foreach($this->tenancyBootstrappers() as $bootstrapper) {
$this->app[$bootstrapper]->end();
}

return $this;
Expand Down
2 changes: 2 additions & 0 deletions src/TenantModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

use Illuminate\Database\Eloquent\Model;

// todo move this to a database driver domain?
/**
* @final Class is subject to breaking changes in minor and patch versions.
*/
Expand Down
6 changes: 2 additions & 4 deletions test
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ set -e
# for development
docker-compose up -d
printf "Variant 1\n\n"
docker-compose exec test env TENANCY_TEST_REDIS_TENANCY=1 TENANCY_TEST_REDIS_CLIENT=phpredis TENANCY_TEST_STORAGE_DRIVER=redis vendor/bin/phpunit --coverage-php coverage/1.cov "$@"
docker-compose exec test env TENANCY_TEST_STORAGE_DRIVER=redis vendor/bin/phpunit --coverage-php coverage/1.cov "$@"
printf "Variant 2\n\n"
docker-compose exec test env TENANCY_TEST_REDIS_TENANCY=0 TENANCY_TEST_REDIS_CLIENT=predis TENANCY_TEST_STORAGE_DRIVER=redis vendor/bin/phpunit --coverage-php coverage/2.cov "$@"
printf "Variant 3\n\n"
docker-compose exec test env TENANCY_TEST_REDIS_TENANCY=1 TENANCY_TEST_REDIS_CLIENT=phpredis TENANCY_TEST_STORAGE_DRIVER=db vendor/bin/phpunit --coverage-php coverage/3.cov "$@"
docker-compose exec test env TENANCY_TEST_STORAGE_DRIVER=db vendor/bin/phpunit --coverage-php coverage/3.cov "$@"
docker-compose exec test vendor/bin/phpcov merge --clover clover.xml coverage/
23 changes: 0 additions & 23 deletions tests/BootstrapsTenancyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Config;
use Stancl\Tenancy\Exceptions\PhpRedisNotInstalledException;

class BootstrapsTenancyTest extends TestCase
{
Expand Down Expand Up @@ -38,28 +37,6 @@ public function redis_is_prefixed()
}
}

/** @test */
public function predis_is_supported()
{
Config::set('database.redis.client', 'predis');
Redis::setDriver('predis');
Config::set('tenancy.redis.tenancy', false);

// assert no exception is thrown from initializing tenancy
$this->assertNotNull($this->initTenancy());
}

/** @test */
public function predis_is_not_supported_without_disabling_redis_multitenancy()
{
Config::set('database.redis.client', 'predis');
Redis::setDriver('predis');
Config::set('tenancy.redis.tenancy', true);

$this->expectException(PhpRedisNotInstalledException::class);
$this->initTenancy();
}

/** @test */
public function filesystem_is_suffixed()
{
Expand Down

0 comments on commit f04ca34

Please sign in to comment.