1
|
|
|
<?php declare( strict_types=1 ); |
2
|
|
|
|
3
|
|
|
namespace BotRiconferme; |
4
|
|
|
|
5
|
|
|
use BotRiconferme\Message\Message; |
6
|
|
|
use BotRiconferme\Wiki\Page\Page; |
7
|
|
|
use BotRiconferme\Wiki\Page\PageBotList; |
8
|
|
|
use BotRiconferme\Wiki\User; |
9
|
|
|
use BotRiconferme\Wiki\Wiki; |
10
|
|
|
use BotRiconferme\Wiki\WikiGroup; |
11
|
|
|
use Psr\Log\LoggerAwareInterface; |
12
|
|
|
use Psr\Log\LoggerInterface; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Base class with a few utility methods available to get a logger, the config and a wiki |
16
|
|
|
*/ |
17
|
|
|
abstract class ContextSource implements LoggerAwareInterface { |
18
|
|
|
/** @var LoggerInterface */ |
19
|
|
|
private $logger; |
20
|
|
|
|
21
|
|
|
/** @var Config */ |
22
|
|
|
private $config; |
23
|
|
|
|
24
|
|
|
/** @var WikiGroup */ |
25
|
|
|
private $wikiGroup; |
26
|
|
|
|
27
|
|
|
/** @var MessageProvider */ |
28
|
|
|
private $messageProvider; |
29
|
|
|
|
30
|
|
|
/** @var PageBotList */ |
31
|
|
|
private $pageBotList; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param LoggerInterface $logger |
35
|
|
|
* @param WikiGroup $wikiGroup |
36
|
|
|
* @param MessageProvider $mp |
37
|
|
|
* @param PageBotList $pbl |
38
|
|
|
*/ |
39
|
|
|
public function __construct( |
40
|
|
|
LoggerInterface $logger, |
41
|
|
|
WikiGroup $wikiGroup, |
42
|
|
|
MessageProvider $mp, |
43
|
|
|
PageBotList $pbl |
44
|
|
|
) { |
45
|
|
|
$this->setLogger( $logger ); |
46
|
|
|
$this->setConfig( Config::getInstance() ); |
47
|
|
|
$this->setWikiGroup( $wikiGroup ); |
48
|
|
|
$this->setMessageProvider( $mp ); |
49
|
|
|
$this->pageBotList = $pbl; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return LoggerInterface |
54
|
|
|
*/ |
55
|
|
|
protected function getLogger(): LoggerInterface { |
56
|
|
|
return $this->logger; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @inheritDoc |
61
|
|
|
*/ |
62
|
|
|
public function setLogger( LoggerInterface $logger ): void { |
63
|
|
|
$this->logger = $logger; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Shorthand to $this->getConfig()->get |
68
|
|
|
* |
69
|
|
|
* @param string $optname |
70
|
|
|
* @return mixed |
71
|
|
|
*/ |
72
|
|
|
protected function getOpt( string $optname ) { |
73
|
|
|
return $this->getConfig()->get( $optname ); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return Config |
78
|
|
|
*/ |
79
|
|
|
protected function getConfig(): Config { |
80
|
|
|
return $this->config; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param Config $cfg |
85
|
|
|
*/ |
86
|
|
|
protected function setConfig( Config $cfg ): void { |
87
|
|
|
$this->config = $cfg; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Shorthand |
92
|
|
|
* @return Wiki |
93
|
|
|
*/ |
94
|
|
|
protected function getWiki(): Wiki { |
95
|
|
|
return $this->getWikiGroup()->getMainWiki(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return WikiGroup |
100
|
|
|
*/ |
101
|
|
|
protected function getWikiGroup(): WikiGroup { |
102
|
|
|
return $this->wikiGroup; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param WikiGroup $wikiGroup |
107
|
|
|
*/ |
108
|
|
|
protected function setWikiGroup( WikiGroup $wikiGroup ): void { |
109
|
|
|
$this->wikiGroup = $wikiGroup; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return MessageProvider |
114
|
|
|
*/ |
115
|
|
|
protected function getMessageProvider(): MessageProvider { |
116
|
|
|
return $this->messageProvider; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param MessageProvider $mp |
121
|
|
|
*/ |
122
|
|
|
protected function setMessageProvider( MessageProvider $mp ): void { |
123
|
|
|
$this->messageProvider = $mp; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Get a message |
128
|
|
|
* |
129
|
|
|
* @param string $key |
130
|
|
|
* @return Message |
131
|
|
|
*/ |
132
|
|
|
protected function msg( string $key ): Message { |
133
|
|
|
return $this->messageProvider->getMessage( $key ); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @return PageBotList |
138
|
|
|
*/ |
139
|
|
|
public function getBotList(): PageBotList { |
140
|
|
|
return $this->pageBotList; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Shorthand to get a page using the local wiki |
145
|
|
|
* |
146
|
|
|
* @param string $title |
147
|
|
|
* @return Page |
148
|
|
|
*/ |
149
|
|
|
protected function getPage( string $title ): Page { |
150
|
|
|
return new Page( $title, $this->getWiki() ); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Shorthand to get a user using the local wiki |
155
|
|
|
* |
156
|
|
|
* @param string $name |
157
|
|
|
* @return User |
158
|
|
|
*/ |
159
|
|
|
protected function getUser( string $name ): User { |
160
|
|
|
$ui = $this->getBotList()->getUserInfo( $name ); |
161
|
|
|
return new User( $ui, $this->getWiki() ); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|