1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PHPMV; |
4
|
|
|
|
5
|
|
|
use PHPMV\js\JavascriptUtils; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* php-vueJS Manager class. |
9
|
|
|
* |
10
|
|
|
* PHPMV$VueManager |
11
|
|
|
* This class is part of php-vuejs |
12
|
|
|
* |
13
|
|
|
* @author jguillaumesio |
14
|
|
|
* @version 1.0.0 |
15
|
|
|
* |
16
|
|
|
*/ |
17
|
|
|
class VueManager { |
18
|
|
|
protected static ?VueManager $instance = null; |
19
|
|
|
protected ?object $container; |
20
|
|
|
protected array $imports; |
21
|
|
|
protected array $vues; |
22
|
|
|
protected bool $useAxios; |
23
|
|
|
protected $config; |
24
|
|
|
|
25
|
2 |
|
protected function __construct() { |
26
|
2 |
|
$this->imports = []; |
27
|
2 |
|
$this->vues = []; |
28
|
2 |
|
$this->useAxios = false; |
29
|
2 |
|
} |
30
|
|
|
|
31
|
2 |
|
public static function getInstance(?object $container=null): ?VueManager { |
32
|
2 |
|
if (!isset(self::$instance)) { |
33
|
2 |
|
self::$instance = new static(); |
34
|
|
|
} |
35
|
2 |
|
if(isset($container)){ |
36
|
|
|
self::$instance->container=$container; |
37
|
|
|
} |
38
|
2 |
|
return self::$instance; |
39
|
|
|
} |
40
|
|
|
|
41
|
2 |
|
public static function deleteInstance(): void { |
42
|
2 |
|
VueManager::$instance = null; |
43
|
2 |
|
} |
44
|
|
|
|
45
|
1 |
|
protected function addImport($import): void { |
46
|
1 |
|
$this->imports[] = $import; |
47
|
1 |
|
} |
48
|
|
|
|
49
|
1 |
|
protected function addGlobal(string $type, string $body, string $name = null): void { |
50
|
1 |
|
if ($name) { |
51
|
1 |
|
$this->addImport("Vue.$type('$name',$body);"); |
52
|
|
|
} else { |
53
|
1 |
|
$this->addImport("Vue.$type($body);"); |
54
|
|
|
} |
55
|
1 |
|
} |
56
|
|
|
|
57
|
1 |
|
public function importComponentObject(VueJSComponent $component): void { //component, mixin, or extend |
58
|
1 |
|
$this->addImport(JavascriptUtils::declareVariable('const', $component->getVarName(), $component->generateObject(), false)); |
59
|
1 |
|
} |
60
|
|
|
|
61
|
1 |
|
public function addGlobalDirective(string $name, array $hookFunction) { |
62
|
1 |
|
foreach ($hookFunction as $key => $value) { |
63
|
1 |
|
$hookFunction[$key] = JavascriptUtils::generateFunction($value, ['el', 'binding', 'vnode', 'oldVnode']); |
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', JavascriptUtils::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(JavascriptUtils::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
|
|
|
/** |
93
|
|
|
* Creates and returns a new VueJS instance. |
94
|
|
|
* |
95
|
|
|
* @param string $element |
96
|
|
|
* @param string|null $varName |
97
|
|
|
* @param false $useVuetify |
98
|
|
|
* @return VueJS |
99
|
|
|
*/ |
100
|
|
|
public function createVue(string $element,?string $varName=null,$useVuetify=false): VueJS { |
101
|
|
|
$config=$this->config; |
102
|
|
|
$config['el']=$element; |
103
|
|
|
$varName??='app'.(\count($this->vues)+1); |
104
|
|
|
return $this->vues[]=new VueJS($config,$varName,$useVuetify); |
105
|
|
|
} |
106
|
|
|
|
107
|
1 |
|
public function __toString(): string { |
108
|
1 |
|
$script = ''; |
109
|
1 |
|
if ($this->useAxios) $script = 'Vue.prototype.$http = axios;' . PHP_EOL; |
110
|
1 |
|
$script .= \implode(PHP_EOL, $this->imports); |
111
|
1 |
|
$script .= PHP_EOL . \implode(PHP_EOL, $this->vues); |
112
|
1 |
|
$script = JavascriptUtils::cleanJSONFunctions($script); |
113
|
1 |
|
return JavascriptUtils::wrapScript($script); |
114
|
|
|
} |
115
|
|
|
|
116
|
1 |
|
public function setAxios(bool $useAxios): void { |
117
|
1 |
|
$this->useAxios = $useAxios; |
118
|
|
|
} |
119
|
|
|
} |