|
1
|
|
|
<?php declare( strict_types=1 ); |
|
2
|
|
|
|
|
3
|
|
|
namespace BotRiconferme; |
|
4
|
|
|
|
|
5
|
|
|
use BotRiconferme\Exception\ConfigException; |
|
6
|
|
|
use BotRiconferme\Exception\MissingPageException; |
|
7
|
|
|
use BotRiconferme\Wiki\Wiki; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Singleton class holding user-defined config |
|
11
|
|
|
*/ |
|
12
|
|
|
class Config { |
|
13
|
|
|
public const REQUIRED_OPTS = [ |
|
14
|
|
|
'username', |
|
15
|
|
|
'list-title', |
|
16
|
|
|
'config-title', |
|
17
|
|
|
'msg-title', |
|
18
|
|
|
'password' |
|
19
|
|
|
]; |
|
20
|
|
|
|
|
21
|
|
|
/** @var self */ |
|
22
|
|
|
private static $instance; |
|
23
|
|
|
/** @var array */ |
|
24
|
|
|
private $opts = []; |
|
25
|
|
|
/** @var Wiki */ |
|
26
|
|
|
private $wiki; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Use self::init() and self::getInstance() |
|
30
|
|
|
* |
|
31
|
|
|
* @param Wiki $wiki |
|
32
|
|
|
*/ |
|
33
|
|
|
private function __construct( Wiki $wiki ) { |
|
34
|
|
|
$this->wiki = $wiki; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Initialize a new self instance with CLI params set and retrieve on-wiki config. |
|
39
|
|
|
* |
|
40
|
|
|
* @param array $defaults |
|
41
|
|
|
* @param Wiki $wiki |
|
42
|
|
|
* @throws ConfigException |
|
43
|
|
|
*/ |
|
44
|
|
|
public static function init( array $defaults, Wiki $wiki ) : void { |
|
45
|
|
|
if ( self::$instance ) { |
|
46
|
|
|
throw new ConfigException( 'Config was already initialized' ); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
$inst = new self( $wiki ); |
|
50
|
|
|
$inst->set( 'list-title', $defaults['list-title'] ); |
|
51
|
|
|
$inst->set( 'msg-title', $defaults['msg-title'] ); |
|
52
|
|
|
$inst->set( 'username', $defaults['username'] ); |
|
53
|
|
|
$inst->set( 'password', $defaults['password'] ); |
|
54
|
|
|
|
|
55
|
|
|
// On-wiki values |
|
56
|
|
|
try { |
|
57
|
|
|
$conf = $inst->wiki->getPageContent( $defaults[ 'config-title' ] ); |
|
58
|
|
|
} catch ( MissingPageException $_ ) { |
|
59
|
|
|
throw new ConfigException( 'Please create a config page.' ); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
foreach ( json_decode( $conf, true ) as $key => $val ) { |
|
63
|
|
|
$inst->set( $key, $val ); |
|
64
|
|
|
} |
|
65
|
|
|
self::$instance = $inst; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Set a config value. |
|
70
|
|
|
* |
|
71
|
|
|
* @param string $key |
|
72
|
|
|
* @param mixed $value |
|
73
|
|
|
*/ |
|
74
|
|
|
protected function set( string $key, $value ) : void { |
|
75
|
|
|
$this->opts[ $key ] = $value; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Generic instance getter |
|
80
|
|
|
* |
|
81
|
|
|
* @return self |
|
82
|
|
|
* @throws ConfigException |
|
83
|
|
|
*/ |
|
84
|
|
|
public static function getInstance() : self { |
|
85
|
|
|
if ( !self::$instance ) { |
|
86
|
|
|
throw new ConfigException( 'Config not yet initialized' ); |
|
87
|
|
|
} |
|
88
|
|
|
return self::$instance; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Get the requested option, or fail if it doesn't exist |
|
93
|
|
|
* |
|
94
|
|
|
* @param string $opt |
|
95
|
|
|
* @return mixed |
|
96
|
|
|
* @throws ConfigException |
|
97
|
|
|
*/ |
|
98
|
|
|
public function get( string $opt ) { |
|
99
|
|
|
if ( !isset( $this->opts[ $opt ] ) ) { |
|
100
|
|
|
throw new ConfigException( "Config option '$opt' not set." ); |
|
101
|
|
|
} |
|
102
|
|
|
return $this->opts[ $opt ]; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|