Completed
Push — master ( 30c85e...956e14 )
by Alexey
04:16
created

Plugin::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
crap 1
1
<?php
2
3
namespace dominus77\highlight;
4
5
use Yii;
6
use yii\web\AssetBundle;
7
use yii\web\JsExpression;
8
use yii\helpers\ArrayHelper;
9
10
/**
11
 * Class Plugin
12
 * @package dominus77\highlight
13
 *
14
 * Connect to View:
15
 * \dominus77\highlight\Plugin::register($this);
16
 *
17
 */
18
class Plugin extends AssetBundle
19
{
20
    /**
21
     * @var array
22
     */
23
    public static $options = [];
24
25
    /**
26
     * @var string
27
     */
28
    public $sourcePath;
29
30
    /**
31
     * @var array
32
     */
33
    public $css = [];
34
35
    /**
36
     * @var array
37
     */
38
    public $js = [];
39
40
    /**
41
     * @inheritdoc
42
     */
43 4
    public function init()
44
    {
45 4
        parent::init();
46 4
        $this->sourcePath = __DIR__ . '/src';
47 4
        $this->initPlugin();
48 4
    }
49
50
    /**
51
     * Initialize Highlight Plugin
52
     */
53 4
    public function initPlugin()
54
    {
55 4
        $options = ArrayHelper::merge([
56 4
            'theme' => 'darkula',      // Themes
57
            'lineNumbers' => false,   // Show line numbers
58
            'singleLine' => false,    // Show number if one line
59
            'cssLineNumbers' => true, // CSS style Line Numbers true/false (Optionals)
60 4
            'highlightInit' => new JsExpression("hljs.initHighlightingOnLoad();"), // init Highlight
61 4
            'lineNumbersInit' => '',   // init Line Numbers
62 4
        ], self::$options);
63
64 4
        $view = Yii::$app->getView();
65 4
        $this->css[] = 'styles/' . $options['theme'] . '.css';
66 4
        $this->js[] = 'highlight.pack.js';
67 4
        $view->registerJs($options['highlightInit'], $view::POS_END);
68
69 4
        if ($options['lineNumbers'] === true) {
70 1
            $this->initLineNumbers($options);
71
        }
72 4
    }
73
74
    /**
75
     * LineNumbers
76
     * @param array $options
77
     */
78 1
    public function initLineNumbers($options = [])
79
    {
80 1
        $view = Yii::$app->getView();
81 1
        $this->css[] = ($options['cssLineNumbers'] === true) ? 'css/highlightjs-line-numbers.css' : '';
82 1
        $this->js[] = 'highlightjs-line-numbers.min.js';
83
84 1
        if (empty($options['lineNumbersInit'])) {
85 1
            $lineNumbersOptions = $this->getOptionsLine($options);
86 1
            $options['lineNumbersInit'] = new JsExpression("
87 1
                hljs.initLineNumbersOnLoad({$lineNumbersOptions});
88
            ");
89
        }
90 1
        $view->registerJs($options['lineNumbersInit'], $view::POS_END);
91 1
    }
92
93
    /**
94
     * Options highlightjs-line-numbers
95
     * @param $options
96
     * @return string
97
     */
98 1
    public function getOptionsLine($options)
99
    {
100 1
        return json_encode([
101 1
            'singleLine' => $options['singleLine'],
102
        ]);
103
    }
104
}
105