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, 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
|
|
|
public function getCharset() |
25
|
|
|
{ |
26
|
|
|
return Yii::$app->charset ?: mb_internal_encoding(); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function renderH($title, $char) |
30
|
|
|
{ |
31
|
|
|
$res = $title . "\n"; |
32
|
|
|
$res .= str_repeat($char, mb_strlen($title, $this->getCharset())); |
33
|
|
|
|
34
|
|
|
return $res . "\n"; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function renderH1($title) |
38
|
|
|
{ |
39
|
|
|
return $this->renderH($title, '='); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function renderH2($title) |
43
|
|
|
{ |
44
|
|
|
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
|
|
|
if (!$this->takeGoal('package')->getPackageManager()->getConfiguration()->getRequire()) { |
109
|
|
|
unset($badges['versioneye.dependencies']); |
110
|
|
|
} |
111
|
|
|
$res = ''; |
112
|
|
|
foreach ($badges as $badge => $tpl) { |
113
|
|
|
if (!$tpl) { |
114
|
|
|
$tpl = $this->markdownBadges[$badge]; |
|
|
|
|
115
|
|
|
} |
116
|
|
|
if ($tpl === 'disabled') { |
117
|
|
|
continue; |
118
|
|
|
} |
119
|
|
|
$res .= $this->renderBadge($tpl) . "\n"; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return $res ? "\n$res" : ''; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function renderBadge($tpl) |
126
|
|
|
{ |
127
|
|
|
error_log( "BADGE: '$tpl'\n" ); |
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@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.