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