Completed
Push — master ( 971dc1...d579c1 )
by Andrii
15:48
created

Readme::save()   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 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
    public function renderH($title, $prefix)
57
    {
58
        return $prefix . ' ' . $title . "\n";
59
    }
60
61
    public function renderH1($title)
62
    {
63
        return $this->renderH($title, '#');
64
    }
65
66
    public function renderH2($title)
67
    {
68
        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
        return $this->getTwig()->render($template, ['app' => Yii::$app]);
187
    }
188
189
    /**
190
     * Twig getter.
191
     * @return \Twig_Environment
192
     */
193
    public function getTwig()
194
    {
195
        if ($this->_twig === null) {
196
            $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...
197
        }
198
199
        return $this->_twig;
200
    }
201
}
202