Completed
Push — master ( 82fc5a...85e288 )
by Andrii
06:52
created

Readme   A

Complexity

Total Complexity 35

Size/Duplication

Total Lines 183
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 7

Test Coverage

Coverage 8.69%

Importance

Changes 0
Metric Value
wmc 35
lcom 3
cbo 7
dl 0
loc 183
ccs 6
cts 69
cp 0.0869
rs 9
c 0
b 0
f 0

17 Methods

Rating   Name   Duplication   Size   Complexity  
A save() 0 4 1
A getTemplate() 0 4 1
A getCharset() 0 4 3
A renderH() 0 4 1
A renderH1() 0 4 1
A renderH2() 0 4 1
A renderH3() 0 4 1
A renderText() 0 6 2
A renderBold() 0 6 1
A renderSection() 0 9 3
A getSection() 0 11 3
A setSections() 0 4 1
A getSections() 0 8 2
A renderSections() 0 12 3
C renderBadges() 0 23 8
A renderBadge() 0 6 1
A getTwig() 0 8 2
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 function save($path, $type)
38
    {
39
        FileHelper::write($path, $this->render($this->getTemplate($type)));
40
    }
41
42
    public function getTemplate($type)
43
    {
44
        return 'readme-' . $type;
45
    }
46
47
    /**
48
     * Get charset.
49
     * @return string
50
     */
51
    public function getCharset()
52
    {
53
        return (isset(Yii::$app->charset) ? Yii::$app->charset : null) ?: mb_internal_encoding();
54
    }
55
56 2
    public function renderH($title, $prefix)
57
    {
58 2
        return $prefix . ' ' . $title . "\n";
59
    }
60
61 1
    public function renderH1($title)
62
    {
63 1
        return $this->renderH($title, '#');
64
    }
65
66 1
    public function renderH2($title)
67
    {
68 1
        return $this->renderH($title, '##');
69
    }
70
71
    public function renderH3($title)
72
    {
73
        return $this->renderH($title, '###');
74
    }
75
76
    public function renderText($text)
77
    {
78
        $text = trim($text);
79
80
        return $text ? "\n$text\n" : '';
81
    }
82
83
    public function renderBold($text)
84
    {
85
        $text = trim($text);
86
87
        return $this->renderText('**' . $text . '**');
88
    }
89
90
    public function renderSection($section, $default = null)
91
    {
92
        $file = 'readme/' . str_replace(' ', '', $section);
93
        $path = Yii::getAlias("@root/docs/$file.md");
94
        $text = file_exists($path) ? file_get_contents($path) : $this->getSection($file, $default);
95
        $text = trim($text);
96
97
        return $text ? "\n## $section\n\n$text\n" : '';
98
    }
99
100
    public function getSection($file, $default = null)
101
    {
102
        $tpl = '@hidev/views/' . Helper::file2template($file);
103
        try {
104
            $res = $this->render($tpl);
105
        } catch (\Exception $e) {
106
            $res = '';
107
        }
108
109
        return $res ?: $default;
110
    }
111
112
    /**
113
     * Set sections list.
114
     * @param array $value
115
     */
116
    public function setSections($value)
117
    {
118
        $this->_sections = (array) $value;
119
    }
120
121
    /**
122
     * Returns sections list. Returns default list if not set.
123
     * @return array
124
     */
125
    public function getSections()
126
    {
127
        if (empty($this->_sections)) {
128
            $this->_sections = ['Requirements', 'Installation', 'Idea', 'Configuration', 'Basic Usage', 'Usage', 'Support', 'License', 'Acknowledgements', 'Acknowledgments'];
129
        }
130
131
        return array_unique($this->_sections);
132
    }
133
134
    /**
135
     * Render all configured sections.
136
     * @return string
137
     */
138
    public function renderSections($sections = null)
139
    {
140
        if ($sections === null) {
141
            $sections = $this->getSections();
142
        }
143
        $res = '';
144
        foreach ($sections as $section) {
145
            $res .= $this->renderSection($section);
146
        }
147
148
        return $res;
149
    }
150
151
    /**
152
     * Render all configured badges.
153
     * @return string
154
     */
155
    public function renderBadges()
156
    {
157
        $badges = $this->badges;
158
        if (!$badges) {
159
            return '';
160
        }
161
        $pm = $this->take('package')->getPackageManager();
162
        if (!$pm || !$pm->getConfiguration()->getRequire()) {
163
            unset($badges['versioneye.dependencies']);
164
        }
165
        $res = '';
166
        foreach ($badges as $badge => $tpl) {
167
            if (!$tpl) {
168
                $tpl = $this->knownBadges[$badge];
169
            }
170
            if ($tpl === 'disabled') {
171
                continue;
172
            }
173
            $res .= $this->renderBadge($tpl) . "\n";
174
        }
175
176
        return $res ? "\n$res" : '';
177
    }
178
179
    /**
180
     * Render badge by given template.
181
     * @param string $template string to render
182
     * @return string
183
     */
184
    public function renderBadge($template)
185
    {
186
        $tpl = $this->getTwig()->createTemplate($template);
187
188
        return $tpl->render(['app' => Yii::$app]);
189
    }
190
191
    /**
192
     * Twig getter.
193
     * @return \Twig_Environment
194
     */
195
    public function getTwig()
196
    {
197
        if ($this->_twig === null) {
198
            $this->_twig = new \Twig_Environment(new \Twig_Loader_Array());
199
        }
200
201
        return $this->_twig;
202
    }
203
}
204