1
|
|
|
<?php |
2
|
|
|
namespace phpbu\App\Configuration; |
3
|
|
|
|
4
|
|
|
use phpbu\App\Exception; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Finder class. |
8
|
|
|
* |
9
|
|
|
* @package phpbu |
10
|
|
|
* @subpackage Backup |
11
|
|
|
* @author Sebastian Feldmann <[email protected]> |
12
|
|
|
* @copyright Sebastian Feldmann <[email protected]> |
13
|
|
|
* @license https://opensource.org/licenses/MIT The MIT License (MIT) |
14
|
|
|
* @link https://phpbu.de/ |
15
|
|
|
* @since Class available since Release 5.0.0 |
16
|
|
|
*/ |
17
|
|
|
class Finder |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* List of default config file names. |
21
|
|
|
* |
22
|
|
|
* @var string[] |
23
|
|
|
*/ |
24
|
|
|
private $defaultConfigNames = [ |
25
|
|
|
'phpbu.xml', |
26
|
|
|
'phpbu.xml.dist', |
27
|
|
|
]; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Try to find a configuration file. |
31
|
|
|
* |
32
|
|
|
* @param string $path |
33
|
|
|
* @return string |
34
|
|
|
* @throws \InvalidArgumentException |
35
|
|
|
* @throws \phpbu\App\Exception |
36
|
|
|
*/ |
37
|
|
|
public function findConfiguration(string $path) : string |
38
|
|
|
{ |
39
|
|
|
// check configuration argument |
40
|
|
|
// if configuration argument is a directory |
41
|
|
|
// check for default configuration files 'phpbu.xml' and 'phpbu.xml.dist' |
42
|
|
|
if (!empty($path)) { |
43
|
|
|
if (is_file($path)) { |
44
|
|
|
return realpath($path); |
45
|
|
|
} |
46
|
|
|
if (is_dir($path)) { |
47
|
|
|
return $this->findConfigurationInDir($path); |
48
|
|
|
} |
49
|
|
|
// no config found :( |
50
|
|
|
throw new Exception('Invalid path'); |
51
|
|
|
} |
52
|
|
|
// no configuration argument search for default configuration files |
53
|
|
|
// 'phpbu.xml' and 'phpbu.xml.dist' in current working directory |
54
|
|
|
return $this->findConfigurationInDir(getcwd()); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Check directory for default configuration files phpbu.xml, phpbu.xml.dist. |
59
|
|
|
* |
60
|
|
|
* @param string $path |
61
|
|
|
* @return string |
62
|
|
|
* @throws \phpbu\App\Exception |
63
|
|
|
*/ |
64
|
|
|
protected function findConfigurationInDir(string $path) : string |
65
|
|
|
{ |
66
|
|
|
foreach ($this->defaultConfigNames as $file) { |
67
|
|
|
$configurationFile = $path . DIRECTORY_SEPARATOR . $file; |
68
|
|
|
if (file_exists($configurationFile)) { |
69
|
|
|
return realpath($configurationFile); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
throw new Exception('Can\'t find configuration in directory \'' . $path . '\'.'); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|