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.* 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, ]; 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/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'); }); " 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 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); } diff --git a/src/TenancyServiceProvider.php b/src/TenancyServiceProvider.php index c3bb8a95..14d2871b 100644 --- a/src/TenancyServiceProvider.php +++ b/src/TenancyServiceProvider.php @@ -11,12 +11,55 @@ 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\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. * * @return void */ - public function boot() + public function boot(): void { $this->commands([ 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); - } } 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'))); }