Database
1 year ago
Database.php
1 year ago
DatabaseInterface.php
1 year ago
DateTimeAdapter.php
1 year ago
Directory.php
1 year ago
DirectoryInterface.php
1 year ago
Maintenance.php
4 years ago
PhpAdapter.php
2 years ago
SourceDatabase.php
2 years ago
WpAdapter.php
3 years ago
SourceDatabase.php
65 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPStaging\Framework\Adapter; |
| 4 | |
| 5 | use stdClass; |
| 6 | use WPStaging\Core\WPStaging; |
| 7 | use wpdb; |
| 8 | |
| 9 | class SourceDatabase |
| 10 | { |
| 11 | /** @var wpdb */ |
| 12 | private $wpdb; |
| 13 | |
| 14 | /** @var object */ |
| 15 | private $options; |
| 16 | |
| 17 | public function __construct($options = stdClass::class) |
| 18 | { |
| 19 | $this->options = $options; |
| 20 | $this->wpdb = WPStaging::make('wpdb'); |
| 21 | } |
| 22 | |
| 23 | /** |
| 24 | * @return bool |
| 25 | */ |
| 26 | public function isExternalDatabase() |
| 27 | { |
| 28 | return !(empty($this->options->databaseUser) || |
| 29 | empty($this->options->databasePassword) || |
| 30 | empty($this->options->databaseDatabase) || |
| 31 | empty($this->options->databaseServer)); |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * @return object |
| 36 | */ |
| 37 | private function getExternalDb() |
| 38 | { |
| 39 | return new wpdb($this->options->databaseUser, str_replace("\\\\", "\\", $this->options->databasePassword), $this->options->databaseDatabase, $this->options->databaseServer); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Check if source database is a local or external one and get the corresponding database object |
| 44 | * |
| 45 | * @return wpdb |
| 46 | */ |
| 47 | public function getDatabase() |
| 48 | { |
| 49 | if ($this->isExternalDatabase()) { |
| 50 | return $this->getExternalDb(); |
| 51 | } |
| 52 | |
| 53 | return $this->wpdb; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * @param object $options |
| 58 | * @return void |
| 59 | */ |
| 60 | public function setOptions($options) |
| 61 | { |
| 62 | $this->options = $options; |
| 63 | } |
| 64 | } |
| 65 |