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