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
|
|
|
public $rmsSite = 'github.com'; |
37
|
|
|
|
38
|
|
|
protected $_changelog = [ |
39
|
|
|
'file' => 'CHANGELOG', |
40
|
|
|
'format' => 'markdown', |
41
|
|
|
]; |
42
|
|
|
|
43
|
|
|
protected $_history = [ |
44
|
|
|
'file' => 'history', |
45
|
|
|
'format' => 'markdown', |
46
|
|
|
]; |
47
|
|
|
|
48
|
|
|
public function bootstrap($app) |
49
|
|
|
{ |
50
|
|
|
if (is_file($this->configFile)) { |
51
|
|
|
$json = file_get_contents($this->configFile); |
52
|
|
|
$data = Json::decode($json); |
53
|
|
|
Yii::configure($this, $data); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function setName($name) |
58
|
|
|
{ |
59
|
|
|
$this->_name = $name; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function getName() |
63
|
|
|
{ |
64
|
|
|
return $this->_name; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function setChangelog(array $options) |
68
|
|
|
{ |
69
|
|
|
$this->_changelog = array_merge($this->_changelog, $options); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function getChangelog($key = null) |
73
|
|
|
{ |
74
|
|
|
if ($key === null) { |
75
|
|
|
return $this->_changelog; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return isset($this->_changelog[$key]) ? $this->_changelog[$key] : null; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function getDest($dest = null, $key = null) |
82
|
|
|
{ |
83
|
|
|
if ($dest === 'changelog') { |
84
|
|
|
return $this->getChangelog($key); |
85
|
|
|
} |
86
|
|
|
if ($dest === 'history') { |
87
|
|
|
return $this->getHistory($key); |
|
|
|
|
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function setAuthors(array $authors) |
92
|
|
|
{ |
93
|
|
|
$this->_authors = $authors; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function getAuthors() |
97
|
|
|
{ |
98
|
|
|
return $this->_authors; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
protected $_options = [ |
102
|
|
|
'markdown' => [ |
103
|
|
|
'extension' => 'md', |
104
|
|
|
'changelog' => [ |
105
|
|
|
'rendererClass' => \hiqdev\chkipper\lib\changelog\MarkdownRenderer::class, |
106
|
|
|
'parserClass' => \hiqdev\chkipper\lib\changelog\MarkdownParser::class, |
|
|
|
|
107
|
|
|
], |
108
|
|
|
], |
109
|
|
|
'keepachangelog' => [ |
110
|
|
|
'extension' => 'md', |
111
|
|
|
'changelog' => [ |
112
|
|
|
'rendererClass' => \hiqdev\chkipper\lib\changelog\KeepAChangelogRenderer::class, |
113
|
|
|
], |
114
|
|
|
], |
115
|
|
|
]; |
116
|
|
|
|
117
|
|
|
public function setOptions(array $options) |
118
|
|
|
{ |
119
|
|
|
$this->_options = ArrayHelper::merge($this->_options, $options); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function getOptions() |
123
|
|
|
{ |
124
|
|
|
return $this->_options; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function getOption($key, $dest = null, $format = null) |
128
|
|
|
{ |
129
|
|
|
if ($format === null) { |
130
|
|
|
$format = strtolower($this->getDest($dest, 'format')); |
131
|
|
|
} |
132
|
|
|
if (empty($this->_options[$format])) { |
133
|
|
|
throw new InvalidConfigException("wrong format '$format' for '$dest'"); |
|
|
|
|
134
|
|
|
} |
135
|
|
|
$options = $this->_options[$format]; |
136
|
|
|
|
137
|
|
|
if (isset($options[$dest][$key])) { |
138
|
|
|
return $options[$dest][$key]; |
139
|
|
|
} |
140
|
|
|
if (!isset($options[$key])) { |
141
|
|
|
throw new InvalidConfigException("wrong option '$key' for '$dest'"); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return $options[$key]; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
public function createChangelogRenderer() |
148
|
|
|
{ |
149
|
|
|
$class = $this->getOption('rendererClass', 'changelog'); |
150
|
|
|
|
151
|
|
|
return Yii::createObject($class); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
public function getChangelogFile() |
155
|
|
|
{ |
156
|
|
|
$file = $this->getChangelog('file'); |
157
|
|
|
$extension = $this->getOption('extension', 'changelog'); |
158
|
|
|
if ($extension) { |
159
|
|
|
$file .= '.' . $extension; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
return $file; |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|