From 40bf28c7d0b7abd252f34af00876ada226d398c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Sat, 27 Aug 2022 03:17:16 +0200 Subject: [PATCH] general refactor, move more classes under Database namespace --- assets/config.php | 12 +++---- composer.json | 3 +- .../DatabaseTenancyBootstrapper.php | 2 +- src/CacheManager.php | 2 ++ src/Contracts/TenantWithDatabase.php | 15 --------- src/Controllers/TenantAssetsController.php | 5 +-- .../Concerns/CreatesDatabaseUsers.php | 4 +-- src/Database/Concerns/HasDatabase.php | 4 +-- src/Database/Concerns/HasDomains.php | 1 + .../Contracts/ManagesDatabaseUsers.php | 6 ++-- .../Contracts/TenantDatabaseManager.php | 4 +-- src/Database/Contracts/TenantWithDatabase.php | 13 ++++++++ src/{ => Database}/DatabaseConfig.php | 32 +++++++------------ src/Database/DatabaseManager.php | 14 +++----- .../DatabaseManagerNotRegisteredException.php | 15 +++++++++ .../Exceptions/NoConnectionSetException.php | 2 +- .../TenantDatabaseAlreadyExistsException.php | 2 +- .../TenantDatabaseDoesNotExistException.php | 2 +- ...nantDatabaseUserAlreadyExistsException.php | 2 +- .../MicrosoftSQLDatabaseManager.php | 8 ++--- .../MySQLDatabaseManager.php | 8 ++--- ...rmissionControlledMySQLDatabaseManager.php | 8 ++--- .../PostgreSQLDatabaseManager.php | 8 ++--- .../PostgreSQLSchemaManager.php | 8 ++--- .../SQLiteDatabaseManager.php | 10 +++--- ...SyncedResourceChangedInForeignDatabase.php | 2 +- src/Events/SyncedResourceSaved.php | 2 +- .../DatabaseManagerNotRegisteredException.php | 13 -------- ...enantCouldNotBeIdentifiedByIdException.php | 2 ++ src/Jobs/CreateDatabase.php | 2 +- src/Jobs/DeleteDatabase.php | 2 +- src/Jobs/DeleteDomains.php | 2 +- src/Jobs/MigrateDatabase.php | 2 +- src/Jobs/SeedDatabase.php | 2 +- src/UUIDGenerator.php | 2 ++ tests/DatabasePreparationTest.php | 2 +- tests/DatabaseUsersTest.php | 6 ++-- tests/Etc/Tenant.php | 2 +- tests/ResourceSyncingTest.php | 2 +- tests/TenantDatabaseManagerTest.php | 16 +++++----- tests/TestCase.php | 6 +--- 41 files changed, 122 insertions(+), 133 deletions(-) delete mode 100644 src/Contracts/TenantWithDatabase.php rename src/{ => Database}/Concerns/CreatesDatabaseUsers.php (80%) rename src/{ => Database}/Contracts/ManagesDatabaseUsers.php (74%) rename src/{ => Database}/Contracts/TenantDatabaseManager.php (86%) create mode 100644 src/Database/Contracts/TenantWithDatabase.php rename src/{ => Database}/DatabaseConfig.php (84%) create mode 100644 src/Database/Exceptions/DatabaseManagerNotRegisteredException.php rename src/{ => Database}/Exceptions/NoConnectionSetException.php (83%) rename src/{ => Database}/Exceptions/TenantDatabaseAlreadyExistsException.php (89%) rename src/{ => Database}/Exceptions/TenantDatabaseDoesNotExistException.php (84%) rename src/{ => Database}/Exceptions/TenantDatabaseUserAlreadyExistsException.php (89%) rename src/{ => Database}/TenantDatabaseManagers/MicrosoftSQLDatabaseManager.php (87%) rename src/{ => Database}/TenantDatabaseManagers/MySQLDatabaseManager.php (86%) rename src/{ => Database}/TenantDatabaseManagers/PermissionControlledMySQLDatabaseManager.php (90%) rename src/{ => Database}/TenantDatabaseManagers/PostgreSQLDatabaseManager.php (84%) rename src/{ => Database}/TenantDatabaseManagers/PostgreSQLSchemaManager.php (85%) rename src/{ => Database}/TenantDatabaseManagers/SQLiteDatabaseManager.php (79%) delete mode 100644 src/Exceptions/DatabaseManagerNotRegisteredException.php diff --git a/assets/config.php b/assets/config.php index e1c82e6b..2a54e0b9 100644 --- a/assets/config.php +++ b/assets/config.php @@ -58,22 +58,22 @@ return [ * TenantDatabaseManagers are classes that handle the creation & deletion of tenant databases. */ 'managers' => [ - 'sqlite' => Stancl\Tenancy\TenantDatabaseManagers\SQLiteDatabaseManager::class, - 'mysql' => Stancl\Tenancy\TenantDatabaseManagers\MySQLDatabaseManager::class, - 'pgsql' => Stancl\Tenancy\TenantDatabaseManagers\PostgreSQLDatabaseManager::class, - 'sqlsrv' => Stancl\Tenancy\TenantDatabaseManagers\MicrosoftSQLDatabaseManager::class, + 'sqlite' => Stancl\Tenancy\Database\TenantDatabaseManagers\SQLiteDatabaseManager::class, + 'mysql' => Stancl\Tenancy\Database\TenantDatabaseManagers\MySQLDatabaseManager::class, + 'pgsql' => Stancl\Tenancy\Database\TenantDatabaseManagers\PostgreSQLDatabaseManager::class, + 'sqlsrv' => Stancl\Tenancy\Database\TenantDatabaseManagers\MicrosoftSQLDatabaseManager::class, /** * Use this database manager for MySQL to have a DB user created for each tenant database. * You can customize the grants given to these users by changing the $grants property. */ - // 'mysql' => Stancl\Tenancy\TenantDatabaseManagers\PermissionControlledMySQLDatabaseManager::class, + // 'mysql' => Stancl\Tenancy\Database\TenantDatabaseManagers\PermissionControlledMySQLDatabaseManager::class, /** * Disable the pgsql manager above, and enable the one below if you * want to separate tenant DBs by schemas rather than databases. */ - // 'pgsql' => Stancl\Tenancy\TenantDatabaseManagers\PostgreSQLSchemaManager::class, // Separate by schema instead of database + // 'pgsql' => Stancl\Tenancy\Database\TenantDatabaseManagers\PostgreSQLSchemaManager::class, // Separate by schema instead of database ], ], diff --git a/composer.json b/composer.json index c155743e..36e1c1b3 100644 --- a/composer.json +++ b/composer.json @@ -61,7 +61,8 @@ "docker-rebuild": "PHP_VERSION=8.1 docker-compose up -d --no-deps --build", "docker-m1": "ln -s docker-compose-m1.override.yml docker-compose.override.yml", "coverage": "open coverage/phpunit/html/index.html", - "test": "PHP_VERSION=8.1 ./test" + "test": "PHP_VERSION=8.1 ./test --no-coverage", + "test-full": "PHP_VERSION=8.1 ./test" }, "minimum-stability": "dev", "prefer-stable": true, diff --git a/src/Bootstrappers/DatabaseTenancyBootstrapper.php b/src/Bootstrappers/DatabaseTenancyBootstrapper.php index 59ee0aec..dd94bfd4 100644 --- a/src/Bootstrappers/DatabaseTenancyBootstrapper.php +++ b/src/Bootstrappers/DatabaseTenancyBootstrapper.php @@ -6,7 +6,7 @@ namespace Stancl\Tenancy\Bootstrappers; use Stancl\Tenancy\Contracts\TenancyBootstrapper; use Stancl\Tenancy\Contracts\Tenant; -use Stancl\Tenancy\Contracts\TenantWithDatabase; +use Stancl\Tenancy\Database\Contracts\TenantWithDatabase; use Stancl\Tenancy\Database\DatabaseManager; use Stancl\Tenancy\Exceptions\TenantDatabaseDoesNotExistException; diff --git a/src/CacheManager.php b/src/CacheManager.php index eb6bac4c..f28134b8 100644 --- a/src/CacheManager.php +++ b/src/CacheManager.php @@ -6,6 +6,8 @@ namespace Stancl\Tenancy; use Illuminate\Cache\CacheManager as BaseCacheManager; +// todo move to Cache namespace? + class CacheManager extends BaseCacheManager { /** diff --git a/src/Contracts/TenantWithDatabase.php b/src/Contracts/TenantWithDatabase.php deleted file mode 100644 index 98268502..00000000 --- a/src/Contracts/TenantWithDatabase.php +++ /dev/null @@ -1,15 +0,0 @@ -file(storage_path("app/public/$path")); - } catch (Throwable $th) { + } catch (Throwable) { abort(404); } } diff --git a/src/Concerns/CreatesDatabaseUsers.php b/src/Database/Concerns/CreatesDatabaseUsers.php similarity index 80% rename from src/Concerns/CreatesDatabaseUsers.php rename to src/Database/Concerns/CreatesDatabaseUsers.php index 863dd4a7..f329f071 100644 --- a/src/Concerns/CreatesDatabaseUsers.php +++ b/src/Database/Concerns/CreatesDatabaseUsers.php @@ -2,9 +2,9 @@ declare(strict_types=1); -namespace Stancl\Tenancy\Concerns; +namespace Stancl\Tenancy\Database\Concerns; -use Stancl\Tenancy\Contracts\TenantWithDatabase; +use Stancl\Tenancy\Database\Contracts\TenantWithDatabase; trait CreatesDatabaseUsers { diff --git a/src/Database/Concerns/HasDatabase.php b/src/Database/Concerns/HasDatabase.php index aab381bd..42e6555a 100644 --- a/src/Database/Concerns/HasDatabase.php +++ b/src/Database/Concerns/HasDatabase.php @@ -4,8 +4,8 @@ declare(strict_types=1); namespace Stancl\Tenancy\Database\Concerns; -use Stancl\Tenancy\Contracts\TenantWithDatabase; -use Stancl\Tenancy\DatabaseConfig; +use Stancl\Tenancy\Database\Contracts\TenantWithDatabase; +use Stancl\Tenancy\Database\DatabaseConfig; trait HasDatabase { diff --git a/src/Database/Concerns/HasDomains.php b/src/Database/Concerns/HasDomains.php index 39411fea..a510fc62 100644 --- a/src/Database/Concerns/HasDomains.php +++ b/src/Database/Concerns/HasDomains.php @@ -2,6 +2,7 @@ declare(strict_types=1); +// todo not sure if this should be in Database\ namespace Stancl\Tenancy\Database\Concerns; use Stancl\Tenancy\Contracts\Domain; diff --git a/src/Contracts/ManagesDatabaseUsers.php b/src/Database/Contracts/ManagesDatabaseUsers.php similarity index 74% rename from src/Contracts/ManagesDatabaseUsers.php rename to src/Database/Contracts/ManagesDatabaseUsers.php index d4da1c3c..83b9fad5 100644 --- a/src/Contracts/ManagesDatabaseUsers.php +++ b/src/Database/Contracts/ManagesDatabaseUsers.php @@ -2,11 +2,9 @@ declare(strict_types=1); -namespace Stancl\Tenancy\Contracts; +namespace Stancl\Tenancy\Database\Contracts; -use Stancl\Tenancy\DatabaseConfig; - -// todo possibly move to Database namespace, along with other classes +use Stancl\Tenancy\Database\DatabaseConfig; interface ManagesDatabaseUsers extends TenantDatabaseManager { diff --git a/src/Contracts/TenantDatabaseManager.php b/src/Database/Contracts/TenantDatabaseManager.php similarity index 86% rename from src/Contracts/TenantDatabaseManager.php rename to src/Database/Contracts/TenantDatabaseManager.php index deaecb33..474ee016 100644 --- a/src/Contracts/TenantDatabaseManager.php +++ b/src/Database/Contracts/TenantDatabaseManager.php @@ -2,9 +2,9 @@ declare(strict_types=1); -namespace Stancl\Tenancy\Contracts; +namespace Stancl\Tenancy\Database\Contracts; -use Stancl\Tenancy\Exceptions\NoConnectionSetException; +use Stancl\Tenancy\Database\Exceptions\NoConnectionSetException; interface TenantDatabaseManager { diff --git a/src/Database/Contracts/TenantWithDatabase.php b/src/Database/Contracts/TenantWithDatabase.php new file mode 100644 index 00000000..c9247d94 --- /dev/null +++ b/src/Database/Contracts/TenantWithDatabase.php @@ -0,0 +1,13 @@ +tenant->setInternal('db_name', $this->getName() ?? (static::$databaseNameGenerator)($this->tenant)); - if ($this->manager() instanceof ManagesDatabaseUsers) { + if ($this->manager() instanceof Contracts\ManagesDatabaseUsers) { $this->tenant->setInternal('db_username', $this->getUsername() ?? (static::$usernameGenerator)($this->tenant)); $this->tenant->setInternal('db_password', $this->getPassword() ?? (static::$passwordGenerator)($this->tenant)); } @@ -143,20 +137,18 @@ class DatabaseConfig }, []); } - /** - * Get the TenantDatabaseManager for this tenant's connection. - */ - public function manager(): TenantDatabaseManager + /** Get the TenantDatabaseManager for this tenant's connection. */ + public function manager(): Contracts\TenantDatabaseManager { $driver = config("database.connections.{$this->getTemplateConnectionName()}.driver"); $databaseManagers = config('tenancy.database.managers'); if (! array_key_exists($driver, $databaseManagers)) { - throw new DatabaseManagerNotRegisteredException($driver); + throw new Exceptions\DatabaseManagerNotRegisteredException($driver); } - /** @var TenantDatabaseManager $databaseManager */ + /** @var Contracts\TenantDatabaseManager $databaseManager */ $databaseManager = app($databaseManagers[$driver]); $databaseManager->setConnection($this->getTemplateConnectionName()); diff --git a/src/Database/DatabaseManager.php b/src/Database/DatabaseManager.php index eb807696..00afe938 100644 --- a/src/Database/DatabaseManager.php +++ b/src/Database/DatabaseManager.php @@ -7,14 +7,8 @@ namespace Stancl\Tenancy\Database; use Illuminate\Config\Repository; use Illuminate\Contracts\Foundation\Application; use Illuminate\Database\DatabaseManager as BaseDatabaseManager; -use Stancl\Tenancy\Contracts\ManagesDatabaseUsers; use Stancl\Tenancy\Contracts\TenantCannotBeCreatedException; -use Stancl\Tenancy\Contracts\TenantWithDatabase; -use Stancl\Tenancy\Exceptions\DatabaseManagerNotRegisteredException; -use Stancl\Tenancy\Exceptions\TenantDatabaseAlreadyExistsException; -use Stancl\Tenancy\Exceptions\TenantDatabaseUserAlreadyExistsException; - -// todo move to Database namespace +use Stancl\Tenancy\Database\Contracts\TenantWithDatabase; /** * @internal Class is subject to breaking changes in minor and patch versions. @@ -97,11 +91,11 @@ class DatabaseManager $manager = $tenant->database()->manager(); if ($manager->databaseExists($database = $tenant->database()->getName())) { - throw new TenantDatabaseAlreadyExistsException($database); + throw new Exceptions\TenantDatabaseAlreadyExistsException($database); } - if ($manager instanceof ManagesDatabaseUsers && $manager->userExists($username = $tenant->database()->getUsername())) { - throw new TenantDatabaseUserAlreadyExistsException($username); + if ($manager instanceof Contracts\ManagesDatabaseUsers && $manager->userExists($username = $tenant->database()->getUsername())) { + throw new Exceptions\TenantDatabaseUserAlreadyExistsException($username); } } } diff --git a/src/Database/Exceptions/DatabaseManagerNotRegisteredException.php b/src/Database/Exceptions/DatabaseManagerNotRegisteredException.php new file mode 100644 index 00000000..d499df5d --- /dev/null +++ b/src/Database/Exceptions/DatabaseManagerNotRegisteredException.php @@ -0,0 +1,15 @@ +database()->getName()), ''); - } catch (\Throwable $th) { + } catch (\Throwable) { return false; } } @@ -22,7 +22,7 @@ class SQLiteDatabaseManager implements TenantDatabaseManager { try { return unlink(database_path($tenant->database()->getName())); - } catch (\Throwable $th) { + } catch (\Throwable) { return false; } } diff --git a/src/Events/SyncedResourceChangedInForeignDatabase.php b/src/Events/SyncedResourceChangedInForeignDatabase.php index 1830a5c1..e54ac1b7 100644 --- a/src/Events/SyncedResourceChangedInForeignDatabase.php +++ b/src/Events/SyncedResourceChangedInForeignDatabase.php @@ -5,7 +5,7 @@ declare(strict_types=1); namespace Stancl\Tenancy\Events; use Stancl\Tenancy\Contracts\Syncable; -use Stancl\Tenancy\Contracts\TenantWithDatabase; +use Stancl\Tenancy\Database\Contracts\TenantWithDatabase; class SyncedResourceChangedInForeignDatabase { diff --git a/src/Events/SyncedResourceSaved.php b/src/Events/SyncedResourceSaved.php index 898bac48..23e56e21 100644 --- a/src/Events/SyncedResourceSaved.php +++ b/src/Events/SyncedResourceSaved.php @@ -6,7 +6,7 @@ namespace Stancl\Tenancy\Events; use Illuminate\Database\Eloquent\Model; use Stancl\Tenancy\Contracts\Syncable; -use Stancl\Tenancy\Contracts\TenantWithDatabase; +use Stancl\Tenancy\Database\Contracts\TenantWithDatabase; class SyncedResourceSaved { diff --git a/src/Exceptions/DatabaseManagerNotRegisteredException.php b/src/Exceptions/DatabaseManagerNotRegisteredException.php deleted file mode 100644 index e93c31a1..00000000 --- a/src/Exceptions/DatabaseManagerNotRegisteredException.php +++ /dev/null @@ -1,13 +0,0 @@ - \Stancl\Tenancy\TenantDatabaseManagers\PostgreSQLSchemaManager::class, + 'tenancy.database.managers.pgsql' => \Stancl\Tenancy\Database\TenantDatabaseManagers\PostgreSQLSchemaManager::class, 'tenancy.boostrappers' => [ DatabaseTenancyBootstrapper::class, ], diff --git a/tests/TestCase.php b/tests/TestCase.php index 554aeb8d..1c6c6d8a 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -48,11 +48,7 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase protected function getEnvironmentSetUp($app) { if (file_exists(__DIR__ . '/../.env')) { - if (method_exists(\Dotenv\Dotenv::class, 'createImmutable')) { - \Dotenv\Dotenv::createImmutable(__DIR__ . '/..')->load(); - } else { - \Dotenv\Dotenv::create(__DIR__ . '/..')->load(); - } + \Dotenv\Dotenv::createImmutable(__DIR__ . '/..')->load(); } $app['config']->set([