Completed
Push — master ( fd0858...d5ba90 )
by Andrii
29:08 queued 13:19
created

ReadmeController::getTwig()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4286
cc 2
eloc 4
nc 2
nop 0
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'];
0 ignored issues
show
Documentation introduced by
The property sections does not exist on object<hidev\readme\controllers\ReadmeController>. Since you implemented __get, maybe consider adding a @property annotation.

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.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

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.

Loading history...
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;
0 ignored issues
show
Documentation introduced by
The property badges does not exist on object<hidev\readme\controllers\ReadmeController>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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.

Loading history...
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];
0 ignored issues
show
Documentation introduced by
The property markdownBadges does not exist on object<hidev\readme\controllers\ReadmeController>. Since you implemented __get, maybe consider adding a @property annotation.

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.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

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.

Loading history...
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());
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...
135
        }
136
137
        return $this->_twig;
138
    }
139
}
140