Skip to content

Commit ba9809b

Browse files
committed
Merge branch 'develop' into cakephp4
2 parents 8938553 + cb97dd7 commit ba9809b

File tree

16 files changed

+71
-105
lines changed

16 files changed

+71
-105
lines changed

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
# 2.x branch
2+
## 2.8 branch
3+
### 2.8.0
4+
* updated for `cakephp` 4 and `phpunit` 8.
5+
26
## 2.7 branch
37
### 2.7.0
4-
* updated for `cakephp` 4 and `phpunit` 8.
8+
* `BackupTrait::getBinary()` method has been moved to `Driver` abstract class;
9+
* `BackupTrait::getTarget()`, `BackupTrait::getDriverName()` and
10+
`BackupTrait::getValidExtensions()` methods have been removed.
511

612
## 2.6 branch
713
### 2.6.6

phpcs.xml.dist

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,8 @@
44
<file>.</file>
55

66
<rule ref="./vendor/mirko-pagliai/php-tools/sniffer-ruleset.xml"/>
7+
8+
<rule ref="SlevomatCodingStandard.Namespaces.UseFromSameNamespace.UseFromSameNamespace">
9+
<severity>0</severity>
10+
</rule>
711
</ruleset>

src/BackupTrait.php

Lines changed: 8 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
use Cake\Datasource\ConnectionInterface;
1919
use Cake\Datasource\ConnectionManager;
2020
use InvalidArgumentException;
21-
use RuntimeException;
2221
use Symfony\Component\Filesystem\Filesystem;
2322

2423
/**
@@ -31,32 +30,20 @@ trait BackupTrait
3130
* @since 2.4.0
3231
* @var array
3332
*/
34-
private static $validExtensions = ['sql.bz2' => 'bzip2', 'sql.gz' => 'gzip', 'sql' => false];
33+
protected static $validExtensions = ['sql.bz2' => 'bzip2', 'sql.gz' => 'gzip', 'sql' => false];
3534

3635
/**
3736
* Returns an absolute path
3837
* @param string $path Relative or absolute path
3938
* @return string
40-
* @uses getTarget()
4139
*/
4240
public function getAbsolutePath(string $path): string
4341
{
44-
return (new Filesystem())->isAbsolutePath($path) ? $path : add_slash_term($this->getTarget()) . $path;
45-
}
46-
47-
/**
48-
* Gets a binary path
49-
* @param string $name Binary name
50-
* @return string
51-
* @since 2.0.0
52-
* @throws \RuntimeException
53-
*/
54-
public function getBinary(string $name): string
55-
{
56-
$binary = Configure::read('DatabaseBackup.binaries.' . $name);
57-
is_true_or_fail($binary, __d('database_backup', 'Binary for `{0}` could not be found. You have to set its path manually', $name), RuntimeException::class);
42+
if (!(new Filesystem())->isAbsolutePath($path)) {
43+
return add_slash_term(Configure::read('DatabaseBackup.target')) . $path;
44+
}
5845

59-
return $binary;
46+
return $path;
6047
}
6148

6249
/**
@@ -93,12 +80,11 @@ public function getConnection(?string $name = null): ConnectionInterface
9380
* @since 2.0.0
9481
* @throws \InvalidArgumentException
9582
* @uses getConnection()
96-
* @uses getDriverName()
9783
*/
9884
public function getDriver(?ConnectionInterface $connection = null): object
9985
{
10086
$connection = $connection ?: $this->getConnection();
101-
$className = $this->getDriverName($connection);
87+
$className = get_class_short_name($connection->getDriver());
10288
$driver = App::classname(sprintf('%s.%s', 'DatabaseBackup', $className), 'Driver');
10389
is_true_or_fail(
10490
$driver,
@@ -109,41 +95,18 @@ public function getDriver(?ConnectionInterface $connection = null): object
10995
return new $driver($connection);
11096
}
11197

112-
/**
113-
* Gets the driver name, according to the database engine
114-
* @param \Cake\Datasource\ConnectionInterface|null $connection A connection object
115-
* @return string The driver name
116-
* @since 2.6.2
117-
* @uses getConnection()
118-
*/
119-
public function getDriverName(?ConnectionInterface $connection = null): string
120-
{
121-
$connection = $connection ?: $this->getConnection();
122-
123-
return get_class_short_name($connection->getDriver());
124-
}
125-
12698
/**
12799
* Returns the extension from a filename
128100
* @param string $filename Filename
129101
* @return string|null Extension or `null` if the extension is not found or
130102
* if is an invalid extension
131-
* @uses getValidExtensions()
103+
* @uses $validExtensions
132104
*/
133105
public function getExtension(string $filename): ?string
134106
{
135107
$extension = get_extension($filename);
136108

137-
return in_array($extension, $this->getValidExtensions()) ? $extension : null;
138-
}
139-
140-
/**
141-
* Returns the target path
142-
* @return string
143-
*/
144-
public function getTarget(): string
145-
{
146-
return Configure::read('DatabaseBackup.target');
109+
return in_array($extension, array_keys(self::$validExtensions)) ? $extension : null;
147110
}
148111

149112
/**
@@ -156,15 +119,4 @@ public function getValidCompressions(): array
156119
{
157120
return array_filter(self::$validExtensions);
158121
}
159-
160-
/**
161-
* Returns all valid extensions
162-
* @return array
163-
* @since 2.4.0
164-
* @uses $validExtensions
165-
*/
166-
public function getValidExtensions(): array
167-
{
168-
return array_keys(self::$validExtensions);
169-
}
170122
}

src/Console/Command.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function execute(Arguments $args, ConsoleIo $io): ?int
3737
$config = $this->getConnection()->config();
3838

3939
$io->out(__d('database_backup', 'Connection: {0}', $config['name']));
40-
$io->out(__d('database_backup', 'Driver: {0}', $this->getDriverName($this->getConnection())));
40+
$io->out(__d('database_backup', 'Driver: {0}', get_class_short_name($this->getConnection()->getDriver())));
4141
$io->hr();
4242

4343
return null;

src/Driver/Driver.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Cake\Event\EventDispatcherTrait;
1919
use Cake\Event\EventListenerInterface;
2020
use DatabaseBackup\BackupTrait;
21+
use RuntimeException;
2122

2223
/**
2324
* Represents a driver containing all methods to export/import database backups
@@ -121,6 +122,7 @@ public function beforeImport(): bool
121122
* @param string $filename Filename where you want to export the database
122123
* @return string
123124
* @uses _exportExecutable()
125+
* @uses getBinary()
124126
*/
125127
protected function _exportExecutableWithCompression(string $filename): string
126128
{
@@ -144,6 +146,7 @@ protected function _exportExecutableWithCompression(string $filename): string
144146
* @param string $filename Filename from which you want to import the database
145147
* @return string
146148
* @uses _importExecutable()
149+
* @uses getBinary()
147150
*/
148151
protected function _importExecutableWithCompression(string $filename): string
149152
{
@@ -190,6 +193,20 @@ final public function export(string $filename): bool
190193
return file_exists($filename);
191194
}
192195

196+
/**
197+
* Gets a binary path
198+
* @param string $name Binary name
199+
* @return string
200+
* @throws \RuntimeException
201+
*/
202+
public function getBinary($name)
203+
{
204+
$binary = Configure::read('DatabaseBackup.binaries.' . $name);
205+
is_true_or_fail($binary, sprintf('Binary for `%s` could not be found. You have to set its path manually', $name), RuntimeException::class);
206+
207+
return $binary;
208+
}
209+
193210
/**
194211
* Gets a config value or the whole configuration
195212
* @param string|null $key Config key or `null` to get all config values

src/Driver/Mysql.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class Mysql extends Driver
3131
/**
3232
* Gets the executable command to export the database
3333
* @return string
34+
* @uses getBinary()
3435
* @uses getConfig()
3536
* @uses $auth
3637
*/
@@ -47,6 +48,7 @@ protected function _exportExecutable(): string
4748
/**
4849
* Gets the executable command to import the database
4950
* @return string
51+
* @uses getBinary()
5052
* @uses getConfig()
5153
* @uses $auth
5254
*/

src/Driver/Postgres.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ protected function getDbnameAsString(): string
4747
/**
4848
* Gets the executable command to export the database
4949
* @return string
50+
* @uses getBinary()
5051
* @uses getDbnameAsString()
5152
*/
5253
protected function _exportExecutable(): string
@@ -57,6 +58,7 @@ protected function _exportExecutable(): string
5758
/**
5859
* Gets the executable command to import the database
5960
* @return string
61+
* @uses getBinary()
6062
* @uses getDbnameAsString()
6163
*/
6264
protected function _importExecutable(): string

src/Driver/Sqlite.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class Sqlite extends Driver
2424
/**
2525
* Gets the executable command to export the database
2626
* @return string
27+
* @uses getBinary()
2728
* @uses getConfig()
2829
*/
2930
protected function _exportExecutable(): string
@@ -34,6 +35,7 @@ protected function _exportExecutable(): string
3435
/**
3536
* Gets the executable command to import the database
3637
* @return string
38+
* @uses getBinary()
3739
* @uses getConfig()
3840
*/
3941
protected function _importExecutable(): string

src/TestSuite/DriverTestCase.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,10 @@ public function testExport()
122122
*/
123123
public function testExportAndImport()
124124
{
125-
foreach ($this->getValidExtensions() as $extension) {
125+
foreach (self::$validExtensions as $extension) {
126126
$this->loadFixtures();
127-
$backup = $this->getAbsolutePath(sprintf('example.%s', $extension));
127+
$backup = uniqid('example_');
128+
$backup = $this->getAbsolutePath($extension ? $backup . '.' . $extension : $backup);
128129

129130
//Initial records. 3 articles and 6 comments
130131
$initial = $this->getAllRecords();
@@ -193,7 +194,7 @@ public function testExportExecutableWithCompression()
193194
$expected = sprintf(
194195
'%s | %s > %s%s',
195196
$basicExecutable,
196-
$this->getBinary($compression),
197+
$this->Driver->getBinary($compression),
197198
escapeshellarg($filename),
198199
REDIRECT_TO_DEV_NULL
199200
);
@@ -240,7 +241,7 @@ public function testImportExecutableWithCompression()
240241
$result = $this->invokeMethod($this->Driver, '_importExecutableWithCompression', [$filename]);
241242
$expected = sprintf(
242243
'%s -dc %s | %s%s',
243-
$this->getBinary($compression),
244+
$this->Driver->getBinary($compression),
244245
escapeshellarg($filename),
245246
$basicExecutable,
246247
REDIRECT_TO_DEV_NULL

src/Utility/BackupManager.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function deleteAll(): array
7070
*/
7171
public function index(): Collection
7272
{
73-
$finder = (new Finder())->files()->name('/\.sql(\.(gz|bz2))?$/')->in($this->getTarget());
73+
$finder = (new Finder())->files()->name('/\.sql(\.(gz|bz2))?$/')->in(Configure::read('DatabaseBackup.target'));
7474

7575
return collection($finder)->map(function (SplFileInfo $file) {
7676
return new Entity([

0 commit comments

Comments
 (0)