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
|
|
|
* Get charset. |
30
|
|
|
* @return string |
31
|
|
|
*/ |
32
|
4 |
|
public function getCharset() |
33
|
|
|
{ |
34
|
4 |
|
return (isset(Yii::$app->charset) ? Yii::$app->charset : null) ?: mb_internal_encoding(); |
35
|
|
|
} |
36
|
|
|
|
37
|
4 |
|
public function renderH($title, $char) |
38
|
|
|
{ |
39
|
4 |
|
$res = $title . "\n"; |
40
|
4 |
|
$res .= str_repeat($char, mb_strlen($title, $this->getCharset())); |
41
|
|
|
|
42
|
4 |
|
return $res . "\n"; |
43
|
|
|
} |
44
|
|
|
|
45
|
3 |
|
public function renderH1($title) |
46
|
|
|
{ |
47
|
3 |
|
return $this->renderH($title, '='); |
48
|
|
|
} |
49
|
|
|
|
50
|
1 |
|
public function renderH2($title) |
51
|
|
|
{ |
52
|
1 |
|
return $this->renderH($title, '-'); |
53
|
|
|
} |
54
|
|
|
|
55
|
1 |
|
public function renderText($text) |
56
|
|
|
{ |
57
|
1 |
|
$text = trim($text); |
58
|
|
|
|
59
|
1 |
|
return $text ? "\n$text\n" : ''; |
60
|
|
|
} |
61
|
|
|
|
62
|
1 |
|
public function renderBold($text) |
63
|
|
|
{ |
64
|
1 |
|
$text = trim($text); |
65
|
|
|
|
66
|
1 |
|
return $this->renderText('**' . $text . '**'); |
67
|
|
|
} |
68
|
|
|
|
69
|
2 |
|
public function renderSection($section, $default = null) |
70
|
|
|
{ |
71
|
2 |
|
$file = 'readme/' . str_replace(' ', '', $section); |
72
|
2 |
|
$path = Yii::getAlias("@root/docs/$file.md"); |
73
|
2 |
|
$text = file_exists($path) ? file_get_contents($path) : $this->getSection($file, $default); |
74
|
2 |
|
$text = trim($text); |
75
|
|
|
|
76
|
2 |
|
return $text ? "\n## $section\n\n$text\n" : ''; |
77
|
|
|
} |
78
|
|
|
|
79
|
2 |
|
public function getSection($file, $default = null) |
80
|
|
|
{ |
81
|
2 |
|
$view = Yii::$app->getView(); |
82
|
2 |
|
$tpl = Helper::file2template($file); |
83
|
|
|
try { |
84
|
2 |
|
$res = $view->render($tpl, ['config' => $this->takeConfig()]); |
85
|
2 |
|
} catch (\Exception $e) { |
86
|
2 |
|
$res = ''; |
87
|
|
|
} |
88
|
|
|
|
89
|
2 |
|
return $res ?: $default; |
90
|
|
|
} |
91
|
|
|
|
92
|
2 |
|
public function getSections() |
93
|
|
|
{ |
94
|
2 |
|
return $this->sections ?: ['Requirements', 'Installation', 'Idea', 'Configuration', 'Basic Usage', 'Usage', 'Support', 'License', 'Acknowledgements', 'Acknowledgments']; |
|
|
|
|
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Render all configured sections. |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
2 |
|
public function renderSections($sections = null) |
102
|
|
|
{ |
103
|
2 |
|
if ($sections === null) { |
104
|
2 |
|
$sections = $this->getSections(); |
105
|
2 |
|
} |
106
|
2 |
|
$res = ''; |
107
|
2 |
|
foreach ($sections as $section) { |
108
|
2 |
|
$res .= $this->renderSection($section); |
109
|
2 |
|
} |
110
|
|
|
|
111
|
2 |
|
return $res; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Render all configured badges. |
116
|
|
|
* @return string |
117
|
|
|
*/ |
118
|
2 |
|
public function renderBadges() |
119
|
1 |
|
{ |
120
|
2 |
|
$badges = $this->badges; |
|
|
|
|
121
|
2 |
|
if (!$badges) { |
122
|
1 |
|
return ''; |
123
|
|
|
} |
124
|
1 |
|
$pm = $this->takeGoal('package')->getPackageManager(); |
125
|
1 |
|
if (!$pm || !$pm->getConfiguration()->getRequire()) { |
126
|
1 |
|
unset($badges['versioneye.dependencies']); |
127
|
1 |
|
} |
128
|
1 |
|
$res = ''; |
129
|
1 |
|
foreach ($badges as $badge => $tpl) { |
130
|
1 |
|
if (!$tpl) { |
131
|
1 |
|
$tpl = $this->markdownBadges[$badge]; |
|
|
|
|
132
|
1 |
|
} |
133
|
1 |
|
if ($tpl === 'disabled') { |
134
|
|
|
continue; |
135
|
|
|
} |
136
|
1 |
|
$res .= $this->renderBadge($tpl) . "\n"; |
137
|
1 |
|
} |
138
|
|
|
|
139
|
1 |
|
return $res ? "\n$res" : ''; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Render badge by given template. |
144
|
|
|
* @param string $template string to render. |
145
|
|
|
* @return string |
146
|
|
|
*/ |
147
|
1 |
|
public function renderBadge($template) |
148
|
|
|
{ |
149
|
1 |
|
return $this->getTwig()->render($template, ['config' => $this->takeConfig()]); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Twig getter. |
154
|
|
|
* @return \Twig_Environment |
155
|
|
|
*/ |
156
|
1 |
|
public function getTwig() |
157
|
|
|
{ |
158
|
1 |
|
if ($this->_twig === null) { |
159
|
1 |
|
$this->_twig = new \Twig_Environment(new \Twig_Loader_String()); |
|
|
|
|
160
|
1 |
|
} |
161
|
|
|
|
162
|
1 |
|
return $this->_twig; |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read 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.