Test Failed
Push — main ( 5327e9...2642ec )
by Guillaume
03:06
created

VueManager::addGlobalDirective()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 2
dl 0
loc 5
ccs 0
cts 0
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
namespace PHPMV;
3
4
use PHPMV\js\JavascriptUtils;
5
use PHPMV\utils\JsUtils;
6
7
class VueManager{
8
9
    private static ?VueManager $instance = null;
10
    protected array $imports;
11
    protected array $vues;
12
13
    protected function __construct() {
14
        $this->imports = [];
15
        $this->vues = [];
16
    }
17
18
    public static function getInstance():?VueManager {
19
        if (!isset(self::$instance)) {
20
            self::$instance = new VueManager();
21
        }
22
        return self::$instance;
23
    }
24
25
    public static function deleteInstance():void {
26
        VueManager::$instance = null;
27
    }
28
29
    public function addImport($import):void {
30
        $this->imports[] = $import;
31
    }
32
33
    protected function importComponentObject(VueJSComponent $component):void {
34
        $this->addImport(JsUtils::declareVariable('const', $component->getVarName(), $component->generateObject(), false));
35
    }
36
37
    public function importComponent(VueJSComponent $component):void {
38
        $this->importComponentObject($component);
39
    }
40
41
    public function importMixin(VueJSComponent $mixin):void {
42
        $this->importComponentObject($mixin);
43
    }
44
45
    public function importExtend(VueJSComponent $extend):void {
46
        $this->importComponentObject($extend);
47
    }
48
49
    protected function addGlobal(string $type, string $name, string $body):void {
50
        $this->addImport("Vue.".$type."('".$name."',".$body.");");
51
    }
52
53
    public function addGlobalDirective(string $name,array $hookFunction):void {
54
        foreach ($hookFunction as $key => $value){
55
            $hookFunction[$key] = JsUtils::generateFunction($value,['el', 'binding', 'vnode', 'oldVnode'],false);
56
        }
57
        $this->addGlobal('directive', $name, JavascriptUtils::arrayToJsObject($hookFunction));
58
    }
59
60
    public function addGlobalFilter(string $name,string $body, array $params = []):void {
61
        $this->addGlobal('filter', $name, JsUtils::generateFunction($body,$params,false));
62
    }
63
64
    public function addGlobalExtend(VueJSComponent $extend):void {
65
        $this->addGlobal('extend', $extend->getName(), $extend->generateObject());
66
    }
67
68
    public function addGlobalMixin(VueJSComponent $mixin):void {
69
        $this->addGlobal('mixin',$mixin->getName(), $mixin->generateObject());
70
    }
71
72
    public function addGlobalObservable(string $varName, array $object):void {
73
        $this->addImport(JsUtils::declareVariable('const',$varName,"Vue.observable(". JavascriptUtils::arrayToJsObject($object) .")", false));
74
    }
75
76
    public function addGlobalComponent(VueJSGlobalComponent $component):void {
77
        $this->addImport($component->generateGlobalScript());
78
    }
79
80
    public function addVue(VueJS $vue):void {
81
        $this->vues[] = $vue;
82
    }
83
84
    public function __toString():string {
85
        $script = implode("\n",$this->imports);
86
        $script .= implode("\n",$this->vues);
87
        return JavascriptUtils::wrapScript($script);
88
    }
89
90
    public function getImports():array {
91
        return $this->imports;
92
    }
93
}