1
|
|
|
<?php |
2
|
|
|
namespace phpbu\App\Backup\Compressor; |
3
|
|
|
|
4
|
|
|
use phpbu\App\Backup\Target; |
5
|
|
|
use phpbu\App\Cli\Executable; |
|
|
|
|
6
|
|
|
use phpbu\App\Cli\Executable\Tar; |
7
|
|
|
use phpbu\App\Exception; |
8
|
|
|
use phpbu\App\Result; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Directory |
12
|
|
|
* |
13
|
|
|
* @package phpbu |
14
|
|
|
* @subpackage Backup |
15
|
|
|
* @author Sebastian Feldmann <[email protected]> |
16
|
|
|
* @copyright Sebastian Feldmann <[email protected]> |
17
|
|
|
* @license https://opensource.org/licenses/MIT The MIT License (MIT) |
18
|
|
|
* @link http://phpbu.de/ |
19
|
|
|
* @since Class available since Release 2.0.1 |
20
|
|
|
*/ |
21
|
|
|
class Directory extends Abstraction |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Validate path. |
25
|
|
|
* |
26
|
|
|
* @param string $path |
27
|
|
|
* @return bool |
28
|
5 |
|
*/ |
29
|
|
|
public function isPathValid($path) : bool |
30
|
5 |
|
{ |
31
|
|
|
return is_dir($path); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* If 'tar' can't compress with the requested compressor this will return false. |
36
|
|
|
* |
37
|
|
|
* @param \phpbu\App\Backup\Target $target |
38
|
|
|
* @return bool |
39
|
|
|
* @throws \phpbu\App\Exception |
40
|
3 |
|
*/ |
41
|
|
|
public function canCompress(Target $target) : bool |
42
|
3 |
|
{ |
43
|
3 |
|
if (!$target->shouldBeCompressed()) { |
44
|
1 |
|
throw new Exception('target should not be compressed at all'); |
45
|
|
|
} |
46
|
|
|
return Tar::isCompressionValid($target->getCompression()->getCommand()); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Compress the configured directory. |
51
|
|
|
* |
52
|
3 |
|
* @param \phpbu\App\Backup\Target $target |
53
|
3 |
|
* @param \phpbu\App\Result $result |
54
|
1 |
|
* @return string |
55
|
1 |
|
* @throws \phpbu\App\Exception |
56
|
|
|
*/ |
57
|
1 |
|
public function compress(Target $target, Result $result) : string |
58
|
1 |
|
{ |
59
|
1 |
|
$target->setMimeType('application/x-tar'); |
60
|
1 |
|
$target->appendFileSuffix('tar'); |
61
|
1 |
|
return parent::compress($target, $result); |
62
|
1 |
|
} |
63
|
1 |
|
|
64
|
3 |
|
/** |
65
|
|
|
* Returns the executable for this action. |
66
|
|
|
* |
67
|
|
|
* @param \phpbu\App\Backup\Target $target |
68
|
|
|
* @return \phpbu\App\Cli\Executable |
69
|
|
|
*/ |
70
|
|
|
protected function createExecutable(Target $target) : Executable |
71
|
|
|
{ |
72
|
|
|
$executable = new Tar($this->pathToCommand); |
73
|
|
|
$executable->archiveDirectory($this->path); |
74
|
|
|
$executable->archiveTo($this->getArchiveFile($target)) |
75
|
|
|
->useCompression( |
76
|
|
|
$target->shouldBeCompressed() ? $target->getCompression()->getCommand() : '' |
77
|
|
|
) |
78
|
|
|
->removeSourceDirectory(true); |
79
|
|
|
return $executable; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Get final archive name. |
84
|
|
|
* |
85
|
|
|
* @param \phpbu\App\Backup\Target $target |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
|
|
public function getArchiveFile(Target $target) : string |
89
|
|
|
{ |
90
|
|
|
return $target->shouldBeCompressed() && $this->canCompress($target) |
91
|
|
|
? $target->getPathname() |
92
|
|
|
: $target->getPathnamePlain(); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: