Skip to content

Commit 527328a

Browse files
Merge pull request #95 from mirko-pagliai/develop
Develop
2 parents 7f52c20 + febae72 commit 527328a

File tree

13 files changed

+107
-60
lines changed

13 files changed

+107
-60
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# 2.x branch
22
## 2.12 branch
3+
### 2.12.1
4+
* fixed a little bug in the `bootstrap.php` file;
5+
* the `Exceptionist` class provided by me-tools is no longer used (in anticipation of an upcoming deprecation).
6+
37
### 2.12.0
48
* added `AbstractBackupUtility::timeout()` method, so now `BackupExport`/`BackupImport` utilities have a method to set the
59
timeout for shell commands at runtime. Added `--timeout` option (short: `-t`) for `ExportCommand`/`ImportCommand`;

config/bootstrap.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
'DatabaseBackup.chmod' => 0664,
4242
'DatabaseBackup.connection' => 'default',
4343
'DatabaseBackup.processTimeout' => 60,
44-
'DatabaseBackup.target' => ROOT . 'backups',
44+
'DatabaseBackup.target' => rtrim(ROOT, DS) . DS . 'backups',
4545
'DatabaseBackup.mysql.export' => '{{BINARY}} --defaults-file={{AUTH_FILE}} {{DB_NAME}}',
4646
'DatabaseBackup.mysql.import' => '{{BINARY}} --defaults-extra-file={{AUTH_FILE}} {{DB_NAME}}',
4747
'DatabaseBackup.postgres.export' => '{{BINARY}} --format=c -b --dbname=\'postgresql://{{DB_USER}}{{DB_PASSWORD}}@{{DB_HOST}}/{{DB_NAME}}\'',

src/Command/ExportCommand.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
use DatabaseBackup\Console\Command;
2323
use DatabaseBackup\Utility\BackupExport;
2424
use Exception;
25-
use Tools\Exceptionist;
2625

2726
/**
2827
* Exports a database backup
@@ -98,7 +97,10 @@ public function execute(Arguments $args, ConsoleIo $io): void
9897

9998
/** @var string $file */
10099
$file = $BackupExport->export();
101-
Exceptionist::isTrue($file, __d('database_backup', 'The `{0}` event stopped the operation', 'Backup.beforeExport'));
100+
if (!$file) {
101+
$io->error(__d('database_backup', 'The `{0}` event stopped the operation', 'Backup.beforeExport'));
102+
$this->abort();
103+
}
102104
$io->success(__d('database_backup', 'Backup `{0}` has been exported', rtr($file)));
103105

104106
//Sends via email and/or rotates

src/Command/ImportCommand.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
use DatabaseBackup\Console\Command;
2323
use DatabaseBackup\Utility\BackupImport;
2424
use Exception;
25-
use Tools\Exceptionist;
2625

2726
/**
2827
* Imports a database backup
@@ -53,7 +52,7 @@ protected function buildOptionParser(ConsoleOptionParser $parser): ConsoleOption
5352
* @param \Cake\Console\Arguments $args The command arguments
5453
* @param \Cake\Console\ConsoleIo $io The console io
5554
* @return void
56-
* @throws \Cake\Console\Exception\StopException|\ReflectionException
55+
* @throws \ReflectionException
5756
*/
5857
public function execute(Arguments $args, ConsoleIo $io): void
5958
{
@@ -71,7 +70,10 @@ public function execute(Arguments $args, ConsoleIo $io): void
7170

7271
/** @var string $file */
7372
$file = $BackupImport->import();
74-
Exceptionist::isTrue($file, __d('database_backup', 'The `{0}` event stopped the operation', 'Backup.beforeImport'));
73+
if (!$file) {
74+
$io->error(__d('database_backup', 'The `{0}` event stopped the operation', 'Backup.beforeImport'));
75+
$this->abort();
76+
}
7577
$io->success(__d('database_backup', 'Backup `{0}` has been imported', rtr($file)));
7678
} catch (Exception $e) {
7779
$io->error($e->getMessage());

src/Driver/AbstractDriver.php

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
use Cake\Event\EventDispatcherTrait;
2020
use Cake\Event\EventListenerInterface;
2121
use DatabaseBackup\BackupTrait;
22-
use Tools\Exceptionist;
22+
use LogicException;
2323

2424
/**
2525
* Represents a driver containing all methods to export/import database backups according to the connection
@@ -64,13 +64,14 @@ final public function implementedEvents(): array
6464
* have the final executables, including compression.
6565
* @param string $type Type or the request operation (`export` or `import`)
6666
* @return string
67-
* @throws \Tools\Exception\NotInArrayException
67+
* @throws \LogicException
6868
* @throws \ReflectionException
69-
* @throws \ErrorException
7069
*/
7170
private function getExecutable(string $type): string
7271
{
73-
Exceptionist::inArray($type, ['export', 'import']);
72+
if (!in_array($type, ['export', 'import'])) {
73+
throw new LogicException(__d('database_backup', '`$type` parameter should be `export` or `import`'));
74+
}
7475
$driverName = strtolower(self::getDriverName());
7576
$replacements = [
7677
'{{BINARY}}' => escapeshellarg($this->getBinary(DATABASE_BACKUP_EXECUTABLES[$driverName][$type])),
@@ -89,9 +90,8 @@ private function getExecutable(string $type): string
8990
* Gets the executable command to export the database, with compression if requested
9091
* @param string $filename Filename where you want to export the database
9192
* @return string
92-
* @throws \Tools\Exception\NotInArrayException
93+
* @throws \LogicException
9394
* @throws \ReflectionException
94-
* @throws \ErrorException
9595
*/
9696
public function getExportExecutable(string $filename): string
9797
{
@@ -108,9 +108,8 @@ public function getExportExecutable(string $filename): string
108108
* Gets the executable command to import the database, with compression if requested
109109
* @param string $filename Filename from which you want to import the database
110110
* @return string
111-
* @throws \Tools\Exception\NotInArrayException
111+
* @throws \LogicException
112112
* @throws \ReflectionException
113-
* @throws \ErrorException
114113
*/
115114
public function getImportExecutable(string $filename): string
116115
{
@@ -165,11 +164,16 @@ public function beforeImport(): bool
165164
* Gets a binary path
166165
* @param string $name Binary name
167166
* @return string
168-
* @throws \ErrorException
167+
* @throws \LogicException
169168
*/
170169
public function getBinary(string $name): string
171170
{
172-
return Exceptionist::isTrue(Configure::read('DatabaseBackup.binaries.' . $name), 'Binary for `' . $name . '` could not be found. You have to set its path manually');
171+
$binary = Configure::read('DatabaseBackup.binaries.' . $name);
172+
if (!$binary) {
173+
throw new LogicException(__d('database_backup', 'Binary for `{0}` could not be found. You have to set its path manually', $name));
174+
}
175+
176+
return $binary;
173177
}
174178

175179
/**

src/Utility/AbstractBackupUtility.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
use DatabaseBackup\Driver\AbstractDriver;
2222
use LogicException;
2323
use Symfony\Component\Process\Process;
24-
use Tools\Exceptionist;
2524

2625
/**
2726
* AbstractBackupUtility.
@@ -93,7 +92,8 @@ public function timeout(int $timeout)
9392
/**
9493
* Gets the driver instance
9594
* @return \DatabaseBackup\Driver\AbstractDriver A driver instance
96-
* @throws \ErrorException|\ReflectionException
95+
* @throws \LogicException
96+
* @throws \ReflectionException
9797
* @since 2.0.0
9898
*/
9999
public function getDriver(): AbstractDriver
@@ -102,7 +102,9 @@ public function getDriver(): AbstractDriver
102102
$name = $this->getDriverName();
103103
/** @var class-string<\DatabaseBackup\Driver\AbstractDriver> $className */
104104
$className = App::classname('DatabaseBackup.' . $name, 'Driver');
105-
Exceptionist::isTrue($className, __d('database_backup', 'The `{0}` driver does not exist', $name));
105+
if (!$className) {
106+
throw new LogicException(__d('database_backup', 'The `{0}` driver does not exist', $name));
107+
}
106108

107109
$this->Driver = new $className($this->getConnection());
108110
}

src/Utility/BackupExport.php

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
namespace DatabaseBackup\Utility;
1717

1818
use Cake\Core\Configure;
19-
use Tools\Exceptionist;
19+
use LogicException;
2020
use Tools\Filesystem;
2121

2222
/**
@@ -68,7 +68,7 @@ class BackupExport extends AbstractBackupUtility
6868
* @param string|null $compression Compression type name
6969
* @return $this
7070
* @see https://github.com/mirko-pagliai/cakephp-database-backup/wiki/How-to-use-the-BackupExport-utility#compression
71-
* @throws \ErrorException
71+
* @throws \LogicException
7272
* @noinspection PhpMissingReturnTypeInspection
7373
*/
7474
public function compression(?string $compression)
@@ -77,7 +77,9 @@ public function compression(?string $compression)
7777

7878
if ($compression) {
7979
$this->extension = (string)array_search($compression, $this->getValidCompressions());
80-
Exceptionist::isTrue($this->extension, __d('database_backup', 'Invalid compression type'));
80+
if (!$this->extension) {
81+
throw new LogicException(__d('database_backup', 'Invalid compression type'));
82+
}
8183
}
8284
$this->compression = $compression;
8385

@@ -91,7 +93,7 @@ public function compression(?string $compression)
9193
* @param string $filename Filename. It can be an absolute path and may contain patterns
9294
* @return $this
9395
* @see https://github.com/mirko-pagliai/cakephp-database-backup/wiki/How-to-use-the-BackupExport-utility#filename
94-
* @throws \ErrorException|\Tools\Exception\NotWritableException
96+
* @throws \LogicException
9597
* @noinspection PhpMissingReturnTypeInspection
9698
*/
9799
public function filename(string $filename)
@@ -107,11 +109,15 @@ public function filename(string $filename)
107109
], $filename);
108110

109111
$filename = $this->getAbsolutePath($filename);
110-
Exceptionist::isWritable(dirname($filename));
111-
Exceptionist::isTrue(!file_exists($filename), __d('database_backup', 'File `{0}` already exists', $filename));
112-
113-
//Checks for extension
114-
Exceptionist::isTrue($this->getExtension($filename), __d('database_backup', 'Invalid `{0}` file extension', pathinfo($filename, PATHINFO_EXTENSION)));
112+
if (!is_writable(dirname($filename))) {
113+
throw new LogicException(__d('database_backup', 'File or directory `' . dirname($filename) . '` is not writable'));
114+
}
115+
if (file_exists($filename)) {
116+
throw new LogicException(__d('database_backup', 'File `{0}` already exists', $filename));
117+
}
118+
if (!$this->getExtension($filename)) {
119+
throw new LogicException(__d('database_backup', 'Invalid `{0}` file extension', pathinfo($filename, PATHINFO_EXTENSION)));
120+
}
115121

116122
//Sets the compression
117123
$this->compression($this->getCompression($filename));
@@ -156,7 +162,8 @@ public function send(?string $recipient = null)
156162
* - `Backup.beforeExport`: will be triggered before export;
157163
* - `Backup.afterExport`: will be triggered after export.
158164
* @return string|false Filename path on success or `false` if the `Backup.beforeExport` event is stopped
159-
* @throws \Exception
165+
* @throws \LogicException
166+
* @throws \ReflectionException
160167
* @see \DatabaseBackup\Driver\AbstractDriver::afterExport()
161168
* @see \DatabaseBackup\Driver\AbstractDriver::beforeExport()
162169
* @see https://github.com/mirko-pagliai/cakephp-database-backup/wiki/How-to-use-the-BackupExport-utility#export
@@ -180,7 +187,9 @@ public function export()
180187

181188
//Exports
182189
$Process = $this->getProcess($this->getDriver()->getExportExecutable($filename));
183-
Exceptionist::isTrue($Process->isSuccessful(), __d('database_backup', 'Export failed with error message: `{0}`', rtrim($Process->getErrorOutput())));
190+
if (!$Process->isSuccessful()) {
191+
throw new LogicException(__d('database_backup', 'Export failed with error message: `{0}`', rtrim($Process->getErrorOutput())));
192+
}
184193
Filesystem::instance()->chmod($filename, Configure::read('DatabaseBackup.chmod'));
185194

186195
//Dispatches the `Backup.afterExport` event implemented by the driver

src/Utility/BackupImport.php

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
namespace DatabaseBackup\Utility;
1717

18-
use Tools\Exceptionist;
18+
use LogicException;
1919

2020
/**
2121
* Utility to import databases
@@ -27,15 +27,19 @@ class BackupImport extends AbstractBackupUtility
2727
* @param string $filename Filename. It can be an absolute path
2828
* @return $this
2929
* @see https://github.com/mirko-pagliai/cakephp-database-backup/wiki/How-to-use-the-BackupImport-utility#filename
30-
* @throws \ErrorException|\Tools\Exception\NotReadableException
30+
* @throws \LogicException
3131
* @noinspection PhpMissingReturnTypeInspection
3232
*/
3333
public function filename(string $filename)
3434
{
35-
$filename = Exceptionist::isReadable($this->getAbsolutePath($filename));
35+
$filename = $this->getAbsolutePath($filename);
36+
if (!is_readable($filename)) {
37+
throw new LogicException(__d('database_backup', 'File or directory `' . $filename . '` is not readable'));
38+
}
3639

37-
//Checks for extension
38-
Exceptionist::isTrue($this->getExtension($filename), __d('database_backup', 'Invalid file extension'));
40+
if (!$this->getExtension($filename)) {
41+
throw new LogicException(__d('database_backup', 'Invalid file extension'));
42+
}
3943

4044
$this->filename = $filename;
4145

@@ -49,14 +53,17 @@ public function filename(string $filename)
4953
* - `Backup.beforeImport`: will be triggered before import;
5054
* - `Backup.afterImport`: will be triggered after import.
5155
* @return string|false Filename path on success or `false` if the `Backup.beforeImport` event is stopped
52-
* @throws \ErrorException|\ReflectionException
56+
* @throws \LogicException
57+
* @throws \ReflectionException
5358
* @see \DatabaseBackup\Driver\AbstractDriver::afterImport()
5459
* @see \DatabaseBackup\Driver\AbstractDriver::beforeImport()
5560
* @see https://github.com/mirko-pagliai/cakephp-database-backup/wiki/How-to-use-the-BackupImport-utility#import
5661
*/
5762
public function import()
5863
{
59-
Exceptionist::isTrue(!empty($this->filename), __d('database_backup', 'You must first set the filename'));
64+
if (empty($this->filename)) {
65+
throw new LogicException(__d('database_backup', 'You must first set the filename'));
66+
}
6067

6168
//This allows the filename to be set again with a next call of this method
6269
$filename = $this->filename;
@@ -70,7 +77,9 @@ public function import()
7077

7178
//Imports
7279
$Process = $this->getProcess($this->getDriver()->getImportExecutable($filename));
73-
Exceptionist::isTrue($Process->isSuccessful(), __d('database_backup', 'Import failed with error message: `{0}`', rtrim($Process->getErrorOutput())));
80+
if (!$Process->isSuccessful()) {
81+
throw new LogicException(__d('database_backup', 'Import failed with error message: `{0}`', rtrim($Process->getErrorOutput())));
82+
}
7483

7584
//Dispatches the `Backup.afterImport` event implemented by the driver
7685
$this->getDriver()->dispatchEvent('Backup.afterImport');

src/Utility/BackupManager.php

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121
use Cake\Mailer\Mailer;
2222
use Cake\ORM\Entity;
2323
use DatabaseBackup\BackupTrait;
24+
use LogicException;
2425
use Symfony\Component\Finder\Finder;
2526
use Symfony\Component\Finder\SplFileInfo;
26-
use Tools\Exceptionist;
2727
use Tools\Filesystem;
2828

2929
/**
@@ -38,11 +38,14 @@ class BackupManager
3838
* @param string $filename Backup filename you want to delete. The path can be relative to the backup directory
3939
* @return string Deleted backup file
4040
* @see https://github.com/mirko-pagliai/cakephp-database-backup/wiki/How-to-use-the-BackupManager-utility#delete
41-
* @throws \Tools\Exception\NotWritableException
41+
* @throws \LogicException
4242
*/
4343
public static function delete(string $filename): string
4444
{
45-
$filename = Exceptionist::isWritable(self::getAbsolutePath($filename));
45+
$filename = self::getAbsolutePath($filename);
46+
if (!is_writable($filename)) {
47+
throw new LogicException(__d('database_backup', 'File or directory `' . $filename . '` is not writable'));
48+
}
4649
Filesystem::instance()->remove($filename);
4750

4851
return $filename;
@@ -87,12 +90,13 @@ public static function index(): CollectionInterface
8790
* @param int $rotate Number of backups that you want to keep
8891
* @return array<\Cake\ORM\Entity> Array of deleted files
8992
* @see https://github.com/mirko-pagliai/cakephp-database-backup/wiki/How-to-use-the-BackupManager-utility#rotate
90-
* @throws \ErrorException
91-
* @noinspection PhpDocRedundantThrowsInspection
93+
* @throws \LogicException
9294
*/
9395
public static function rotate(int $rotate): array
9496
{
95-
Exceptionist::isPositive($rotate, __d('database_backup', 'Invalid rotate value'));
97+
if (!is_positive($rotate)) {
98+
throw new LogicException(__d('database_backup', 'Invalid rotate value'));
99+
}
96100
$backupsToBeDeleted = self::index()->skip($rotate);
97101
array_map([__CLASS__, 'delete'], $backupsToBeDeleted->extract('filename')->toList());
98102

@@ -105,26 +109,29 @@ public static function rotate(int $rotate): array
105109
* @param string $recipient Recipient's email address
106110
* @return \Cake\Mailer\Mailer
107111
* @since 1.1.0
108-
* @throws \Tools\Exception\NotReadableException
112+
* @throws \LogicException
109113
*/
110114
protected static function getEmailInstance(string $backup, string $recipient): Mailer
111115
{
112-
$file = Exceptionist::isReadable(self::getAbsolutePath($backup));
116+
$filename = self::getAbsolutePath($backup);
117+
if (!is_readable($filename)) {
118+
throw new LogicException(__d('database_backup', 'File or directory `' . $filename . '` is not readable'));
119+
}
113120
$server = env('SERVER_NAME', 'localhost');
114121

115122
return (new Mailer())
116123
->setFrom(Configure::readOrFail('DatabaseBackup.mailSender'))
117124
->setTo($recipient)
118-
->setSubject(__d('database_backup', 'Database backup {0} from {1}', basename($file), $server))
119-
->setAttachments([basename($file) => compact('file') + ['mimetype' => mime_content_type($file)]]);
125+
->setSubject(__d('database_backup', 'Database backup {0} from {1}', basename($filename), $server))
126+
->setAttachments([basename($filename) => ['file' => $filename, 'mimetype' => mime_content_type($filename)]]);
120127
}
121128

122129
/**
123130
* Sends a backup file via email
124131
* @param string $filename Backup filename you want to send via email. The path can be relative to the backup directory
125132
* @param string $recipient Recipient's email address
126133
* @return array{headers: string, message: string}
127-
* @throws \Tools\Exception\NotReadableException
134+
* @throws \LogicException
128135
* @since 1.1.0
129136
*/
130137
public static function send(string $filename, string $recipient): array

tests/TestCase/Utility/BackupExportAndImportTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public function setUp(): void
7373
/** @var \Cake\Database\Connection $connection */
7474
$connection = $this->getConnection('test');
7575
foreach (['Articles', 'Comments'] as $name) {
76-
$this->{$name} ??= $this->getTable($name, compact('connection'));
76+
$this->{$name} ??= $this->fetchTable($name, compact('connection'));
7777
}
7878
}
7979

0 commit comments

Comments
 (0)