From ca8baf9a6d06de232c9e8b85a2284b6ce01384bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Tue, 24 Sep 2019 17:44:56 +0200 Subject: [PATCH 1/7] Add donate button --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 587aab45..0a62ccdf 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,7 @@ [![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) [![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.* From a166de2ef63de0a56b42a14cf33bce717bb659ec Mon Sep 17 00:00:00 2001 From: Chris Brown Date: Wed, 25 Sep 2019 02:07:01 -0400 Subject: [PATCH 2/7] Rename migration stubs so they show next to each other (#127) --- ...s_table.php => 2019_09_15_000010_create_tenants_table.php} | 0 ...s_table.php => 2019_09_15_000020_create_domains_table.php} | 0 tests/CommandsTest.php | 4 ++-- 3 files changed, 2 insertions(+), 2 deletions(-) rename assets/migrations/{2019_08_08_000000_create_tenants_table.php => 2019_09_15_000010_create_tenants_table.php} (100%) rename assets/migrations/{2019_09_15_000000_create_domains_table.php => 2019_09_15_000020_create_domains_table.php} (100%) diff --git a/assets/migrations/2019_08_08_000000_create_tenants_table.php b/assets/migrations/2019_09_15_000010_create_tenants_table.php similarity index 100% rename from assets/migrations/2019_08_08_000000_create_tenants_table.php rename to assets/migrations/2019_09_15_000010_create_tenants_table.php diff --git a/assets/migrations/2019_09_15_000000_create_domains_table.php b/assets/migrations/2019_09_15_000020_create_domains_table.php similarity index 100% rename from assets/migrations/2019_09_15_000000_create_domains_table.php rename to assets/migrations/2019_09_15_000020_create_domains_table.php diff --git a/tests/CommandsTest.php b/tests/CommandsTest.php index ae35888d..d7ffb3cd 100644 --- a/tests/CommandsTest.php +++ b/tests/CommandsTest.php @@ -133,8 +133,8 @@ class CommandsTest extends TestCase ->expectsQuestion('Do you want to publish the default database migrations?', 'yes'); $this->assertFileExists(base_path('routes/tenant.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_000000_create_domains_table.php')); + $this->assertFileExists(database_path('migrations/2019_09_15_000010_create_tenants_table.php')); + $this->assertFileExists(database_path('migrations/2019_09_15_000020_create_domains_table.php')); $this->assertDirectoryExists(database_path('migrations/tenant')); $this->assertSame(file_get_contents(__DIR__ . '/Etc/modifiedHttpKernel.stub'), file_get_contents(app_path('Http/Kernel.php'))); } From 5b154d1834a4bf634c78350591764fca416910e1 Mon Sep 17 00:00:00 2001 From: Anton Komarev <1849174+antonkomarev@users.noreply.github.com> Date: Thu, 26 Sep 2019 07:25:07 +0300 Subject: [PATCH 3/7] Fix default tenant route (#132) --- src/Commands/Install.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Commands/Install.php b/src/Commands/Install.php index 39ed31d1..136a190e 100644 --- a/src/Commands/Install.php +++ b/src/Commands/Install.php @@ -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'); }); " From fc39512b3bf809756d1c2c3e081c4aa38edf0929 Mon Sep 17 00:00:00 2001 From: Anton Komarev <1849174+antonkomarev@users.noreply.github.com> Date: Thu, 26 Sep 2019 15:33:29 +0300 Subject: [PATCH 4/7] Use ::class instead of strings in config (#133) --- assets/config.php | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/assets/config.php b/assets/config.php index bee7384c..538759ec 100644 --- a/assets/config.php +++ b/assets/config.php @@ -3,7 +3,7 @@ declare(strict_types=1); return [ - 'storage_driver' => 'Stancl\Tenancy\StorageDrivers\Database\DatabaseStorageDriver', + 'storage_driver' => Stancl\Tenancy\StorageDrivers\Database\DatabaseStorageDriver::class, 'storage' => [ 'db' => [ // Stancl\Tenancy\StorageDrivers\Database\DatabaseStorageDriver 'data_column' => 'data', @@ -55,9 +55,9 @@ return [ ], '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', + 'sqlite' => Stancl\Tenancy\TenantDatabaseManagers\SQLiteDatabaseManager::class, + 'mysql' => Stancl\Tenancy\TenantDatabaseManagers\MySQLDatabaseManager::class, + 'pgsql' => Stancl\Tenancy\TenantDatabaseManagers\PostgreSQLDatabaseManager::class, ], 'database_manager_connections' => [ // Connections used by TenantDatabaseManagers. This tells, for example, the @@ -69,23 +69,23 @@ return [ 'bootstrappers' => [ // Tenancy bootstrappers are executed when tenancy is initialized. // Their responsibility is making Laravel features tenant-aware. - 'database' => 'Stancl\Tenancy\TenancyBootstrappers\DatabaseTenancyBootstrapper', - 'cache' => 'Stancl\Tenancy\TenancyBootstrappers\CacheTenancyBootstrapper', - 'filesystem' => 'Stancl\Tenancy\TenancyBootstrappers\FilesystemTenancyBootstrapper', - 'redis' => 'Stancl\Tenancy\TenancyBootstrappers\RedisTenancyBootstrapper', - 'queue' => 'Stancl\Tenancy\TenancyBootstrappers\QueueTenancyBootstrapper', + 'database' => Stancl\Tenancy\TenancyBootstrappers\DatabaseTenancyBootstrapper::class, + 'cache' => Stancl\Tenancy\TenancyBootstrappers\CacheTenancyBootstrapper::class, + 'filesystem' => Stancl\Tenancy\TenancyBootstrappers\FilesystemTenancyBootstrapper::class, + 'redis' => Stancl\Tenancy\TenancyBootstrappers\RedisTenancyBootstrapper::class, + 'queue' => Stancl\Tenancy\TenancyBootstrappers\QueueTenancyBootstrapper::class, ], 'features' => [ // Features are classes that provide additional functionality // not needed for tenancy to be bootstrapped. They are run // regardless of whether tenancy has been initialized. - 'Stancl\Tenancy\Features\TelescopeTags', - 'Stancl\Tenancy\Features\TenantRedirect', + Stancl\Tenancy\Features\TelescopeTags::class, + Stancl\Tenancy\Features\TenantRedirect::class, ], 'home_url' => '/app', '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 'queue_database_creation' => false, 'queue_database_deletion' => false, - 'unique_id_generator' => 'Stancl\Tenancy\UUIDGenerator', + 'unique_id_generator' => Stancl\Tenancy\UUIDGenerator::class, ]; From 0081726bda333d2c240605ba8f7832276a98bbed Mon Sep 17 00:00:00 2001 From: Anton Komarev <1849174+antonkomarev@users.noreply.github.com> Date: Thu, 26 Sep 2019 15:35:02 +0300 Subject: [PATCH 5/7] Add exceptions missing parent constructor call (#137) --- src/Exceptions/DatabaseManagerNotRegisteredException.php | 2 +- src/Exceptions/TenantCouldNotBeIdentifiedException.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Exceptions/DatabaseManagerNotRegisteredException.php b/src/Exceptions/DatabaseManagerNotRegisteredException.php index 8af2f5e1..e93c31a1 100644 --- a/src/Exceptions/DatabaseManagerNotRegisteredException.php +++ b/src/Exceptions/DatabaseManagerNotRegisteredException.php @@ -8,6 +8,6 @@ class DatabaseManagerNotRegisteredException extends \Exception { public function __construct($driver) { - $this->message = "Database manager for driver $driver is not registered."; + parent::__construct("Database manager for driver $driver is not registered."); } } diff --git a/src/Exceptions/TenantCouldNotBeIdentifiedException.php b/src/Exceptions/TenantCouldNotBeIdentifiedException.php index a25d8ddb..fbebcd10 100644 --- a/src/Exceptions/TenantCouldNotBeIdentifiedException.php +++ b/src/Exceptions/TenantCouldNotBeIdentifiedException.php @@ -12,7 +12,7 @@ class TenantCouldNotBeIdentifiedException extends \Exception implements Provides { 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 From d00cf97e0ee0433afae3a627c03163fe45cefc22 Mon Sep 17 00:00:00 2001 From: Anton Komarev <1849174+antonkomarev@users.noreply.github.com> Date: Thu, 26 Sep 2019 15:35:41 +0300 Subject: [PATCH 6/7] Reorder Service Provider methods in execution sequence (#139) --- src/TenancyServiceProvider.php | 82 +++++++++++++++++----------------- 1 file changed, 41 insertions(+), 41 deletions(-) diff --git a/src/TenancyServiceProvider.php b/src/TenancyServiceProvider.php index e93c2d08..c70c5418 100644 --- a/src/TenancyServiceProvider.php +++ b/src/TenancyServiceProvider.php @@ -11,12 +11,52 @@ use Stancl\Tenancy\TenancyBootstrappers\FilesystemTenancyBootstrapper; 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\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. * * @return void */ - public function boot() + public function boot(): void { $this->commands([ Commands\Run::class, @@ -55,44 +95,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\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); - } } From 3f4018f93c94788b6c1f87fde65c843ebf053133 Mon Sep 17 00:00:00 2001 From: Anton Komarev <1849174+antonkomarev@users.noreply.github.com> Date: Thu, 26 Sep 2019 18:01:38 +0300 Subject: [PATCH 7/7] [2.x] Add missing field in InitializeTenancy middleware (#135) * Add missing field * Make field protected * Specify exact exception type * Changed Closure on callable --- src/Middleware/InitializeTenancy.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/Middleware/InitializeTenancy.php b/src/Middleware/InitializeTenancy.php index 3b488f90..4303ab39 100644 --- a/src/Middleware/InitializeTenancy.php +++ b/src/Middleware/InitializeTenancy.php @@ -5,10 +5,14 @@ declare(strict_types=1); namespace Stancl\Tenancy\Middleware; use Closure; +use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedException; class InitializeTenancy { - public function __construct(Closure $onFail = null) + /** @var callable */ + protected $onFail; + + public function __construct(callable $onFail = null) { $this->onFail = $onFail ?? function ($e) { throw $e; @@ -26,7 +30,7 @@ class InitializeTenancy { try { tenancy()->init(); - } catch (\Exception $e) { + } catch (TenantCouldNotBeIdentifiedException $e) { ($this->onFail)($e); }