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, array $configuration = []){ |
54
|
|
|
foreach($configuration as $key => $value){ |
55
|
|
|
$configuration[$key] = $value; |
56
|
|
|
} |
57
|
|
|
foreach ($hookFunction as $key => $value){ |
58
|
|
|
$hookFunction[$key] = JsUtils::generateFunction($value,['el', 'binding', 'vnode', 'oldVnode'],false); |
59
|
|
|
} |
60
|
|
|
$this->addGlobal('directive', $name, JavascriptUtils::arrayToJsObject($configuration + $hookFunction)); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function addGlobalFilter(string $name,string $body, array $params = []):void { |
64
|
|
|
$this->addGlobal('filter', $name, JsUtils::generateFunction($body,$params,false)); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function addGlobalExtend(VueJSComponent $extend):void { |
68
|
|
|
$this->addGlobal('extend', $extend->getName(), $extend->generateObject()); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function addGlobalMixin(VueJSComponent $mixin):void { |
72
|
|
|
$this->addGlobal('mixin',$mixin->getName(), $mixin->generateObject()); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function addGlobalObservable(string $varName, array $object):void { |
76
|
|
|
$this->addImport(JsUtils::declareVariable('const',$varName,"Vue.observable(". JavascriptUtils::arrayToJsObject($object) .")", false)); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function addGlobalComponent(VueJSComponent $component):void { |
80
|
|
|
$this->addImport($component->generateGlobalScript()); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function addVue(VueJS $vue):void { |
84
|
|
|
$this->vues[] = $vue; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function __toString():string { |
88
|
|
|
$script = implode("\n",$this->imports); |
89
|
|
|
$script .= implode("\n",$this->vues); |
90
|
|
|
return JavascriptUtils::wrapScript($script); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function getImports():array { |
94
|
|
|
return $this->imports; |
95
|
|
|
} |
96
|
|
|
} |