Plugin   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 5
dl 0
loc 87
ccs 31
cts 31
cp 1
rs 10
c 0
b 0
f 0

4 Methods

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