1
|
|
|
<?php |
2
|
|
|
namespace PHPMV; |
3
|
|
|
|
4
|
|
|
|
5
|
|
|
use PHPMV\js\JavascriptUtils; |
6
|
|
|
use PHPMV\utils\JsUtils; |
7
|
|
|
|
8
|
|
|
class GlobalVueJS{ |
9
|
|
|
|
10
|
|
|
private static ?GlobalVueJS $instance = null; |
11
|
|
|
protected array $global; |
12
|
|
|
|
13
|
|
|
protected function __construct() { |
14
|
|
|
$this->global=[]; |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
public static function getInstance(): GlobalVueJS{ |
18
|
|
|
if (!isset(self::$instance)) { |
19
|
|
|
self::$instance = new GlobalVueJS(); |
20
|
|
|
} |
21
|
|
|
return self::$instance; |
|
|
|
|
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public static function deleteInstance():void { |
25
|
|
|
self::$instance = null; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function addGlobalDirective(string $name,array $hookFunction):void { |
29
|
|
|
foreach ($hookFunction as $key=>$value){ |
30
|
|
|
$hookFunction[$key] = JsUtils::generateFunction($value,['el', 'binding', 'vnode', 'oldVnode'],false); |
31
|
|
|
} |
32
|
|
|
$this->global[]="Vue.directive('".$name."',".JavascriptUtils::arrayToJsObject($hookFunction).");"; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function addGlobalFilter(string $name,string $body, array $params = []):void { |
36
|
|
|
$this->global[]="Vue.filter('".$name."',".JsUtils::generateFunction($body,$params,false).");"; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function addGlobalObservable(string $varName, array $object){ |
40
|
|
|
$this->global[]="const ".$varName." = Vue.observable(". JavascriptUtils::arrayToJsObject($object) .");"; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function __toString():string { |
44
|
|
|
$script=""; |
45
|
|
|
if(!empty($this->global)){ |
46
|
|
|
foreach($this->global as $global){ |
47
|
|
|
$script .= $global."\n"; |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
$script = JsUtils::cleanJSONFunctions($script); |
51
|
|
|
$script = JavascriptUtils::wrapScript($script); |
52
|
|
|
return $script; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function getGlobal():array { |
56
|
|
|
return $this->global; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function setGlobal(array $global):void { |
60
|
|
|
$this->global = $global; |
61
|
|
|
} |
62
|
|
|
} |