|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Popstas\Transmission\Console; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
6
|
|
|
use Symfony\Component\Yaml\Yaml; |
|
7
|
|
|
|
|
8
|
|
|
class Config |
|
9
|
|
|
{ |
|
10
|
|
|
private static $defaultConfig = array( |
|
11
|
|
|
'transmission' => [ |
|
12
|
|
|
[ |
|
13
|
|
|
'host' => 'localhost', |
|
14
|
|
|
'port' => 9091, |
|
15
|
|
|
'user' => '', |
|
16
|
|
|
'password' => '', |
|
17
|
|
|
], |
|
18
|
|
|
], |
|
19
|
|
|
|
|
20
|
|
|
'influxdb-host' => 'localhost', |
|
21
|
|
|
'influxdb-port' => 8086, |
|
22
|
|
|
'influxdb-database' => 'transmission', |
|
23
|
|
|
'influxdb-user' => '', |
|
24
|
|
|
'influxdb-password' => '', |
|
25
|
|
|
|
|
26
|
|
|
'download-torrents-dir' => '', |
|
27
|
|
|
'download-imdb-min' => '8.0', |
|
28
|
|
|
'download-kinopoisk-min' => '8.0', |
|
29
|
|
|
'download-comments-min' => 10, |
|
30
|
|
|
'download-votes-min' => 25, |
|
31
|
|
|
|
|
32
|
|
|
'download-filename-whitelist' => [], |
|
33
|
|
|
'download-filename-blacklist' => [], |
|
34
|
|
|
|
|
35
|
|
|
'weburg-series-list' => [], |
|
36
|
|
|
'weburg-series-max-age' => 1, |
|
37
|
|
|
'weburg-series-allowed-misses' => 1, |
|
38
|
|
|
'weburg-request-delay' => 2, |
|
39
|
|
|
); |
|
40
|
|
|
|
|
41
|
|
|
private $config; |
|
42
|
|
|
private $configFile; |
|
43
|
|
|
|
|
44
|
|
|
public function __construct($configFile = null) |
|
45
|
|
|
{ |
|
46
|
|
|
$this->config = self::$defaultConfig; |
|
47
|
|
|
|
|
48
|
|
|
$this->configFile = $configFile; |
|
49
|
|
|
if (!isset($configFile)) { |
|
50
|
|
|
$this->configFile = self::getHomeDir() . '/.transmission-cli.yml'; |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function loadConfigFile() |
|
55
|
|
|
{ |
|
56
|
|
|
if (!file_exists($this->configFile)) { |
|
57
|
|
|
if ($this->configFile == self::getHomeDir() . '/.transmission-cli.yml') { |
|
58
|
|
|
$this->saveConfigFile(); |
|
59
|
|
|
} else { |
|
60
|
|
|
throw new \RuntimeException('Config file not found: ' . $this->configFile); |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
$yml = Yaml::parse(file_get_contents($this->configFile)); |
|
64
|
|
|
if (!is_array($yml)) { |
|
65
|
|
|
throw new \RuntimeException('Config file corrupted: ' . $this->configFile); |
|
66
|
|
|
} |
|
67
|
|
|
$this->config = $yml + $this->config; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function saveConfigFile() |
|
71
|
|
|
{ |
|
72
|
|
|
$configRaw = Yaml::dump($this->config, 2); |
|
73
|
|
|
file_put_contents($this->configFile, $configRaw); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function get($key) |
|
77
|
|
|
{ |
|
78
|
|
|
if (!isset($this->config[$key])) { |
|
79
|
|
|
return null; |
|
80
|
|
|
} |
|
81
|
|
|
return $this->config[$key]; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function set($key, $value) |
|
85
|
|
|
{ |
|
86
|
|
|
$this->config[$key] = $value; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public function overrideConfig(InputInterface $input, $optionName, $configName = null) |
|
90
|
|
|
{ |
|
91
|
|
|
if (!isset($configName)) { |
|
92
|
|
|
$configName = $optionName; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
$optionValue = $input->hasOption($optionName) ? $input->getOption($optionName) : null; |
|
96
|
|
|
if (isset($optionValue)) { |
|
97
|
|
|
$this->set($configName, $optionValue); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
return $this->get($configName); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
public static function getHomeDir() |
|
104
|
|
|
{ |
|
105
|
|
|
// environment variable 'HOME' isn't set on Windows and generates a Notice. |
|
106
|
|
|
$home = getenv('HOME'); |
|
107
|
|
|
if (!empty($home)) { |
|
108
|
|
|
// home should never end with a trailing slash. |
|
109
|
|
|
$home = rtrim($home, '/'); |
|
110
|
|
|
} elseif (!empty($_SERVER['HOMEDRIVE']) && !empty($_SERVER['HOMEPATH'])) { |
|
111
|
|
|
// home on windows |
|
112
|
|
|
$home = $_SERVER['HOMEDRIVE'] . $_SERVER['HOMEPATH']; |
|
113
|
|
|
// If HOMEPATH is a root directory the path can end with a slash. Make sure |
|
114
|
|
|
// that doesn't happen. |
|
115
|
|
|
$home = rtrim($home, '\\/'); |
|
116
|
|
|
} |
|
117
|
|
|
return empty($home) ? null : $home; |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|