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

VueManager   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 29
dl 0
loc 85
ccs 0
cts 15
cp 0
rs 10
c 1
b 0
f 0
wmc 20

18 Methods

Rating   Name   Duplication   Size   Complexity  
A addGlobalFilter() 0 2 1
A importExtend() 0 2 1
A addVue() 0 2 1
A addGlobalDirective() 0 5 2
A addGlobalObservable() 0 2 1
A __construct() 0 3 1
A getInstance() 0 5 2
A addImport() 0 2 1
A deleteInstance() 0 2 1
A addGlobalExtend() 0 2 1
A importComponentObject() 0 2 1
A addGlobal() 0 2 1
A __toString() 0 4 1
A importComponent() 0 2 1
A addGlobalComponent() 0 2 1
A addGlobalMixin() 0 2 1
A importMixin() 0 2 1
A getImports() 0 2 1
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
}