Readme   A
last analyzed

Complexity

Total Complexity 35

Size/Duplication

Total Lines 182
Duplicated Lines 0 %

Test Coverage

Coverage 8.82%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 35
eloc 56
c 3
b 1
f 0
dl 0
loc 182
ccs 6
cts 68
cp 0.0882
rs 9.6

17 Methods

Rating   Name   Duplication   Size   Complexity  
A save() 0 4 3
A renderText() 0 5 2
A setSections() 0 3 1
A renderH1() 0 3 1
A renderBadges() 0 19 6
A renderBold() 0 5 1
A getTwig() 0 7 2
A renderSections() 0 11 3
A getTemplate() 0 3 1
A getSections() 0 7 2
A getCharset() 0 3 3
A getSection() 0 10 3
A renderH2() 0 3 1
A renderH3() 0 3 1
A renderH() 0 3 1
A renderSection() 0 8 3
A renderBadge() 0 5 1
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\components;
12
13
use hidev\helpers\FileHelper;
14
use hidev\helpers\Helper;
15
use Yii;
16
17
/**
18
 * README generation component.
19
 * @author Andrii Vasyliev <[email protected]>
20
 */
21
class Readme extends \hidev\base\Component
22
{
23
    /**
24
     * @var \Twig\Environment
25
     */
26
    protected $_twig;
27
28
    /**
29
     * @var array list of sections to render
30
     */
31
    protected $_sections;
32
33
    public $knownBadges = [];
34
35
    public $badges;
36
37
    public $forceRewrite = false;
38
39
    public function save($path, $type)
40
    {
41
        if ($this->forceRewrite || !file_exists($path)) {
42
            FileHelper::write($path, $this->render($this->getTemplate($type)));
43
        }
44
    }
45
46
    public function getTemplate($type)
47
    {
48
        return 'readme-' . $type;
49
    }
50
51
    /**
52
     * Get charset.
53
     * @return string
54
     */
55
    public function getCharset()
56
    {
57
        return (isset(Yii::$app->charset) ? Yii::$app->charset : null) ?: mb_internal_encoding();
0 ignored issues
show
Bug Best Practice introduced by
The expression return IssetNode ? Yii::... mb_internal_encoding() also could return the type boolean which is incompatible with the documented return type string.
Loading history...
58
    }
59
60 2
    public function renderH($title, $prefix)
61
    {
62 2
        return $prefix . ' ' . $title . "\n";
63
    }
64
65 1
    public function renderH1($title)
66
    {
67 1
        return $this->renderH($title, '#');
68
    }
69
70 1
    public function renderH2($title)
71
    {
72 1
        return $this->renderH($title, '##');
73
    }
74
75
    public function renderH3($title)
76
    {
77
        return $this->renderH($title, '###');
78
    }
79
80
    public function renderText($text)
81
    {
82
        $text = trim($text);
83
84
        return $text ? "\n$text\n" : '';
85
    }
86
87
    public function renderBold($text)
88
    {
89
        $text = trim($text);
90
91
        return $this->renderText('**' . $text . '**');
92
    }
93
94
    public function renderSection($section, $default = null)
95
    {
96
        $file = 'readme/' . str_replace(' ', '', $section);
97
        $path = Yii::getAlias("@root/docs/$file.md");
98
        $text = file_exists($path) ? file_get_contents($path) : $this->getSection($file, $default);
99
        $text = trim($text);
100
101
        return $text ? "\n## $section\n\n$text\n" : '';
102
    }
103
104
    public function getSection($file, $default = null)
105
    {
106
        $tpl = '@hidev/views/' . Helper::file2template($file);
107
        try {
108
            $res = $this->render($tpl);
109
        } catch (\Exception $e) {
110
            $res = '';
111
        }
112
113
        return $res ?: $default;
114
    }
115
116
    /**
117
     * Set sections list.
118
     * @param array $value
119
     */
120
    public function setSections($value)
121
    {
122
        $this->_sections = (array) $value;
123
    }
124
125
    /**
126
     * Returns sections list. Returns default list if not set.
127
     * @return array
128
     */
129
    public function getSections()
130
    {
131
        if (empty($this->_sections)) {
132
            $this->_sections = ['Requirements', 'Installation', 'Idea', 'Configuration', 'Basic Usage', 'Usage', 'Support', 'Disclaimer', 'License', 'Acknowledgements', 'Acknowledgments'];
133
        }
134
135
        return array_unique($this->_sections);
136
    }
137
138
    /**
139
     * Render all configured sections.
140
     * @return string
141
     */
142
    public function renderSections($sections = null)
143
    {
144
        if ($sections === null) {
145
            $sections = $this->getSections();
146
        }
147
        $res = '';
148
        foreach ($sections as $section) {
149
            $res .= $this->renderSection($section);
150
        }
151
152
        return $res;
153
    }
154
155
    /**
156
     * Render all configured badges.
157
     * @return string
158
     */
159
    public function renderBadges()
160
    {
161
        $badges = $this->badges;
162
        if (!$badges) {
163
            return '';
164
        }
165
        $pm = $this->take('package')->getPackageManager();
0 ignored issues
show
Unused Code introduced by
The assignment to $pm is dead and can be removed.
Loading history...
166
        $res = '';
167
        foreach ($badges as $badge => $tpl) {
168
            if (!$tpl) {
169
                $tpl = $this->knownBadges[$badge];
170
            }
171
            if ($tpl === 'disabled') {
172
                continue;
173
            }
174
            $res .= $this->renderBadge($tpl) . "\n";
175
        }
176
177
        return $res ? "\n$res" : '';
178
    }
179
180
    /**
181
     * Render badge by given template.
182
     * @param string $template string to render
183
     * @return string
184
     */
185
    public function renderBadge($template)
186
    {
187
        $tpl = $this->getTwig()->createTemplate($template);
188
189
        return $tpl->render(['app' => Yii::$app]);
190
    }
191
192
    /**
193
     * Twig getter.
194
     * @return \Twig\Environment
195
     */
196
    public function getTwig()
197
    {
198
        if ($this->_twig === null) {
199
            $this->_twig = new \Twig\Environment(new \Twig\Loader\ArrayLoader());
200
        }
201
202
        return $this->_twig;
203
    }
204
}
205