|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* README plugin for HiDev |
|
5
|
|
|
* |
|
6
|
|
|
* @link https://github.com/hiqdev/hidev-readme |
|
7
|
|
|
* @package hidev-readme |
|
8
|
|
|
* @license BSD-3-Clause |
|
9
|
|
|
* @copyright Copyright (c) 2015-2016, HiQDev (http://hiqdev.com/) |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace hidev\readme\controllers; |
|
13
|
|
|
|
|
14
|
|
|
use hidev\helpers\Helper; |
|
15
|
|
|
use Yii; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Goal for README.file generation. |
|
19
|
|
|
* @author Andrii Vasyliev <[email protected]> |
|
20
|
|
|
*/ |
|
21
|
|
|
class ReadmeController extends \hidev\controllers\TemplateController |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var \Twig_Environment |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $_twig; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var array list of sections to render |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $_sections; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Get charset. |
|
35
|
|
|
* @return string |
|
36
|
|
|
*/ |
|
37
|
4 |
|
public function getCharset() |
|
38
|
|
|
{ |
|
39
|
4 |
|
return (isset(Yii::$app->charset) ? Yii::$app->charset : null) ?: mb_internal_encoding(); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
4 |
|
public function renderH($title, $char) |
|
43
|
|
|
{ |
|
44
|
4 |
|
$res = $title . "\n"; |
|
45
|
4 |
|
$res .= str_repeat($char, mb_strlen($title, $this->getCharset())); |
|
46
|
|
|
|
|
47
|
4 |
|
return $res . "\n"; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
3 |
|
public function renderH1($title) |
|
51
|
|
|
{ |
|
52
|
3 |
|
return $this->renderH($title, '='); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
1 |
|
public function renderH2($title) |
|
56
|
|
|
{ |
|
57
|
1 |
|
return $this->renderH($title, '-'); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
1 |
|
public function renderText($text) |
|
61
|
|
|
{ |
|
62
|
1 |
|
$text = trim($text); |
|
63
|
|
|
|
|
64
|
1 |
|
return $text ? "\n$text\n" : ''; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
1 |
|
public function renderBold($text) |
|
68
|
|
|
{ |
|
69
|
1 |
|
$text = trim($text); |
|
70
|
|
|
|
|
71
|
1 |
|
return $this->renderText('**' . $text . '**'); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
2 |
|
public function renderSection($section, $default = null) |
|
75
|
|
|
{ |
|
76
|
2 |
|
$file = 'readme/' . str_replace(' ', '', $section); |
|
77
|
2 |
|
$path = Yii::getAlias("@root/docs/$file.md"); |
|
78
|
2 |
|
$text = file_exists($path) ? file_get_contents($path) : $this->getSection($file, $default); |
|
79
|
2 |
|
$text = trim($text); |
|
80
|
|
|
|
|
81
|
2 |
|
return $text ? "\n## $section\n\n$text\n" : ''; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
2 |
|
public function getSection($file, $default = null) |
|
85
|
|
|
{ |
|
86
|
2 |
|
$view = Yii::$app->getView(); |
|
87
|
2 |
|
$tpl = Helper::file2template($file); |
|
88
|
|
|
try { |
|
89
|
2 |
|
$res = $view->render($tpl, ['config' => $this->takeConfig()]); |
|
90
|
2 |
|
} catch (\Exception $e) { |
|
91
|
2 |
|
$res = ''; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
2 |
|
return $res ?: $default; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Set sections list. |
|
99
|
|
|
* @param array $value |
|
100
|
|
|
*/ |
|
101
|
|
|
public function setSections($value) |
|
102
|
|
|
{ |
|
103
|
|
|
$this->_sections = (array) $value; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Returns sections list. Returns default list if not set. |
|
108
|
|
|
* @return array |
|
109
|
|
|
*/ |
|
110
|
2 |
|
public function getSections() |
|
111
|
|
|
{ |
|
112
|
2 |
|
if (empty($this->_sections)) { |
|
113
|
2 |
|
$this->_sections = ['Requirements', 'Installation', 'Idea', 'Configuration', 'Basic Usage', 'Usage', 'Support', 'License', 'Acknowledgements', 'Acknowledgments']; |
|
114
|
2 |
|
} |
|
115
|
|
|
|
|
116
|
2 |
|
return $this->_sections; |
|
117
|
1 |
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Render all configured sections. |
|
121
|
|
|
* @return string |
|
122
|
|
|
*/ |
|
123
|
2 |
|
public function renderSections($sections = null) |
|
124
|
|
|
{ |
|
125
|
2 |
|
if ($sections === null) { |
|
126
|
2 |
|
$sections = $this->getSections(); |
|
127
|
2 |
|
} |
|
128
|
2 |
|
$res = ''; |
|
129
|
2 |
|
foreach ($sections as $section) { |
|
130
|
2 |
|
$res .= $this->renderSection($section); |
|
131
|
2 |
|
} |
|
132
|
|
|
|
|
133
|
2 |
|
return $res; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Render all configured badges. |
|
138
|
|
|
* @return string |
|
139
|
|
|
*/ |
|
140
|
2 |
|
public function renderBadges() |
|
141
|
|
|
{ |
|
142
|
2 |
|
$badges = $this->badges; |
|
|
|
|
|
|
143
|
2 |
|
if (!$badges) { |
|
144
|
1 |
|
return ''; |
|
145
|
|
|
} |
|
146
|
1 |
|
$pm = $this->takeGoal('package')->getPackageManager(); |
|
147
|
1 |
|
if (!$pm || !$pm->getConfiguration()->getRequire()) { |
|
148
|
1 |
|
unset($badges['versioneye.dependencies']); |
|
149
|
1 |
|
} |
|
150
|
1 |
|
$res = ''; |
|
151
|
1 |
|
foreach ($badges as $badge => $tpl) { |
|
152
|
1 |
|
if (!$tpl) { |
|
153
|
1 |
|
$tpl = $this->markdownBadges[$badge]; |
|
|
|
|
|
|
154
|
1 |
|
} |
|
155
|
1 |
|
if ($tpl === 'disabled') { |
|
156
|
|
|
continue; |
|
157
|
|
|
} |
|
158
|
1 |
|
$res .= $this->renderBadge($tpl) . "\n"; |
|
159
|
1 |
|
} |
|
160
|
|
|
|
|
161
|
1 |
|
return $res ? "\n$res" : ''; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Render badge by given template. |
|
166
|
|
|
* @param string $template string to render. |
|
167
|
|
|
* @return string |
|
168
|
|
|
*/ |
|
169
|
1 |
|
public function renderBadge($template) |
|
170
|
|
|
{ |
|
171
|
1 |
|
return $this->getTwig()->render($template, ['config' => $this->takeConfig()]); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Twig getter. |
|
176
|
|
|
* @return \Twig_Environment |
|
177
|
|
|
*/ |
|
178
|
1 |
|
public function getTwig() |
|
179
|
|
|
{ |
|
180
|
1 |
|
if ($this->_twig === null) { |
|
181
|
1 |
|
$this->_twig = new \Twig_Environment(new \Twig_Loader_String()); |
|
|
|
|
|
|
182
|
1 |
|
} |
|
183
|
|
|
|
|
184
|
1 |
|
return $this->_twig; |
|
185
|
|
|
} |
|
186
|
|
|
} |
|
187
|
|
|
|
Since your code implements the magic setter
_set, this function will be called for any write access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.