1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace EDouna\LaravelDBBackup\Databases; |
4
|
|
|
|
5
|
|
|
use EDouna\LaravelDBBackup\ProcessHandler; |
6
|
|
|
use Illuminate\Support\Facades\Config; |
7
|
|
|
use SQLiteDatabase; |
|
|
|
|
8
|
|
|
|
9
|
|
|
class Database |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var mixed |
13
|
|
|
*/ |
14
|
|
|
protected $database; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
public $realDatabase; |
20
|
|
|
|
21
|
|
|
protected $processHandler; |
22
|
|
|
|
23
|
|
|
public $backupFilename; |
24
|
|
|
|
25
|
|
|
protected $storageFolder; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var array |
29
|
|
|
*/ |
30
|
|
|
protected $supportedDatabaseDrivers = ['mysql', 'sqlite']; |
31
|
|
|
|
32
|
|
|
public function __construct() |
33
|
|
|
{ |
34
|
|
|
$this->database = Config::get('database.default'); |
35
|
|
|
$this->realDatabase = Config::get('database.connections.'.$this->database); |
36
|
|
|
$this->processHandler = new ProcessHandler(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function buildDatabaseClass(): void |
40
|
|
|
{ |
41
|
|
|
switch ($this->realDatabase['driver']) { |
42
|
|
|
case 'mysql': |
43
|
|
|
$this->realDatabase = $this->buildMySQL($this->realDatabase); |
|
|
|
|
44
|
|
|
break; |
45
|
|
|
case 'sqlite': |
46
|
|
|
$this->realDatabase = $this->buildSQLite($this->realDatabase); |
47
|
|
|
break; |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return bool |
53
|
|
|
*/ |
54
|
|
|
public function isDatabaseSupported(): bool |
55
|
|
|
{ |
56
|
|
|
return (in_array($this->realDatabase->getDatabaseIdentifier(), $this->supportedDatabaseDrivers)) ? true : false; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return object |
61
|
|
|
*/ |
62
|
|
|
public function getRealDatabase(): object |
63
|
|
|
{ |
64
|
|
|
return $this->realDatabase; |
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function setBackupFilename(string $backupFilename): void |
68
|
|
|
{ |
69
|
|
|
$this->backupFilename = $backupFilename; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return string |
74
|
|
|
*/ |
75
|
|
|
public function getBackupFilename(): string |
76
|
|
|
{ |
77
|
|
|
return $this->backupFilename; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param array $database |
82
|
|
|
* |
83
|
|
|
* @return MySQLDatabase |
84
|
|
|
*/ |
85
|
|
|
protected function buildMySQL(array $database): MySQLDatabase |
86
|
|
|
{ |
87
|
|
|
$this->database = new MySQLDatabase( |
88
|
|
|
$database['database'], |
89
|
|
|
$database['username'], |
90
|
|
|
$database['password'], |
91
|
|
|
$database['host'], |
92
|
|
|
$database['port'], |
93
|
|
|
$this->processHandler |
94
|
|
|
); |
95
|
|
|
|
96
|
|
|
return $this->database; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Create an SQLite database instance. |
101
|
|
|
* |
102
|
|
|
* @param array $database |
103
|
|
|
* |
104
|
|
|
* @return Databases\SQLiteDatabase |
|
|
|
|
105
|
|
|
*/ |
106
|
|
|
protected function buildSQLite(array $database): SQLiteDatabase |
107
|
|
|
{ |
108
|
|
|
$this->database = new SQLiteDatabase($database['database']); |
109
|
|
|
|
110
|
|
|
$this->generateBackupFilename(); |
|
|
|
|
111
|
|
|
|
112
|
|
|
return $this->database; |
|
|
|
|
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: