Completed
Push — master ( 6c3139...bec789 )
by Andrii
12:16
created

Readme::renderBadge()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 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\Helper;
14
use Yii;
15
16
/**
17
 * README generation component.
18
 * @author Andrii Vasyliev <[email protected]>
19
 */
20
class Readme extends \hidev\base\Component
21
{
22
    /**
23
     * @var \Twig_Environment
24
     */
25
    protected $_twig;
26
27
    /**
28
     * @var array list of sections to render
29
     */
30
    protected $_sections;
31
32
    public $knownBadges = [];
33
34
    public $badges;
35
36
    /**
37
     * Get charset.
38
     * @return string
39
     */
40
    public function getCharset()
41
    {
42
        return (isset(Yii::$app->charset) ? Yii::$app->charset : null) ?: mb_internal_encoding();
43
    }
44
45
    public function renderH($title, $prefix)
46
    {
47
        return $prefix . ' ' . $title . "\n";
48
    }
49
50
    public function renderH1($title)
51
    {
52
        return $this->renderH($title, '#');
53
    }
54
55
    public function renderH2($title)
56
    {
57
        return $this->renderH($title, '##');
58
    }
59
60
    public function renderH3($title)
61
    {
62
        return $this->renderH($title, '###');
63
    }
64
65
    public function renderText($text)
66
    {
67
        $text = trim($text);
68
69
        return $text ? "\n$text\n" : '';
70
    }
71
72
    public function renderBold($text)
73
    {
74
        $text = trim($text);
75
76
        return $this->renderText('**' . $text . '**');
77
    }
78
79
    public function renderSection($section, $default = null)
80
    {
81
        $file = 'readme/' . str_replace(' ', '', $section);
82
        $path = Yii::getAlias("@root/docs/$file.md");
83
        $text = file_exists($path) ? file_get_contents($path) : $this->getSection($file, $default);
84
        $text = trim($text);
85
86
        return $text ? "\n## $section\n\n$text\n" : '';
87
    }
88
89
    public function getSection($file, $default = null)
90
    {
91
        $tpl = '@hidev/views/' . Helper::file2template($file);
92
        try {
93
            $res = $this->render($tpl);
94
        } catch (\Exception $e) {
95
            $res = '';
96
        }
97
98
        return $res ?: $default;
99
    }
100
101
    /**
102
     * Set sections list.
103
     * @param array $value
104
     */
105
    public function setSections($value)
106
    {
107
        $this->_sections = (array) $value;
108
    }
109
110
    /**
111
     * Returns sections list. Returns default list if not set.
112
     * @return array
113
     */
114
    public function getSections()
115
    {
116
        if (empty($this->_sections)) {
117
            $this->_sections = ['Requirements', 'Installation', 'Idea', 'Configuration', 'Basic Usage', 'Usage', 'Support', 'License', 'Acknowledgements', 'Acknowledgments'];
118
        }
119
120
        return array_unique($this->_sections);
121
    }
122
123
    /**
124
     * Render all configured sections.
125
     * @return string
126
     */
127
    public function renderSections($sections = null)
128
    {
129
        if ($sections === null) {
130
            $sections = $this->getSections();
131
        }
132
        $res = '';
133
        foreach ($sections as $section) {
134
            $res .= $this->renderSection($section);
135
        }
136
137
        return $res;
138
    }
139
140
    /**
141
     * Render all configured badges.
142
     * @return string
143
     */
144
    public function renderBadges()
145
    {
146
        $badges = $this->badges;
147
        if (!$badges) {
148
            return '';
149
        }
150
        $pm = $this->take('package')->getPackageManager();
151
        if (!$pm || !$pm->getConfiguration()->getRequire()) {
152
            unset($badges['versioneye.dependencies']);
153
        }
154
        $res = '';
155
        foreach ($badges as $badge => $tpl) {
156
            if (!$tpl) {
157
                $tpl = $this->knownBadges[$badge];
158
            }
159
            if ($tpl === 'disabled') {
160
                continue;
161
            }
162
            $res .= $this->renderBadge($tpl) . "\n";
163
        }
164
165
        return $res ? "\n$res" : '';
166
    }
167
168
    /**
169
     * Render badge by given template.
170
     * @param string $template string to render
171
     * @return string
172
     */
173
    public function renderBadge($template)
174
    {
175
        return $this->getTwig()->render($template, ['app' => Yii::$app]);
176
    }
177
178
    /**
179
     * Twig getter.
180
     * @return \Twig_Environment
181
     */
182
    public function getTwig()
183
    {
184
        if ($this->_twig === null) {
185
            $this->_twig = new \Twig_Environment(new \Twig_Loader_String());
0 ignored issues
show
Deprecated Code introduced by
The class Twig_Loader_String has been deprecated with message: since 1.18.1 (to be removed in 2.0)

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
186
        }
187
188
        return $this->_twig;
189
    }
190
}
191