Skip to content

Commit

Permalink
Test support for PHP 8 (#986)
Browse files Browse the repository at this point in the history
  • Loading branch information
bkintanar authored Jul 6, 2021
1 parent e5afd7b commit 19a07e7
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 51 deletions.
30 changes: 28 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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"
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
83 changes: 35 additions & 48 deletions src/Generators/Webserver/Database/Drivers/MariaDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,61 +14,55 @@

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
{
/**
* @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
{
Expand All @@ -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);
}
}

0 comments on commit 19a07e7

Please sign in to comment.