From 19a07e78b6ab46ceb03bb03a2efff04f4b47bff8 Mon Sep 17 00:00:00 2001 From: Bertrand Kintanar Date: Tue, 6 Jul 2021 23:28:47 +0800 Subject: [PATCH] Test support for PHP 8 (#986) --- .circleci/config.yml | 30 ++++++- composer.json | 2 +- .../Webserver/Database/Drivers/MariaDB.php | 83 ++++++++----------- 3 files changed, 64 insertions(+), 51 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 0284bbed..ef8b7f77 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -99,7 +99,7 @@ jobs: <<: *steps "php-7.4-apache": docker: - - image: php:7.3-apache + - image: php:7.4-apache environment: *environment - *mysql - *mariadb @@ -110,7 +110,29 @@ jobs: <<: *steps "php-7.4-nginx": docker: - - image: php:7.3-fpm + - image: php:7.4-fpm + environment: *environment + - *mysql + - *mariadb + - *pgsql + - *mysql2 + - *mariadb2 + - *pgsql2 + <<: *steps + "php-8.0-apache": + docker: + - image: php:8.0-apache + environment: *environment + - *mysql + - *mariadb + - *pgsql + - *mysql2 + - *mariadb2 + - *pgsql2 + <<: *steps + "php-8.0-nginx": + docker: + - image: php:8.0-fpm environment: *environment - *mysql - *mariadb @@ -155,6 +177,8 @@ workflows: - "php-7.3-nginx" - "php-7.4-apache" - "php-7.4-nginx" + - "php-8.0-apache" + - "php-8.0-nginx" - coverage: filters: branches: @@ -164,3 +188,5 @@ workflows: - "php-7.3-nginx" - "php-7.4-apache" - "php-7.4-nginx" + - "php-8.0-apache" + - "php-8.0-nginx" diff --git a/composer.json b/composer.json index 2026ab45..104439bc 100644 --- a/composer.json +++ b/composer.json @@ -23,7 +23,7 @@ "docs": "https://tenancy.dev" }, "require": { - "php": "^7.3", + "php": "^7.3|^8.0", "phpseclib/phpseclib": "~2.0", "doctrine/dbal": "~2.5|~3.0", "ramsey/uuid": "^4.0", diff --git a/src/Generators/Webserver/Database/Drivers/MariaDB.php b/src/Generators/Webserver/Database/Drivers/MariaDB.php index 22fb2c7f..65d5fd1b 100644 --- a/src/Generators/Webserver/Database/Drivers/MariaDB.php +++ b/src/Generators/Webserver/Database/Drivers/MariaDB.php @@ -14,16 +14,14 @@ namespace Hyn\Tenancy\Generators\Webserver\Database\Drivers; -use Hyn\Tenancy\Contracts\Webserver\DatabaseGenerator; +use Illuminate\Support\Arr; use Hyn\Tenancy\Contracts\Website; use Hyn\Tenancy\Database\Connection; use Hyn\Tenancy\Events\Websites\Created; use Hyn\Tenancy\Events\Websites\Deleted; use Hyn\Tenancy\Events\Websites\Updated; use Hyn\Tenancy\Exceptions\GeneratorFailedException; -use Illuminate\Database\Connection as IlluminateConnection; -use Illuminate\Support\Arr; -use Illuminate\Support\Str; +use Hyn\Tenancy\Contracts\Webserver\DatabaseGenerator; class MariaDB implements DatabaseGenerator { @@ -31,44 +29,40 @@ class MariaDB implements DatabaseGenerator * @param Created $event * @param array $config * @param Connection $connection + * * @return bool */ public function created(Created $event, array $config, Connection $connection): bool { $createUser = config('tenancy.db.auto-create-tenant-database-user', true); - $user = function ($connection) use ($config, $createUser) { - if ($createUser) { - return $connection->statement("CREATE USER IF NOT EXISTS `{$config['username']}`@'{$config['host']}' IDENTIFIED BY '{$config['password']}'"); - } + $user = "CREATE USER IF NOT EXISTS `{$config['username']}`@'{$config['host']}' IDENTIFIED BY '{$config['password']}'"; - return true; - }; - $create = function ($connection) use ($config) { - return $connection->statement("CREATE DATABASE IF NOT EXISTS `{$config['database']}` + $create = "CREATE DATABASE IF NOT EXISTS `{$config['database']}` DEFAULT CHARACTER SET {$config['charset']} - DEFAULT COLLATE {$config['collation']}"); - }; - $grant = function ($connection) use ($config, $createUser) { - if ($createUser) { - $privileges = config('tenancy.db.tenant-database-user-privileges', null) ?? 'ALL'; - return $connection->statement("GRANT $privileges ON `{$config['database']}`.* TO `{$config['username']}`@'{$config['host']}'"); - } - - return true; - }; - - return $connection->system($event->website)->transaction(function (IlluminateConnection $connection) use ($user, $create, $grant) { - return $user($connection) && $create($connection) && $grant($connection); - }); + DEFAULT COLLATE {$config['collation']}"; + + $privileges = config('tenancy.db.tenant-database-user-privileges', null) ?? 'ALL'; + + $grant = "GRANT $privileges ON `{$config['database']}`.* TO `{$config['username']}`@'{$config['host']}'"; + + if ($createUser) { + return $connection->system($event->website)->statement($user) + && $connection->system($event->website)->statement($create) + && $connection->system($event->website)->statement($grant); + } + + return $connection->system($event->website)->statement($create); } /** * @param Updated $event * @param array $config * @param Connection $connection - * @return bool + * * @throws GeneratorFailedException + * + * @return bool */ public function updated(Updated $event, array $config, Connection $connection): bool { @@ -87,39 +81,32 @@ public function updated(Updated $event, array $config, Connection $connection): * @param Deleted $event * @param array $config * @param Connection $connection + * * @return bool */ public function deleted(Deleted $event, array $config, Connection $connection): bool { - $user = function ($connection) use ($config) { - if (config('tenancy.db.auto-delete-tenant-database-user', false)) { - return $connection->statement("DROP USER IF EXISTS `{$config['username']}`@'{$config['host']}'"); - } + $deleteUser = config('tenancy.db.auto-delete-tenant-database-user', false); + + $user = "DROP USER IF EXISTS `{$config['username']}`@'{$config['host']}'"; - return true; - }; + $delete = "DROP DATABASE IF EXISTS `{$config['database']}`"; - $delete = function ($connection) use ($config) { - return $connection->statement("DROP DATABASE IF EXISTS `{$config['database']}`"); - }; + if ($deleteUser) { + return $connection->system($event->website)->statement($user) + && $connection->system($event->website)->statement($delete); + } - return $connection->system($event->website)->transaction(function (IlluminateConnection $connection) use ($user, $delete) { - return $delete($connection) && $user($connection); - }); + return $connection->system($event->website)->statement($delete); } public function updatePassword(Website $website, array $config, Connection $connection): bool { - $user = function (IlluminateConnection $connection) use ($config) { - return $connection->statement("ALTER USER `{$config['username']}`@'{$config['host']}' IDENTIFIED BY '{$config['password']}'"); - }; + $user = "ALTER USER `{$config['username']}`@'{$config['host']}' IDENTIFIED BY '{$config['password']}'"; - $flush = function (IlluminateConnection $connection) { - return $connection->statement('FLUSH PRIVILEGES'); - }; + $flush = 'FLUSH PRIVILEGES'; - return $connection->system($website)->transaction(function (IlluminateConnection $connection) use ($user, $flush) { - return $user($connection) && $flush($connection); - }); + return $connection->system($website)->statement($user) + && $connection->system($website)->statement($flush); } }