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