Skip to content

Commit cf5b65a

Browse files
Merge pull request #38 from mirko-pagliai/develop
Develop
2 parents 13cbb8a + 81b2714 commit cf5b65a

28 files changed

+331
-308
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.directory
2+
.phpunit.result.cache
23
*_old
34
*.old.*
45
composer.lock

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
# 2.x branch
22
## 2.6 branch
33
### 2.6.2
4+
* added `BackupTrait::getDriverName()` method;
5+
* `BackupExport::compression()` takes a compression type name as string or
6+
`null` to disable compression;
7+
* `BackupExport::send()` takes a recipient's email address as string or `null`
8+
to disable sending backup;
9+
* `BackupTrait::getCompression()` returns `null` with no compression;
10+
* the `DriverTestCase` class now implements `testExportOnFailure()` and
11+
`testImportOnFailure()` test methods;
12+
* improved printing of the backup table for the `IndexCommand`;
13+
* updated for `php-tools` 1.2 and `me-tools` 2.18.7.
414
* added [API](//mirko-pagliai.github.io/cakephp-database-backup).
515

616
### 2.6.1

appveyor.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ before_test:
2727

2828
install:
2929
- cd c:\
30-
- curl -fsS -o php.zip https://windows.php.net/downloads/releases/php-5.6.40-nts-Win32-VC11-x86.zip
30+
- curl -fsS -o php.zip https://windows.php.net/downloads/releases/php-7.2.18-nts-Win32-VC15-x86.zip
3131
- 7z x php.zip -oc:\php > nul
3232
- cd c:\php
3333
- copy php.ini-production php.ini

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111
"require": {
1212
"php": ">=5.5.9",
1313
"cakephp/cakephp": "^3.7",
14-
"mirko-pagliai/php-tools": "^1.1.12"
14+
"mirko-pagliai/php-tools": "^1.2"
1515
},
1616
"require-dev": {
1717
"cakephp/cakephp-codesniffer": "^3.0",
18-
"mirko-pagliai/me-tools": "^2.18.3",
18+
"mirko-pagliai/me-tools": "^2.18.7",
1919
"phpunit/phpunit": "^5.7|^6.0"
2020
},
2121
"autoload": {

config/bootstrap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
* @license https://opensource.org/licenses/mit-license.php MIT License
1212
* @see https://github.com/mirko-pagliai/cakephp-database-backup/wiki/Configuration
1313
*/
14+
1415
use Cake\Core\Configure;
1516

1617
//Sets the redirect to `/dev/null`. This string can be concatenated to shell commands

src/BackupTrait.php

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,17 @@ public function getBinary($name)
5656
/**
5757
* Returns the compression type from a filename
5858
* @param string $filename Filename
59-
* @return string|bool Compression type as string or `false`
59+
* @return string|null Compression type as string or `null`
6060
* @uses getExtension()
6161
* @uses getValidCompressions()
6262
*/
6363
public function getCompression($filename)
6464
{
6565
//Gets the extension from the filename
6666
$extension = $this->getExtension($filename);
67+
$keyExists = array_key_exists($extension, $this->getValidCompressions());
6768

68-
return array_key_exists($extension, $this->getValidCompressions()) ? $this->getValidCompressions()[$extension] : false;
69+
return $keyExists ? $this->getValidCompressions()[$extension] : null;
6970
}
7071

7172
/**
@@ -79,24 +80,43 @@ public function getConnection($name = null)
7980
}
8081

8182
/**
82-
* Gets the driver containing all methods to export/import database backups
83-
* according to the database engine
83+
* Gets the driver instance containing all methods to export/import database
84+
* backups, according to the database engine
8485
* @param \Cake\Datasource\ConnectionInterface|null $connection A connection object
85-
* @return object A driver instance
86+
* @return object The driver instance
8687
* @since 2.0.0
87-
* @throws InvalidArgumentException
88+
* @throws \InvalidArgumentException
8889
* @uses getConnection()
90+
* @uses getDriverName()
8991
*/
9092
public function getDriver(ConnectionInterface $connection = null)
9193
{
9294
$connection = $connection ?: $this->getConnection();
93-
$className = get_class_short_name($connection->getDriver());
95+
$className = $this->getDriverName($connection);
9496
$driver = App::classname(sprintf('%s.%s', 'DatabaseBackup', $className), 'Driver');
95-
is_true_or_fail($driver, __d('database_backup', 'The `{0}` driver does not exist', $className), InvalidArgumentException::class);
97+
is_true_or_fail(
98+
$driver,
99+
__d('database_backup', 'The `{0}` driver does not exist', $className),
100+
InvalidArgumentException::class
101+
);
96102

97103
return new $driver($connection);
98104
}
99105

106+
/**
107+
* Gets the driver name, according to the database engine
108+
* @param \Cake\Datasource\ConnectionInterface|null $connection A connection object
109+
* @return string The driver name
110+
* @since 2.6.2
111+
* @uses getConnection()
112+
*/
113+
public function getDriverName(ConnectionInterface $connection = null)
114+
{
115+
$connection = $connection ?: $this->getConnection();
116+
117+
return get_class_short_name(get_class($connection->getDriver()));
118+
}
119+
100120
/**
101121
* Returns the extension from a filename
102122
* @param string $filename Filename

src/Command/DeleteAllCommand.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ class DeleteAllCommand extends Command
2626
{
2727
/**
2828
* Hook method for defining this command's option parser
29-
* @param ConsoleOptionParser $parser The parser to be defined
30-
* @return ConsoleOptionParser
29+
* @param \Cake\Console\ConsoleOptionParser $parser The parser to be defined
30+
* @return \Cake\Console\ConsoleOptionParser
3131
*/
3232
protected function buildOptionParser(ConsoleOptionParser $parser)
3333
{
@@ -36,8 +36,8 @@ protected function buildOptionParser(ConsoleOptionParser $parser)
3636

3737
/**
3838
* Deletes all backup files
39-
* @param Arguments $args The command arguments
40-
* @param ConsoleIo $io The console io
39+
* @param \Cake\Console\Arguments $args The command arguments
40+
* @param \Cake\Console\ConsoleIo $io The console io
4141
* @return null|int The exit code or null for success
4242
* @see https://github.com/mirko-pagliai/cakephp-database-backup/wiki/How-to-use-the-BackupShell#delete_all
4343
* @uses DatabaseBackup\Utility\BackupManager::deleteAll()
@@ -46,7 +46,7 @@ public function execute(Arguments $args, ConsoleIo $io)
4646
{
4747
parent::execute($args, $io);
4848

49-
$files = (new BackupManager)->deleteAll();
49+
$files = (new BackupManager())->deleteAll();
5050

5151
if (!$files) {
5252
$io->verbose(__d('database_backup', 'No backup has been deleted'));

src/Command/ExportCommand.php

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ class ExportCommand extends Command
2929
{
3030
/**
3131
* Hook method for defining this command's option parser
32-
* @param ConsoleOptionParser $parser The parser to be defined
33-
* @return ConsoleOptionParser
32+
* @param \Cake\Console\ConsoleOptionParser $parser The parser to be defined
33+
* @return \Cake\Console\ConsoleOptionParser
3434
*/
3535
protected function buildOptionParser(ConsoleOptionParser $parser)
3636
{
@@ -47,8 +47,9 @@ protected function buildOptionParser(ConsoleOptionParser $parser)
4747
'short' => 'f',
4848
],
4949
'rotate' => [
50-
'help' => __d('database_backup', 'Rotates backups. You have to indicate the number of backups you ' .
51-
'want to keep. So, it will delete all backups that are older. By default, no backup will be deleted'),
50+
'help' => __d('database_backup', 'Rotates backups. You have to indicate the number of backups' .
51+
'you ìwant to keep. So, it will delete all backups that are older. By default, no backup' .
52+
'will be deleted'),
5253
'short' => 'r',
5354
],
5455
'send' => [
@@ -63,8 +64,8 @@ protected function buildOptionParser(ConsoleOptionParser $parser)
6364
* Exports a database backup.
6465
*
6566
* This command uses `RotateCommand` and `SendCommand`.
66-
* @param Arguments $args The command arguments
67-
* @param ConsoleIo $io The console io
67+
* @param \Cake\Console\Arguments $args The command arguments
68+
* @param \Cake\Console\ConsoleIo $io The console io
6869
* @return null|int The exit code or null for success
6970
* @see https://github.com/mirko-pagliai/cakephp-database-backup/wiki/How-to-use-the-BackupShell#export
7071
* @uses DatabaseBackup\Command\RotateCommand::execute()
@@ -78,13 +79,13 @@ public function execute(Arguments $args, ConsoleIo $io)
7879
parent::execute($args, $io);
7980

8081
try {
81-
$instance = new BackupExport;
82+
$instance = new BackupExport();
8283
//Sets the output filename or the compression type.
8384
//Regarding the `rotate` option, the `BackupShell::rotate()` method
8485
// will be called at the end, instead of `BackupExport::rotate()`
85-
if ($args->hasOption('filename')) {
86+
if ($args->getOption('filename')) {
8687
$instance->filename($args->getOption('filename'));
87-
} elseif ($args->hasOption('compression')) {
88+
} elseif ($args->getOption('compression')) {
8889
$instance->compression($args->getOption('compression'));
8990
}
9091

@@ -95,8 +96,8 @@ public function execute(Arguments $args, ConsoleIo $io)
9596
$quiet = $args->getOption('quiet');
9697

9798
//Sends via email
98-
if ($args->hasOption('send')) {
99-
$SendCommand = new SendCommand;
99+
if ($args->getOption('send')) {
100+
$SendCommand = new SendCommand();
100101
$SendCommand->execute(new Arguments(
101102
[$file, $args->getOption('send')],
102103
compact('verbose', 'quiet'),
@@ -105,8 +106,8 @@ public function execute(Arguments $args, ConsoleIo $io)
105106
}
106107

107108
//Rotates
108-
if ($args->hasOption('rotate')) {
109-
$RotateCommand = new RotateCommand;
109+
if ($args->getOption('rotate')) {
110+
$RotateCommand = new RotateCommand();
110111
$RotateCommand->execute(new Arguments(
111112
[$args->getOption('rotate')],
112113
compact('verbose', 'quiet'),

src/Command/ImportCommand.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ class ImportCommand extends Command
2727
{
2828
/**
2929
* Hook method for defining this command's option parser
30-
* @param ConsoleOptionParser $parser The parser to be defined
31-
* @return ConsoleOptionParser
30+
* @param \Cake\Console\ConsoleOptionParser $parser The parser to be defined
31+
* @return \Cake\Console\ConsoleOptionParser
3232
*/
3333
protected function buildOptionParser(ConsoleOptionParser $parser)
3434
{
@@ -41,8 +41,8 @@ protected function buildOptionParser(ConsoleOptionParser $parser)
4141

4242
/**
4343
* Imports a database backup
44-
* @param Arguments $args The command arguments
45-
* @param ConsoleIo $io The console io
44+
* @param \Cake\Console\Arguments $args The command arguments
45+
* @param \Cake\Console\ConsoleIo $io The console io
4646
* @return null|int The exit code or null for success
4747
* @see https://github.com/mirko-pagliai/cakephp-database-backup/wiki/How-to-use-the-BackupShell#import
4848
* @uses DatabaseBackup\Utility\BackupImport::filename()
@@ -53,7 +53,7 @@ public function execute(Arguments $args, ConsoleIo $io)
5353
parent::execute($args, $io);
5454

5555
try {
56-
$file = (new BackupImport)->filename($args->getArgument('filename'))->import();
56+
$file = (new BackupImport())->filename($args->getArgument('filename'))->import();
5757
$io->success(__d('database_backup', 'Backup `{0}` has been imported', rtr($file)));
5858
} catch (Exception $e) {
5959
$io->error($e->getMessage());

src/Command/IndexCommand.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ class IndexCommand extends Command
2828
{
2929
/**
3030
* Hook method for defining this command's option parser
31-
* @param ConsoleOptionParser $parser The parser to be defined
32-
* @return ConsoleOptionParser
31+
* @param \Cake\Console\ConsoleOptionParser $parser The parser to be defined
32+
* @return \Cake\Console\ConsoleOptionParser
3333
*/
3434
protected function buildOptionParser(ConsoleOptionParser $parser)
3535
{
@@ -38,8 +38,8 @@ protected function buildOptionParser(ConsoleOptionParser $parser)
3838

3939
/**
4040
* Lists database backups
41-
* @param Arguments $args The command arguments
42-
* @param ConsoleIo $io The console io
41+
* @param \Cake\Console\Arguments $args The command arguments
42+
* @param \Cake\Console\ConsoleIo $io The console io
4343
* @return null|int The exit code or null for success
4444
* @see https://github.com/mirko-pagliai/cakephp-database-backup/wiki/How-to-use-the-BackupShell#index
4545
* @uses DatabaseBackup\Utility\BackupManager::index()
@@ -49,7 +49,7 @@ public function execute(Arguments $args, ConsoleIo $io)
4949
parent::execute($args, $io);
5050

5151
//Gets all backups
52-
$backups = (new BackupManager)->index();
52+
$backups = (new BackupManager())->index();
5353
$io->out(__d('database_backup', 'Backup files found: {0}', $backups->count()));
5454

5555
if ($backups->isEmpty()) {
@@ -64,7 +64,10 @@ public function execute(Arguments $args, ConsoleIo $io)
6464
__d('database_backup', 'Datetime'),
6565
];
6666
$cells = $backups->map(function (Entity $backup) {
67-
return $backup->set('size', Number::toReadableSize($backup->size))->toArray();
67+
return $backup->set('compression', $backup->compression ?: '')
68+
->set('datetime', $backup->datetime->nice())
69+
->set('size', Number::toReadableSize($backup->size))
70+
->toArray();
6871
});
6972
$io->helper('table')->output(array_merge([$headers], $cells->toList()));
7073

0 commit comments

Comments
 (0)