1
|
|
|
<?php declare( strict_types=1 ); |
2
|
|
|
|
3
|
|
|
namespace BotRiconferme; |
4
|
|
|
|
5
|
|
|
use BotRiconferme\Wiki\Controller; |
6
|
|
|
use BotRiconferme\Wiki\Page\Page; |
7
|
|
|
use Psr\Log\LoggerAwareInterface; |
8
|
|
|
use Psr\Log\LoggerInterface; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Base class with a few utility methods available to get a logger, the config and a wiki controller |
12
|
|
|
*/ |
13
|
|
|
abstract class ContextSource implements LoggerAwareInterface { |
14
|
|
|
/** @var LoggerInterface */ |
15
|
|
|
private $logger; |
16
|
|
|
|
17
|
|
|
/** @var Config */ |
18
|
|
|
private $config; |
19
|
|
|
|
20
|
|
|
/** @var Controller */ |
21
|
|
|
private $controller; |
22
|
|
|
|
23
|
|
|
public function __construct( Logger $logger, Controller $controller ) { |
24
|
|
|
$this->setLogger( $logger ); |
25
|
|
|
$this->setConfig( Config::getInstance() ); |
26
|
|
|
$this->setController( $controller ); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @return LoggerInterface |
31
|
|
|
*/ |
32
|
|
|
protected function getLogger() : LoggerInterface { |
33
|
|
|
return $this->logger; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @inheritDoc |
38
|
|
|
*/ |
39
|
|
|
public function setLogger( LoggerInterface $logger ) { |
40
|
|
|
$this->logger = $logger; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Shorthand to $this->getConfig()->get |
45
|
|
|
* |
46
|
|
|
* @param string $optname |
47
|
|
|
* @return mixed |
48
|
|
|
*/ |
49
|
|
|
protected function getOpt( string $optname ) { |
50
|
|
|
return $this->getConfig()->get( $optname ); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @return Config |
55
|
|
|
*/ |
56
|
|
|
protected function getConfig() : Config { |
57
|
|
|
return $this->config; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param Config $cfg |
62
|
|
|
*/ |
63
|
|
|
protected function setConfig( Config $cfg ) { |
64
|
|
|
$this->config = $cfg; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return Controller |
69
|
|
|
*/ |
70
|
|
|
protected function getController() : Controller { |
71
|
|
|
return $this->controller; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param Controller $controller |
76
|
|
|
*/ |
77
|
|
|
protected function setController( Controller $controller ) { |
78
|
|
|
$this->controller = $controller; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Get a message |
83
|
|
|
* |
84
|
|
|
* @param string $key |
85
|
|
|
* @return Message |
86
|
|
|
*/ |
87
|
|
|
protected function msg( string $key ) : Message { |
88
|
|
|
return new Message( $key ); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Shorthand to get a page using the local controller |
93
|
|
|
* |
94
|
|
|
* @param string $title |
95
|
|
|
* @return Page |
96
|
|
|
*/ |
97
|
|
|
protected function getPage( string $title ) : Page { |
98
|
|
|
return new Page( $title, $this->getController() ); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|