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 array $config; |
24
|
|
|
|
25
|
2 |
|
protected function __construct() { |
26
|
2 |
|
$this->imports = []; |
27
|
2 |
|
$this->vues = []; |
28
|
2 |
|
$this->useAxios = false; |
29
|
2 |
|
} |
30
|
|
|
|
31
|
|
|
protected function getTemplateComponentDirectory(): string { |
32
|
|
|
return $this->config['templateDir'] ?? 'vuejs/'; |
33
|
|
|
} |
34
|
|
|
|
35
|
2 |
|
public static function getInstance(?object $container = null): ?VueManager { |
36
|
2 |
|
if (!isset(self::$instance)) { |
37
|
2 |
|
self::$instance = new static(); |
38
|
|
|
} |
39
|
2 |
|
if (isset($container)) { |
40
|
|
|
self::$instance->container = $container; |
41
|
|
|
} |
42
|
2 |
|
return self::$instance; |
43
|
|
|
} |
44
|
|
|
|
45
|
2 |
|
public static function deleteInstance(): void { |
46
|
2 |
|
VueManager::$instance = null; |
47
|
2 |
|
} |
48
|
|
|
|
49
|
1 |
|
public function importComponentObject(VueJSComponent $component): void { //component, mixin, or extend |
50
|
1 |
|
$this->imports[] = $component; |
51
|
1 |
|
} |
52
|
|
|
|
53
|
1 |
|
public function addGlobalDirective(string $name, array $hookFunction) { |
54
|
1 |
|
foreach ($hookFunction as $key => $value) { |
55
|
1 |
|
$hookFunction[$key] = JavascriptUtils::generateFunction($value, ['el', 'binding', 'vnode', 'oldVnode']); |
56
|
|
|
} |
57
|
1 |
|
$this->imports[] = "Vue.directive('$name'," . JavascriptUtils::arrayToJsObject($hookFunction) . ");"; |
58
|
1 |
|
} |
59
|
|
|
|
60
|
1 |
|
public function addGlobalFilter(string $name, string $body, array $params = []): void { |
61
|
1 |
|
$this->imports[] = "Vue.filter('$name'," . JavascriptUtils::generateFunction($body, $params, false) . ");"; |
62
|
1 |
|
} |
63
|
|
|
|
64
|
1 |
|
public function addGlobalObservable(string $varName, array $object): void { |
65
|
1 |
|
$this->imports[] = JavascriptUtils::declareVariable('const', $varName, "Vue.observable(" . JavascriptUtils::arrayToJsObject($object) . ")", false); |
66
|
1 |
|
} |
67
|
|
|
|
68
|
1 |
|
public function addGlobalMixin(VueJSComponent $mixin): void { |
69
|
1 |
|
$mixin->setTypeAndGlobal('mixin'); |
70
|
1 |
|
$this->imports[] = $mixin; |
71
|
1 |
|
} |
72
|
|
|
|
73
|
1 |
|
public function addGlobalExtend(VueJSComponent $extend): void { |
74
|
1 |
|
$extend->setTypeAndGlobal('extend'); |
75
|
1 |
|
$this->imports[] = $extend; |
76
|
1 |
|
} |
77
|
|
|
|
78
|
1 |
|
public function addGlobalComponent(VueJSComponent $component): void { |
79
|
1 |
|
$component->setGlobal(true); |
80
|
1 |
|
$this->imports[] = $component; |
81
|
1 |
|
} |
82
|
|
|
|
83
|
1 |
|
public function addVue(VueJS $vue): void { |
84
|
1 |
|
$this->vues[] = $vue; |
85
|
1 |
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Creates, adds and returns a new VueJS instance. |
89
|
|
|
* |
90
|
|
|
* @param string $element The dom selector associated with this Vue |
91
|
|
|
* @param string|null $varName The vue variable name |
92
|
|
|
* @param false $useVuetify True if Vuutify is used |
93
|
|
|
* @return VueJS The created vue |
94
|
|
|
*/ |
95
|
|
|
public function createVue(string $element, ?string $varName = null, bool $useVuetify = false): VueJS { |
96
|
|
|
$config = $this->config; |
97
|
|
|
$config['el'] = $element; |
98
|
|
|
$varName ??= 'app' . (\count($this->vues) + 1); |
99
|
|
|
return $this->vues[] = new VueJS($config, $varName, $useVuetify); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Creates, adds and returns a new VueJSComponent instance (component, mixin or extend). |
104
|
|
|
* @param string $name The component name |
105
|
|
|
* @param string|null $varName The component varName |
106
|
|
|
* @param string $type The object type : one of component (default value), extend or mixin |
107
|
|
|
* @return VueJSComponent The created component |
108
|
|
|
*/ |
109
|
|
|
public function createComponent(string $name, string $varName = null, string $type='component'): VueJSComponent { |
110
|
|
|
$compo=new VueJSComponent($name,$varName,$type); |
111
|
|
|
$this->addGlobalComponent($compo); |
112
|
|
|
return $compo; |
113
|
|
|
} |
114
|
|
|
|
115
|
1 |
|
public function __toString(): string { |
116
|
1 |
|
$script = ''; |
117
|
1 |
|
if ($this->useAxios) { |
118
|
1 |
|
$script = 'Vue.prototype.$http = axios;' . \PHP_EOL; |
119
|
|
|
} |
120
|
|
|
|
121
|
1 |
|
if(isset($this->config['delimiters'])){ |
122
|
|
|
$script.='Vue.options.delimiters = '.\json_encode($this->config['delimiters']).';'.\PHP_EOL; |
123
|
|
|
} |
124
|
|
|
|
125
|
1 |
|
$script .= \implode(\PHP_EOL, $this->imports); |
126
|
1 |
|
$script .= \PHP_EOL.\implode(\PHP_EOL, $this->vues); |
127
|
|
|
|
128
|
1 |
|
$script = JavascriptUtils::cleanJSONFunctions($script); |
129
|
1 |
|
return JavascriptUtils::wrapScript($script); |
130
|
|
|
} |
131
|
|
|
|
132
|
1 |
|
public function setAxios(bool $useAxios): void { |
133
|
1 |
|
$this->useAxios = $useAxios; |
134
|
1 |
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Sets the global VueJS configuration array with el, delimiters, useAxios, templateDir. |
138
|
|
|
* |
139
|
|
|
* @param array $config |
140
|
|
|
*/ |
141
|
|
|
public function setConfig(array $config): void { |
142
|
|
|
if (isset($config['useAxios'])) { |
143
|
|
|
$this->setAxios($config['useAxios']); |
144
|
|
|
unset($config['useAxios']); |
145
|
|
|
} |
146
|
|
|
$this->config = $config; |
147
|
|
|
} |
148
|
|
|
} |