14
14
15
15
namespace Hyn \Tenancy \Generators \Webserver \Database \Drivers ;
16
16
17
- use Hyn \ Tenancy \ Contracts \ Webserver \ DatabaseGenerator ;
17
+ use Illuminate \ Support \ Arr ;
18
18
use Hyn \Tenancy \Contracts \Website ;
19
19
use Hyn \Tenancy \Database \Connection ;
20
20
use Hyn \Tenancy \Events \Websites \Created ;
21
21
use Hyn \Tenancy \Events \Websites \Deleted ;
22
22
use Hyn \Tenancy \Events \Websites \Updated ;
23
23
use Hyn \Tenancy \Exceptions \GeneratorFailedException ;
24
- use Illuminate \Database \Connection as IlluminateConnection ;
25
- use Illuminate \Support \Arr ;
26
- use Illuminate \Support \Str ;
24
+ use Hyn \Tenancy \Contracts \Webserver \DatabaseGenerator ;
27
25
28
26
class MariaDB implements DatabaseGenerator
29
27
{
30
28
/**
31
29
* @param Created $event
32
30
* @param array $config
33
31
* @param Connection $connection
32
+ *
34
33
* @return bool
35
34
*/
36
35
public function created (Created $ event , array $ config , Connection $ connection ): bool
37
36
{
38
37
$ createUser = config ('tenancy.db.auto-create-tenant-database-user ' , true );
39
38
40
- $ user = function ($ connection ) use ($ config , $ createUser ) {
41
- if ($ createUser ) {
42
- return $ connection ->statement ("CREATE USER IF NOT EXISTS ` {$ config ['username ' ]}`@' {$ config ['host ' ]}' IDENTIFIED BY ' {$ config ['password ' ]}' " );
43
- }
39
+ $ user = "CREATE USER IF NOT EXISTS ` {$ config ['username ' ]}`@' {$ config ['host ' ]}' IDENTIFIED BY ' {$ config ['password ' ]}' " ;
44
40
45
- return true ;
46
- };
47
- $ create = function ($ connection ) use ($ config ) {
48
- return $ connection ->statement ("CREATE DATABASE IF NOT EXISTS ` {$ config ['database ' ]}`
41
+ $ create = "CREATE DATABASE IF NOT EXISTS ` {$ config ['database ' ]}`
49
42
DEFAULT CHARACTER SET {$ config ['charset ' ]}
50
- DEFAULT COLLATE {$ config ['collation ' ]}" );
51
- };
52
- $ grant = function ($ connection ) use ($ config , $ createUser ) {
53
- if ($ createUser ) {
54
- $ privileges = config ('tenancy.db.tenant-database-user-privileges ' , null ) ?? 'ALL ' ;
55
- return $ connection ->statement ("GRANT $ privileges ON ` {$ config ['database ' ]}`.* TO ` {$ config ['username ' ]}`@' {$ config ['host ' ]}' " );
56
- }
57
-
58
- return true ;
59
- };
60
-
61
- return $ connection ->system ($ event ->website )->transaction (function (IlluminateConnection $ connection ) use ($ user , $ create , $ grant ) {
62
- return $ user ($ connection ) && $ create ($ connection ) && $ grant ($ connection );
63
- });
43
+ DEFAULT COLLATE {$ config ['collation ' ]}" ;
44
+
45
+ $ privileges = config ('tenancy.db.tenant-database-user-privileges ' , null ) ?? 'ALL ' ;
46
+
47
+ $ grant = "GRANT $ privileges ON ` {$ config ['database ' ]}`.* TO ` {$ config ['username ' ]}`@' {$ config ['host ' ]}' " ;
48
+
49
+ if ($ createUser ) {
50
+ return $ connection ->system ($ event ->website )->statement ($ user )
51
+ && $ connection ->system ($ event ->website )->statement ($ create )
52
+ && $ connection ->system ($ event ->website )->statement ($ grant );
53
+ }
54
+
55
+ return $ connection ->system ($ event ->website )->statement ($ create );
64
56
}
65
57
66
58
/**
67
59
* @param Updated $event
68
60
* @param array $config
69
61
* @param Connection $connection
70
- * @return bool
62
+ *
71
63
* @throws GeneratorFailedException
64
+ *
65
+ * @return bool
72
66
*/
73
67
public function updated (Updated $ event , array $ config , Connection $ connection ): bool
74
68
{
@@ -87,39 +81,32 @@ public function updated(Updated $event, array $config, Connection $connection):
87
81
* @param Deleted $event
88
82
* @param array $config
89
83
* @param Connection $connection
84
+ *
90
85
* @return bool
91
86
*/
92
87
public function deleted (Deleted $ event , array $ config , Connection $ connection ): bool
93
88
{
94
- $ user = function ($ connection ) use ($ config ) {
95
- if (config ('tenancy.db.auto-delete-tenant-database-user ' , false )) {
96
- return $ connection ->statement ("DROP USER IF EXISTS ` {$ config ['username ' ]}`@' {$ config ['host ' ]}' " );
97
- }
89
+ $ deleteUser = config ('tenancy.db.auto-delete-tenant-database-user ' , false );
90
+
91
+ $ user = "DROP USER IF EXISTS ` {$ config ['username ' ]}`@' {$ config ['host ' ]}' " ;
98
92
99
- return true ;
100
- };
93
+ $ delete = "DROP DATABASE IF EXISTS ` {$ config ['database ' ]}` " ;
101
94
102
- $ delete = function ($ connection ) use ($ config ) {
103
- return $ connection ->statement ("DROP DATABASE IF EXISTS ` {$ config ['database ' ]}` " );
104
- };
95
+ if ($ deleteUser ) {
96
+ return $ connection ->system ($ event ->website )->statement ($ user )
97
+ && $ connection ->system ($ event ->website )->statement ($ delete );
98
+ }
105
99
106
- return $ connection ->system ($ event ->website )->transaction (function (IlluminateConnection $ connection ) use ($ user , $ delete ) {
107
- return $ delete ($ connection ) && $ user ($ connection );
108
- });
100
+ return $ connection ->system ($ event ->website )->statement ($ delete );
109
101
}
110
102
111
103
public function updatePassword (Website $ website , array $ config , Connection $ connection ): bool
112
104
{
113
- $ user = function (IlluminateConnection $ connection ) use ($ config ) {
114
- return $ connection ->statement ("ALTER USER ` {$ config ['username ' ]}`@' {$ config ['host ' ]}' IDENTIFIED BY ' {$ config ['password ' ]}' " );
115
- };
105
+ $ user = "ALTER USER ` {$ config ['username ' ]}`@' {$ config ['host ' ]}' IDENTIFIED BY ' {$ config ['password ' ]}' " ;
116
106
117
- $ flush = function (IlluminateConnection $ connection ) {
118
- return $ connection ->statement ('FLUSH PRIVILEGES ' );
119
- };
107
+ $ flush = 'FLUSH PRIVILEGES ' ;
120
108
121
- return $ connection ->system ($ website )->transaction (function (IlluminateConnection $ connection ) use ($ user , $ flush ) {
122
- return $ user ($ connection ) && $ flush ($ connection );
123
- });
109
+ return $ connection ->system ($ website )->statement ($ user )
110
+ && $ connection ->system ($ website )->statement ($ flush );
124
111
}
125
112
}
0 commit comments