1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of dimtrovich/db-dumper". |
5
|
|
|
* |
6
|
|
|
* (c) 2024 Dimitri Sitchet Tomkeu <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view |
9
|
|
|
* the LICENSE file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Dimtrovich\DbDumper; |
13
|
|
|
|
14
|
|
|
use BadMethodCallException; |
15
|
|
|
use Dimtrovich\DbDumper\Adapters\Factory as AdapterFactory; |
16
|
|
|
use Dimtrovich\DbDumper\Compressor\Factory as CompressorFactory; |
17
|
|
|
use PDO; |
18
|
|
|
|
19
|
|
|
trait Dumper |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Configuration options |
23
|
|
|
*/ |
24
|
|
|
private Option $option; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Compression manager |
28
|
|
|
*/ |
29
|
|
|
private CompressorFactory $compressor; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Database adapter |
33
|
|
|
*/ |
34
|
|
|
private AdapterFactory $adapter; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Event manager |
38
|
|
|
*/ |
39
|
|
|
private Event $event; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Database connection PDO instance |
43
|
|
|
*/ |
44
|
|
|
private PDO $pdo; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Database driver |
48
|
|
|
*/ |
49
|
|
|
private string $driver; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Database name |
53
|
|
|
*/ |
54
|
|
|
private string $database; |
55
|
|
|
|
56
|
|
|
public function __construct(string $database, PDO $pdo, array $options = []) |
57
|
|
|
{ |
58
|
|
|
$this->database = $database; |
59
|
|
|
$this->pdo = $pdo; |
60
|
|
|
$this->option = new Option($options, $this->driver = $pdo->getAttribute(PDO::ATTR_DRIVER_NAME)); |
61
|
|
|
$this->event = new Event(); |
62
|
|
|
$this->compressor = CompressorFactory::create($this->option->compress); |
63
|
|
|
$this->adapter = AdapterFactory::create($pdo, $this->option); |
64
|
|
|
|
65
|
|
|
if ($this->driver === 'mysql') { |
66
|
|
|
// This drops MYSQL dependency, only use the constant if it's defined. |
67
|
|
|
$pdo->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
// Execute init commands once connected |
71
|
|
|
foreach ($this->option->init_commands as $stmt) { |
72
|
|
|
$pdo->exec($stmt); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$pdo->setAttribute(PDO::ATTR_ORACLE_NULLS, PDO::NULL_NATURAL); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Get Dumper configurations option |
80
|
|
|
*/ |
81
|
|
|
public function getOption(): Option |
82
|
|
|
{ |
83
|
|
|
return $this->option; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Set Dumper configurations option |
88
|
|
|
* |
89
|
|
|
* @param array|Option $option |
90
|
|
|
* |
91
|
|
|
* @return static |
92
|
|
|
*/ |
93
|
|
|
public function setOption($option) |
94
|
|
|
{ |
95
|
|
|
if ($option instanceof Option) { |
96
|
|
|
$this->option = $option; |
97
|
|
|
} else { |
98
|
|
|
$this->option->setOptions($option); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function __call($name, $args) |
105
|
|
|
{ |
106
|
|
|
if (str_starts_with($name, 'on')) { |
107
|
|
|
$name = CaseConverter::toDot(substr($name, 2)); |
108
|
|
|
|
109
|
|
|
$this->event->on($name, array_shift($args)); |
110
|
|
|
|
111
|
|
|
return; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
throw new BadMethodCallException(sprintf('Method "%s" is not allowed to be called on "%s"', $name, static::class)); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|