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
|
1 |
|
public function renderText($text) |
57
|
|
|
{ |
58
|
1 |
|
$text = trim($text); |
59
|
|
|
|
60
|
1 |
|
return $text ? "\n$text\n" : ''; |
61
|
|
|
} |
62
|
|
|
|
63
|
1 |
|
public function renderBold($text) |
64
|
|
|
{ |
65
|
1 |
|
$text = trim($text); |
66
|
|
|
|
67
|
1 |
|
return $this->renderText('**' . $text . '**'); |
68
|
|
|
} |
69
|
|
|
|
70
|
2 |
|
public function renderSection($section, $default = null) |
71
|
|
|
{ |
72
|
2 |
|
$file = 'readme/' . str_replace(' ', '', $section); |
73
|
2 |
|
$path = Yii::getAlias("@root/docs/$file.md"); |
74
|
2 |
|
$text = file_exists($path) ? file_get_contents($path) : $this->getSection($file, $default); |
75
|
2 |
|
$text = trim($text); |
76
|
|
|
|
77
|
2 |
|
return $text ? "\n## $section\n\n$text\n" : ''; |
78
|
|
|
} |
79
|
|
|
|
80
|
2 |
|
public function getSection($file, $default = null) |
81
|
|
|
{ |
82
|
2 |
|
$view = Yii::$app->getView(); |
83
|
2 |
|
$tpl = Helper::file2template($file); |
84
|
|
|
try { |
85
|
2 |
|
$res = $view->render($tpl, ['config' => $this->takeConfig()]); |
86
|
2 |
|
} catch (\Exception $e) { |
87
|
2 |
|
$res = ''; |
88
|
|
|
} |
89
|
|
|
|
90
|
2 |
|
return $res ?: $default; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Set sections list. |
95
|
|
|
* @param array $value |
96
|
|
|
*/ |
97
|
|
|
public function setSections($value) |
98
|
|
|
{ |
99
|
|
|
$this->_sections = (array) $value; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Returns sections list. Returns default list if not set. |
104
|
|
|
* @return array |
105
|
|
|
*/ |
106
|
2 |
|
public function getSections() |
107
|
|
|
{ |
108
|
2 |
|
if (empty($this->_sections)) { |
109
|
2 |
|
$this->_sections = ['Requirements', 'Installation', 'Idea', 'Configuration', 'Basic Usage', 'Usage', 'Support', 'License', 'Acknowledgements', 'Acknowledgments']; |
110
|
2 |
|
} |
111
|
|
|
|
112
|
2 |
|
return $this->_sections; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Render all configured sections. |
117
|
|
|
* @return string |
118
|
|
|
*/ |
119
|
2 |
|
public function renderSections($sections = null) |
120
|
|
|
{ |
121
|
2 |
|
if ($sections === null) { |
122
|
2 |
|
$sections = $this->getSections(); |
123
|
2 |
|
} |
124
|
2 |
|
$res = ''; |
125
|
2 |
|
foreach ($sections as $section) { |
126
|
2 |
|
$res .= $this->renderSection($section); |
127
|
2 |
|
} |
128
|
|
|
|
129
|
2 |
|
return $res; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Render all configured badges. |
134
|
|
|
* @return string |
135
|
|
|
*/ |
136
|
2 |
|
public function renderBadges() |
137
|
|
|
{ |
138
|
2 |
|
$badges = $this->badges; |
|
|
|
|
139
|
2 |
|
if (!$badges) { |
140
|
|
|
return ''; |
141
|
|
|
} |
142
|
2 |
|
$pm = $this->takeGoal('package')->getPackageManager(); |
143
|
2 |
|
if (!$pm || !$pm->getConfiguration()->getRequire()) { |
144
|
2 |
|
unset($badges['versioneye.dependencies']); |
145
|
2 |
|
} |
146
|
2 |
|
$res = ''; |
147
|
2 |
|
foreach ($badges as $badge => $tpl) { |
148
|
2 |
|
if (!$tpl) { |
149
|
2 |
|
$tpl = $this->markdownBadges[$badge]; |
|
|
|
|
150
|
2 |
|
} |
151
|
2 |
|
if ($tpl === 'disabled') { |
152
|
|
|
continue; |
153
|
|
|
} |
154
|
2 |
|
$res .= $this->renderBadge($tpl) . "\n"; |
155
|
2 |
|
} |
156
|
|
|
|
157
|
2 |
|
return $res ? "\n$res" : ''; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Render badge by given template. |
162
|
|
|
* @param string $template string to render |
163
|
|
|
* @return string |
164
|
|
|
*/ |
165
|
2 |
|
public function renderBadge($template) |
166
|
|
|
{ |
167
|
2 |
|
return $this->getTwig()->render($template, ['config' => $this->takeConfig()]); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Twig getter. |
172
|
|
|
* @return \Twig_Environment |
173
|
|
|
*/ |
174
|
2 |
|
public function getTwig() |
175
|
|
|
{ |
176
|
2 |
|
if ($this->_twig === null) { |
177
|
2 |
|
$this->_twig = new \Twig_Environment(new \Twig_Loader_String()); |
|
|
|
|
178
|
2 |
|
} |
179
|
|
|
|
180
|
2 |
|
return $this->_twig; |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|
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.