Skip to content

Commit fe990ce

Browse files
authored
Merge pull request #1 from coderatio/laravel6-support
Added support for laravel 6
2 parents 5c8eaba + 66a02ad commit fe990ce

8 files changed

Lines changed: 75 additions & 123 deletions

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
/vendor
2-
composer.lock
2+
composer.lock
3+
.DS_Store

.idea/workspace.xml

Lines changed: 21 additions & 76 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
},
1818
"minimum-stability": "dev",
1919
"require": {
20-
"php": ">=5.6"
20+
"php": "^5.6|^7.0|^7.1|^7.2"
2121
},
2222
"require-dev": {
23-
"phpunit/phpunit": "7"
23+
"phpunit/phpunit": ">=7"
2424
}
2525
}

demo.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,4 @@
44

55
use Coderatio\SimpleBackup\SimpleBackup;
66

7-
SimpleBackup::setDatabase(['test_import_db', 'root', ''])
8-
->then()->importFrom('backups/my_db.sql')
9-
->then(function ($simpleBackup) {
10-
echo var_dump($simpleBackup->getResponse());
11-
});
7+
SimpleBackup::setDatabase(['test_papay', 'root', 'root'])->downloadAfterExport();

src/Foundation/Configurator.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ final class Configurator
88
'db_host' => 'localhost',
99
'db_name' => '',
1010
'db_user' => '',
11-
'insert_chunk' => 100,
11+
'insert_chunk' => 200,
1212
'db_password' => ''
1313
];
1414

@@ -47,7 +47,7 @@ public static function insertDumpHeader($connection, $config)
4747
$generation_time = date('M d, Y ') . 'at ' . date('h:s A');
4848
$copyright = 'Coderatio';
4949

50-
$contents = "-- Simple Backup SQL Dump\r\n-- Version 1.0\r\n-- https://www.github.com/coderatio/simple-backup/\r\n--\r\n-- Host: localhost:3306\r\n-- Generation Time: {$generation_time}\r\n-- MYSQL Server Version: {$mysql_version}\r\n-- PHP Version: {$php_version}\r\n-- Developer: Josiah O. Yahaya\r\n-- Copyright: {$copyright}";
50+
$contents = "-- Simple Backup SQL Dump\r\n-- Version 1.0.3\r\n-- https://www.github.com/coderatio/simple-backup/\r\n--\r\n-- Host: localhost:3306\r\n-- Generation Time: {$generation_time}\r\n-- MYSQL Server Version: {$mysql_version}\r\n-- PHP Version: {$php_version}\r\n-- Developer: Josiah O. Yahaya\r\n-- Copyright: {$copyright}";
5151

5252
$contents .= "\r\n\r\nSET SQL_MODE = \"NO_AUTO_VALUE_ON_ZERO\";\r\nSET AUTOCOMMIT = 0;\r\nSTART TRANSACTION;\r\nSET time_zone = \"+00:00\"\r\n\r\n--\r\n-- Database: `" . $config['db_name'] . "`\r\n-- Total Tables: {$config['total_tables']}\r\n--\r\n\r\n";
5353

@@ -56,10 +56,6 @@ public static function insertDumpHeader($connection, $config)
5656

5757
protected function isAssociativeArray($config)
5858
{
59-
if (array_keys($config) !== range(0, count($config) - 1)) {
60-
return true;
61-
}
62-
63-
return false;
59+
return array_keys($config) !== range(0, count($config) - 1);
6460
}
6561
}

src/Foundation/Database.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public static function prepare($config = [])
2222
);
2323

2424
if ($self->connection->connect_error) {
25-
throw new RuntimeException('Failed to connect to database: ' . $self->connection->connect_error);
25+
throw new \RuntimeException('Failed to connect to database: ' . $self->connection->connect_error);
2626
}
2727

2828
return $self->connection;

src/Foundation/Provider.php

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,20 @@ public static function init($config)
1212
$config['include_tables'] = isset($config['include_tables']) ? $config['include_tables'] : [];
1313
$config['exclude_tables'] = isset($config['exclude_tables']) ? $config['exclude_tables'] : [];
1414

15-
return new self(
16-
"mysql:host={$config['db_host']};dbname={$config['db_name']}",
17-
"{$config['db_user']}",
18-
"{$config['db_password']}",
19-
[
20-
'include-tables' => $config['include_tables'],
21-
'exclude-tables' => $config['exclude_tables']
22-
]
23-
);
15+
try {
16+
return new self(
17+
"mysql:host={$config['db_host']};dbname={$config['db_name']}",
18+
(string)($config['db_user']),
19+
(string)($config['db_password']),
20+
[
21+
'include-tables' => $config['include_tables'],
22+
'exclude-tables' => $config['exclude_tables']
23+
]
24+
);
25+
}
26+
27+
catch (\Exception $e) {
28+
throw new \RuntimeException($e->getMessage());
29+
}
2430
}
2531
}

src/SimpleBackup.php

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Coderatio\SimpleBackup;
44

5+
use Closure;
56
use RuntimeException;
67
use Coderatio\SimpleBackup\Foundation\Database;
78
use Coderatio\SimpleBackup\Foundation\Provider;
@@ -229,7 +230,7 @@ public function importFrom($sql_file_OR_content, $config = [])
229230
set_time_limit(3000);
230231

231232
$error_message = '';
232-
$error_status = true;
233+
$error_status = false;
233234

234235
try {
235236
if (!empty($config)) {
@@ -279,7 +280,7 @@ public function importFrom($sql_file_OR_content, $config = [])
279280
}
280281
}
281282
} catch (\Exception $e) {
282-
$error_status = false;
283+
$error_status = true;
283284

284285
$this->response = [
285286
'status' => false,
@@ -289,7 +290,7 @@ public function importFrom($sql_file_OR_content, $config = [])
289290

290291
$this->response['message'] = $error_message;
291292

292-
if ($error_status === true) {
293+
if ($error_status === false) {
293294
$this->response['message'] = 'Importing finished successfully';
294295
}
295296

@@ -354,7 +355,7 @@ public function storeAfterExportTo($path_to_store, $name = null)
354355

355356
$this->export_name = $export_name;
356357

357-
if (!file_exists($path_to_store) && !mkdir($path_to_store)) {
358+
if (!file_exists($path_to_store) && !mkdir($path_to_store) && !is_dir($path_to_store)) {
358359
throw new RuntimeException(sprintf('Directory "%s" was not created', $path_to_store));
359360
}
360361

@@ -386,12 +387,12 @@ public function getExportedName()
386387
* This is used to chain more methods.
387388
* You can pass in a function to modify any other thing.
388389
*
389-
* @param null $callback
390+
* @param Closure $callback
390391
* @return $this
391392
*/
392-
public function then($callback = null)
393+
public function then(Closure $callback = null)
393394
{
394-
if ($callback !== null && is_callable($callback)) {
395+
if ($callback !== null) {
395396
$callback($this);
396397
}
397398

@@ -426,26 +427,29 @@ public function setTableLimitsOn($tables = [])
426427
return $this;
427428
}
428429

429-
/**
430-
* Include only the tables mentioned in @var $tables
431-
*
432-
* @param array $tables
430+
/**
431+
* Include only the tables mentioned in @param array $tables
433432
* @return $this
433+
* @throws NoTablesFoundException
434+
* @var $tables
435+
*
434436
*/
435437
public function includeOnly($tables = [])
436438
{
437439
$this->include_only_some_tables = true;
438440

439-
$this->tables_to_include = array_filter($tables, function($table) {
440-
if(in_array($table, $this->getTargetTables())) {
441+
$this->tables_to_include = array_filter($tables, static function($table) {
442+
if(in_array($table, $this->getTargetTables(), false)) {
441443
return $table;
442444
}
445+
446+
return null;
443447
});
444448

445449
$this->tables = $this->tables_to_include;
446450

447451
if(empty($this->tables_to_include)) {
448-
throw new NoTablesFoundException("No tables found to export.");
452+
throw new NoTablesFoundException('No tables found to export.');
449453
}
450454

451455
$this->config['include_tables'] = $this->tables_to_include;
@@ -455,30 +459,34 @@ public function includeOnly($tables = [])
455459
}
456460

457461
/**
458-
* Exclude only the tables mentioned in @var $tables
459-
*
460-
* @param array $tables
462+
* Exclude only the tables mentioned in @param array $tables
461463
* @return $this
464+
* @throws NoTablesFoundException
465+
* @var $tables
466+
*
462467
*/
463468
public function excludeOnly($tables = [])
464469
{
465470
$this->exclude_only_some_tables = true;
466471

467-
$this->tables_to_exclude = array_filter($this->getTargetTables(), function($table) use ($tables) {
468-
if(in_array($table, $tables)) {
472+
$this->tables_to_exclude = array_filter($this->getTargetTables(), static function($table) use ($tables) {
473+
if(in_array($table, $tables, false)) {
469474
return $table;
470475
}
476+
477+
return null;
471478
});
472479

473-
$this->tables = array_filter($this->tables, function($table){
474-
if(!in_array($table, $this->tables_to_exclude)) {
480+
$this->tables = array_filter($this->tables, static function($table){
481+
if(!in_array($table, $this->tables_to_exclude, false)) {
475482
return $table;
476483
}
477484

485+
return null;
478486
});
479487

480488
if(empty($this->tables)) {
481-
throw new NoTablesFoundException("No tables found to export.");
489+
throw new NoTablesFoundException('No tables found to export.');
482490
}
483491

484492
$this->config['exclude_tables'] = $this->tables_to_exclude;

0 commit comments

Comments
 (0)