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