|
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. |
|
19
|
|
|
*/ |
|
20
|
|
|
class ReadmeController extends \hidev\controllers\TemplateController |
|
21
|
|
|
{ |
|
22
|
|
|
protected $_twig; |
|
23
|
|
|
|
|
24
|
2 |
|
public function getCharset() |
|
25
|
|
|
{ |
|
26
|
2 |
|
return Yii::$app->charset ?: mb_internal_encoding(); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
2 |
|
public function renderH($title, $char) |
|
30
|
|
|
{ |
|
31
|
2 |
|
$res = $title . "\n"; |
|
32
|
2 |
|
$res .= str_repeat($char, mb_strlen($title, $this->getCharset())); |
|
33
|
|
|
|
|
34
|
2 |
|
return $res . "\n"; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
1 |
|
public function renderH1($title) |
|
38
|
|
|
{ |
|
39
|
1 |
|
return $this->renderH($title, '='); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
1 |
|
public function renderH2($title) |
|
43
|
|
|
{ |
|
44
|
1 |
|
return $this->renderH($title, '-'); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function renderText($text) |
|
48
|
|
|
{ |
|
49
|
|
|
$text = trim($text); |
|
50
|
|
|
|
|
51
|
|
|
return $text ? "\n$text\n" : ''; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function renderBold($text) |
|
55
|
|
|
{ |
|
56
|
|
|
$text = trim($text); |
|
57
|
|
|
|
|
58
|
|
|
return $this->renderText('**' . $text . '**'); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function renderSection($section, $default = null) |
|
62
|
|
|
{ |
|
63
|
|
|
$file = 'readme/' . str_replace(' ', '', $section); |
|
64
|
|
|
$path = Yii::getAlias("@prjdir/docs/$file.md"); |
|
65
|
|
|
$text = file_exists($path) ? file_get_contents($path) : $this->getSection($file, $default); |
|
66
|
|
|
$text = trim($text); |
|
67
|
|
|
|
|
68
|
|
|
return $text ? "\n## $section\n\n$text\n" : ''; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function getSection($file, $default = null) |
|
72
|
|
|
{ |
|
73
|
|
|
$view = Yii::$app->getView(); |
|
74
|
|
|
$tpl = Helper::file2template($file); |
|
75
|
|
|
try { |
|
76
|
|
|
$res = $view->render($tpl, ['config' => $this->takeConfig()]); |
|
77
|
|
|
} catch (\Exception $e) { |
|
78
|
|
|
$res = ''; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
return $res ?: $default; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function getSections() |
|
85
|
|
|
{ |
|
86
|
|
|
return $this->sections ?: ['Requirements', 'Installation', 'Idea', 'Configuration', 'Basic Usage', 'Usage', 'Support', 'License', 'Acknowledgments']; |
|
|
|
|
|
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public function renderSections($sections = null) |
|
90
|
|
|
{ |
|
91
|
|
|
if ($sections === null) { |
|
92
|
|
|
$sections = $this->getSections(); |
|
93
|
|
|
} |
|
94
|
|
|
$res = ''; |
|
95
|
|
|
foreach ($sections as $section) { |
|
96
|
|
|
$res .= $this->renderSection($section); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
return $res; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
public function renderBadges() |
|
103
|
|
|
{ |
|
104
|
|
|
$badges = $this->badges; |
|
|
|
|
|
|
105
|
|
|
if (!$badges) { |
|
106
|
|
|
return ''; |
|
107
|
|
|
} |
|
108
|
|
|
$pm = $this->takeGoal('package')->getPackageManager(); |
|
109
|
|
|
if (!$pm || !$pm->getConfiguration()->getRequire()) { |
|
110
|
|
|
unset($badges['versioneye.dependencies']); |
|
111
|
|
|
} |
|
112
|
|
|
$res = ''; |
|
113
|
|
|
foreach ($badges as $badge => $tpl) { |
|
114
|
|
|
if (!$tpl) { |
|
115
|
|
|
$tpl = $this->markdownBadges[$badge]; |
|
|
|
|
|
|
116
|
|
|
} |
|
117
|
|
|
if ($tpl === 'disabled') { |
|
118
|
|
|
continue; |
|
119
|
|
|
} |
|
120
|
|
|
$res .= $this->renderBadge($tpl) . "\n"; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
return $res ? "\n$res" : ''; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
public function renderBadge($tpl) |
|
127
|
|
|
{ |
|
128
|
|
|
return $this->getTwig()->render($tpl, ['config' => $this->takeConfig()]); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
public function getTwig() |
|
132
|
|
|
{ |
|
133
|
|
|
if ($this->_twig === null) { |
|
134
|
|
|
$this->_twig = new \Twig_Environment(new \Twig_Loader_String()); |
|
|
|
|
|
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
return $this->_twig; |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|
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@propertyannotation 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.