-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDump.php
More file actions
132 lines (109 loc) · 2.36 KB
/
Copy pathDump.php
File metadata and controls
132 lines (109 loc) · 2.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
/**
* @author : Jakiboy
* @package : FloatPHP
* @subpackage : Helpers Connection Component
* @version : 1.5.x
* @copyright : (c) 2018 - 2025 Jihad Sinnaour <[email protected]>
* @link : https://floatphp.com
* @license : MIT
*
* This file is a part of FloatPHP Framework.
*/
declare(strict_types=1);
namespace FloatPHP\Helpers\Connection;
use FloatPHP\Classes\Filesystem\Arrayify;
use FloatPHP\Classes\Server\System;
use \mysqli;
System::setTimeLimit(0);
System::setMemoryLimit('-1');
/**
* MySQLi dump class.
*/
final class Dump
{
use \FloatPHP\Kernel\TraitConfiguration;
/**
* @access private
* @var array $access
*/
private $access = [];
/**
* @param array $config
*/
public function __construct($config = [])
{
// Init access
$this->access = Arrayify::merge(
$this->getDbAccess(),
$config
);
}
/**
* Import dump file.
*
* @access public
* @param string $file
* @return bool
*/
public function import(string $file) : bool
{
// Init connection
$connection = @new mysqli(
hostname: $this->access['host'],
username: $this->access['user'],
password: $this->access['pswd'],
database: $this->access['name']
);
// Check connection
if ( $connection->connect_errno ) {
return false;
}
$status = 0;
// Temporary variable, store current query
$temp = '';
// Read file
$lines = file($file);
// Loop through each line
foreach ($lines as $line) {
// Skip comment
if ( substr($line, 0, 2) == '--' || $line == '' ) {
continue;
}
// Add line to current segment
$temp .= $line;
// End of query
if ( substr(trim($line), -1, 1) == ';' ) {
// Perform query
$i = (int)$connection->query($temp);
$status += $i;
if ( !$i ) break;
// Reset temp
$temp = '';
}
}
$connection->close();
return (bool)$status;
}
/**
* Export dump file.
*
* @access public
* @param string $file
* @return bool
*/
public function export(string $file = 'dump.sql') : bool
{
$command = 'mysqldump --opt';
$command .= " -u {$this->access['user']}";
if ( $this->access['host'] ) {
$command .= " -h {$this->access['host']}";
}
if ( $this->access['pswd'] ) {
$command .= " -p {$this->access['pswd']}";
}
$command .= " {$this->access['name']} > {$file}";
System::execute($command, $output, $status);
return $status === 0;
}
}