1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Changelog keeper |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/hiqdev/chkipper |
6
|
|
|
* @package chkipper |
7
|
|
|
* @license BSD-3-Clause |
8
|
|
|
* @copyright Copyright (c) 2016-2017, HiQDev (http://hiqdev.com/) |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace hiqdev\chkipper\components; |
12
|
|
|
|
13
|
|
|
use hiqdev\chkipper\lib\ConfigInterface; |
14
|
|
|
use Yii; |
15
|
|
|
use yii\base\BootstrapInterface; |
16
|
|
|
use yii\base\Component; |
17
|
|
|
use yii\helpers\ArrayHelper; |
18
|
|
|
use yii\helpers\Json; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Config class. |
22
|
|
|
* Loads and holds configuration. |
23
|
|
|
* |
24
|
|
|
* @author Andrii Vasyliev <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
class Config extends Component implements BootstrapInterface, ConfigInterface |
27
|
|
|
{ |
28
|
|
|
protected $_name; |
29
|
|
|
|
30
|
|
|
protected $_authors = []; |
31
|
|
|
|
32
|
|
|
public $configFile = 'chkipper.json'; |
33
|
|
|
|
34
|
|
|
public $historyFile = 'history.md'; |
35
|
|
|
|
36
|
|
|
protected $_changelog = [ |
37
|
|
|
'file' => 'CHANGELOG', |
38
|
|
|
'format' => 'markdown', |
39
|
|
|
]; |
40
|
|
|
|
41
|
|
|
protected $_history = [ |
42
|
|
|
'file' => 'history', |
43
|
|
|
'format' => 'markdown', |
44
|
|
|
]; |
45
|
|
|
|
46
|
|
|
public function bootstrap($app) |
47
|
|
|
{ |
48
|
|
|
if (is_file($this->configFile)) { |
49
|
|
|
$json = file_get_contents($this->configFile); |
50
|
|
|
$data = Json::decode($json); |
51
|
|
|
Yii::configure($this, $data); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function setName($name) |
56
|
|
|
{ |
57
|
|
|
$this->_name = $name; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function getName() |
61
|
|
|
{ |
62
|
|
|
return $this->_name; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function setChangelog(array $options) |
66
|
|
|
{ |
67
|
|
|
$this->_changelog = array_merge($this->_changelog, $options); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function getChangelog($key = null) |
71
|
|
|
{ |
72
|
|
|
if ($key === null) { |
73
|
|
|
return $this->_changelog; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return isset($this->_changelog[$key]) ? $this->_changelog[$key] : null; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function getDest($dest = null, $key = null) |
80
|
|
|
{ |
81
|
|
|
if ($dest === 'changelog') { |
82
|
|
|
return $this->getChangelog($key); |
83
|
|
|
} |
84
|
|
|
if ($dest === 'history') { |
85
|
|
|
return $this->getHistory($key); |
|
|
|
|
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function setAuthors(array $authors) |
90
|
|
|
{ |
91
|
|
|
$this->_authors = $authors; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function getAuthors() |
95
|
|
|
{ |
96
|
|
|
return $this->_authors; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
|
100
|
|
|
protected $_options = [ |
101
|
|
|
'markdown' => [ |
102
|
|
|
'extension' => 'md', |
103
|
|
|
'changelog' => [ |
104
|
|
|
'rendererClass' => \hiqdev\chkipper\lib\changelog\MarkdownRenderer::class, |
105
|
|
|
'parserClass' => \hiqdev\chkipper\lib\changelog\MarkdownParser::class, |
106
|
|
|
], |
107
|
|
|
], |
108
|
|
|
'keepachangelog' => [ |
109
|
|
|
'extension' => 'md', |
110
|
|
|
'changelog' => [ |
111
|
|
|
'rendererClass' => \hiqdev\chkipper\lib\changelog\KeepAChangelogRenderer::class, |
112
|
|
|
], |
113
|
|
|
], |
114
|
|
|
]; |
115
|
|
|
|
116
|
|
|
public function setOptions(array $options) |
117
|
|
|
{ |
118
|
|
|
$this->_options = ArrayHelper::merge($this->_options, $options); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function getOptions() |
122
|
|
|
{ |
123
|
|
|
return $this->_options; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function getOption($key, $dest = null, $format = null) |
127
|
|
|
{ |
128
|
|
|
if ($format === null) { |
129
|
|
|
$format = strtolower($this->getDest($dest, 'format')); |
130
|
|
|
} |
131
|
|
|
if (empty($this->_options[$format])) { |
132
|
|
|
throw new InvalidConfigException("wrong format '$format' for '$dest'"); |
133
|
|
|
} |
134
|
|
|
$options = $this->_options[$format]; |
135
|
|
|
|
136
|
|
|
if (isset($options[$dest][$key])) { |
137
|
|
|
return $options[$dest][$key]; |
138
|
|
|
} |
139
|
|
|
if (!isset($options[$key])) { |
140
|
|
|
throw new InvalidConfigException("wrong option '$key' for '$dest'"); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return $options[$key]; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function createChangelogRenderer() |
147
|
|
|
{ |
148
|
|
|
$class = $this->getOption('rendererClass', 'changelog'); |
149
|
|
|
|
150
|
|
|
return Yii::createObject($class); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
public function getChangelogFile() |
154
|
|
|
{ |
155
|
|
|
$file = $this->getChangelog('file'); |
156
|
|
|
$extension = $this->getOption('extension', 'changelog'); |
157
|
|
|
if ($extension) { |
158
|
|
|
$file .= '.' . $extension; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
return $file; |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: