1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-02-05 17:14:03 +00:00

Merge branch '2.x' into migrate-fresh

This commit is contained in:
Chris Brown 2019-09-26 11:44:08 -04:00 committed by GitHub
commit 702df56a3f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 68 additions and 62 deletions

View file

@ -4,6 +4,7 @@
[![Latest Stable Version](https://poser.pugx.org/stancl/tenancy/version)](https://packagist.org/packages/stancl/tenancy) [![Latest Stable Version](https://poser.pugx.org/stancl/tenancy/version)](https://packagist.org/packages/stancl/tenancy)
[![Travis CI build](https://travis-ci.com/stancl/tenancy.svg?branch=2.x)](https://travis-ci.com/stancl/tenancy) [![Travis CI build](https://travis-ci.com/stancl/tenancy.svg?branch=2.x)](https://travis-ci.com/stancl/tenancy)
[![codecov](https://codecov.io/gh/stancl/tenancy/branch/2.x/graph/badge.svg)](https://codecov.io/gh/stancl/tenancy) [![codecov](https://codecov.io/gh/stancl/tenancy/branch/2.x/graph/badge.svg)](https://codecov.io/gh/stancl/tenancy)
[![Donate](https://img.shields.io/badge/Donate-%3C3-red)](https://gumroad.com/l/tenancy)
### *A Laravel multi-database tenancy package that respects your code.* ### *A Laravel multi-database tenancy package that respects your code.*

View file

@ -3,7 +3,7 @@
declare(strict_types=1); declare(strict_types=1);
return [ return [
'storage_driver' => 'Stancl\Tenancy\StorageDrivers\Database\DatabaseStorageDriver', 'storage_driver' => Stancl\Tenancy\StorageDrivers\Database\DatabaseStorageDriver::class,
'storage' => [ 'storage' => [
'db' => [ // Stancl\Tenancy\StorageDrivers\Database\DatabaseStorageDriver 'db' => [ // Stancl\Tenancy\StorageDrivers\Database\DatabaseStorageDriver
'data_column' => 'data', 'data_column' => 'data',
@ -55,9 +55,9 @@ return [
], ],
'database_managers' => [ 'database_managers' => [
// Tenant database managers handle the creation & deletion of tenant databases. // Tenant database managers handle the creation & deletion of tenant databases.
'sqlite' => 'Stancl\Tenancy\TenantDatabaseManagers\SQLiteDatabaseManager', 'sqlite' => Stancl\Tenancy\TenantDatabaseManagers\SQLiteDatabaseManager::class,
'mysql' => 'Stancl\Tenancy\TenantDatabaseManagers\MySQLDatabaseManager', 'mysql' => Stancl\Tenancy\TenantDatabaseManagers\MySQLDatabaseManager::class,
'pgsql' => 'Stancl\Tenancy\TenantDatabaseManagers\PostgreSQLDatabaseManager', 'pgsql' => Stancl\Tenancy\TenantDatabaseManagers\PostgreSQLDatabaseManager::class,
], ],
'database_manager_connections' => [ 'database_manager_connections' => [
// Connections used by TenantDatabaseManagers. This tells, for example, the // Connections used by TenantDatabaseManagers. This tells, for example, the
@ -69,23 +69,23 @@ return [
'bootstrappers' => [ 'bootstrappers' => [
// Tenancy bootstrappers are executed when tenancy is initialized. // Tenancy bootstrappers are executed when tenancy is initialized.
// Their responsibility is making Laravel features tenant-aware. // Their responsibility is making Laravel features tenant-aware.
'database' => 'Stancl\Tenancy\TenancyBootstrappers\DatabaseTenancyBootstrapper', 'database' => Stancl\Tenancy\TenancyBootstrappers\DatabaseTenancyBootstrapper::class,
'cache' => 'Stancl\Tenancy\TenancyBootstrappers\CacheTenancyBootstrapper', 'cache' => Stancl\Tenancy\TenancyBootstrappers\CacheTenancyBootstrapper::class,
'filesystem' => 'Stancl\Tenancy\TenancyBootstrappers\FilesystemTenancyBootstrapper', 'filesystem' => Stancl\Tenancy\TenancyBootstrappers\FilesystemTenancyBootstrapper::class,
'redis' => 'Stancl\Tenancy\TenancyBootstrappers\RedisTenancyBootstrapper', 'redis' => Stancl\Tenancy\TenancyBootstrappers\RedisTenancyBootstrapper::class,
'queue' => 'Stancl\Tenancy\TenancyBootstrappers\QueueTenancyBootstrapper', 'queue' => Stancl\Tenancy\TenancyBootstrappers\QueueTenancyBootstrapper::class,
], ],
'features' => [ 'features' => [
// Features are classes that provide additional functionality // Features are classes that provide additional functionality
// not needed for tenancy to be bootstrapped. They are run // not needed for tenancy to be bootstrapped. They are run
// regardless of whether tenancy has been initialized. // regardless of whether tenancy has been initialized.
'Stancl\Tenancy\Features\TelescopeTags', Stancl\Tenancy\Features\TelescopeTags::class,
'Stancl\Tenancy\Features\TenantRedirect', Stancl\Tenancy\Features\TenantRedirect::class,
], ],
'home_url' => '/app', 'home_url' => '/app',
'migrate_after_creation' => false, // run migrations after creating a tenant 'migrate_after_creation' => false, // run migrations after creating a tenant
'delete_database_after_tenant_deletion' => false, // delete the tenant's database after deleting the tenant 'delete_database_after_tenant_deletion' => false, // delete the tenant's database after deleting the tenant
'queue_database_creation' => false, 'queue_database_creation' => false,
'queue_database_deletion' => false, 'queue_database_deletion' => false,
'unique_id_generator' => 'Stancl\Tenancy\UUIDGenerator', 'unique_id_generator' => Stancl\Tenancy\UUIDGenerator::class,
]; ];

View file

@ -65,7 +65,7 @@ class Install extends Command
| |
*/ */
Route::get('/', function () { Route::get('/app', function () {
return 'This is your multi-tenant application. The id of the current tenant is ' . tenant('id'); return 'This is your multi-tenant application. The id of the current tenant is ' . tenant('id');
}); });
" "

View file

@ -8,6 +8,6 @@ class DatabaseManagerNotRegisteredException extends \Exception
{ {
public function __construct($driver) public function __construct($driver)
{ {
$this->message = "Database manager for driver $driver is not registered."; parent::__construct("Database manager for driver $driver is not registered.");
} }
} }

View file

@ -12,7 +12,7 @@ class TenantCouldNotBeIdentifiedException extends \Exception implements Provides
{ {
public function __construct($domain) public function __construct($domain)
{ {
$this->message = "Tenant could not be identified on domain $domain"; parent::__construct("Tenant could not be identified on domain $domain");
} }
public function getSolution(): Solution public function getSolution(): Solution

View file

@ -5,10 +5,14 @@ declare(strict_types=1);
namespace Stancl\Tenancy\Middleware; namespace Stancl\Tenancy\Middleware;
use Closure; use Closure;
use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedException;
class InitializeTenancy class InitializeTenancy
{ {
public function __construct(Closure $onFail = null) /** @var callable */
protected $onFail;
public function __construct(callable $onFail = null)
{ {
$this->onFail = $onFail ?? function ($e) { $this->onFail = $onFail ?? function ($e) {
throw $e; throw $e;
@ -26,7 +30,7 @@ class InitializeTenancy
{ {
try { try {
tenancy()->init(); tenancy()->init();
} catch (\Exception $e) { } catch (TenantCouldNotBeIdentifiedException $e) {
($this->onFail)($e); ($this->onFail)($e);
} }

View file

@ -11,12 +11,55 @@ use Stancl\Tenancy\TenancyBootstrappers\FilesystemTenancyBootstrapper;
class TenancyServiceProvider extends ServiceProvider class TenancyServiceProvider extends ServiceProvider
{ {
/**
* Register services.
*
* @return void
*/
public function register(): void
{
$this->mergeConfigFrom(__DIR__ . '/../assets/config.php', 'tenancy');
$this->app->bind(Contracts\StorageDriver::class, function ($app) {
return $app->make($app['config']['tenancy.storage_driver']);
});
$this->app->bind(Contracts\UniqueIdentifierGenerator::class, $this->app['config']['tenancy.unique_id_generator']);
$this->app->singleton(DatabaseManager::class);
$this->app->singleton(TenantManager::class);
$this->app->bind(Tenant::class, function ($app) {
return $app[TenantManager::class]->getTenant();
});
foreach ($this->app['config']['tenancy.bootstrappers'] as $bootstrapper) {
$this->app->singleton($bootstrapper);
}
$this->app->singleton(Commands\Migrate::class, function ($app) {
return new Commands\Migrate($app['migrator'], $app[DatabaseManager::class]);
});
$this->app->singleton(Commands\MigrateFresh::class, function ($app) {
return new Commands\MigrateFresh($app['migrator'], $app[DatabaseManager::class]);
});
$this->app->singleton(Commands\Rollback::class, function ($app) {
return new Commands\Rollback($app['migrator'], $app[DatabaseManager::class]);
});
$this->app->singleton(Commands\Seed::class, function ($app) {
return new Commands\Seed($app['db'], $app[DatabaseManager::class]);
});
$this->app->bind('globalCache', function ($app) {
return new CacheManager($app);
});
$this->app->register(TenantRouteServiceProvider::class);
}
/** /**
* Bootstrap services. * Bootstrap services.
* *
* @return void * @return void
*/ */
public function boot() public function boot(): void
{ {
$this->commands([ $this->commands([
Commands\Run::class, Commands\Run::class,
@ -57,46 +100,4 @@ class TenancyServiceProvider extends ServiceProvider
}); });
} }
/**
* Register services.
*
* @return void
*/
public function register()
{
$this->mergeConfigFrom(__DIR__ . '/../assets/config.php', 'tenancy');
$this->app->bind(Contracts\StorageDriver::class, function ($app) {
return $app->make($app['config']['tenancy.storage_driver']);
});
$this->app->bind(Contracts\UniqueIdentifierGenerator::class, $this->app['config']['tenancy.unique_id_generator']);
$this->app->singleton(DatabaseManager::class);
$this->app->singleton(TenantManager::class);
$this->app->bind(Tenant::class, function ($app) {
return $app[TenantManager::class]->getTenant();
});
foreach ($this->app['config']['tenancy.bootstrappers'] as $bootstrapper) {
$this->app->singleton($bootstrapper);
}
$this->app->singleton(Commands\Migrate::class, function ($app) {
return new Commands\Migrate($app['migrator'], $app[DatabaseManager::class]);
});
$this->app->singleton(Commands\MigrateFresh::class, function ($app) {
return new Commands\MigrateFresh($app['migrator'], $app[DatabaseManager::class]);
});
$this->app->singleton(Commands\Rollback::class, function ($app) {
return new Commands\Rollback($app['migrator'], $app[DatabaseManager::class]);
});
$this->app->singleton(Commands\Seed::class, function ($app) {
return new Commands\Seed($app['db'], $app[DatabaseManager::class]);
});
$this->app->bind('globalCache', function ($app) {
return new CacheManager($app);
});
$this->app->register(TenantRouteServiceProvider::class);
}
} }

View file

@ -133,8 +133,8 @@ class CommandsTest extends TestCase
->expectsQuestion('Do you want to publish the default database migrations?', 'yes'); ->expectsQuestion('Do you want to publish the default database migrations?', 'yes');
$this->assertFileExists(base_path('routes/tenant.php')); $this->assertFileExists(base_path('routes/tenant.php'));
$this->assertFileExists(base_path('config/tenancy.php')); $this->assertFileExists(base_path('config/tenancy.php'));
$this->assertFileExists(database_path('migrations/2019_08_08_000000_create_tenants_table.php')); $this->assertFileExists(database_path('migrations/2019_09_15_000010_create_tenants_table.php'));
$this->assertFileExists(database_path('migrations/2019_09_15_000000_create_domains_table.php')); $this->assertFileExists(database_path('migrations/2019_09_15_000020_create_domains_table.php'));
$this->assertDirectoryExists(database_path('migrations/tenant')); $this->assertDirectoryExists(database_path('migrations/tenant'));
$this->assertSame(file_get_contents(__DIR__ . '/Etc/modifiedHttpKernel.stub'), file_get_contents(app_path('Http/Kernel.php'))); $this->assertSame(file_get_contents(__DIR__ . '/Etc/modifiedHttpKernel.stub'), file_get_contents(app_path('Http/Kernel.php')));
} }