1
|
|
|
<?php |
2
|
|
|
namespace PHPMV; |
3
|
|
|
|
4
|
|
|
use PHPMV\js\JavascriptUtils; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Created by PhpStorm. |
8
|
|
|
* User: qgorak |
9
|
|
|
* Date: 19/11/2020 |
10
|
|
|
* Time: 14:20 |
11
|
|
|
*/ |
12
|
|
|
class AbstractVueJS { |
13
|
|
|
static private $removeQuote = ["start"=>"!!%","end"=>"%!!"]; |
14
|
|
|
static protected $global; |
15
|
|
|
protected $data; |
16
|
|
|
protected $methods; |
17
|
|
|
protected $computeds; |
18
|
|
|
protected $watchers; |
19
|
|
|
protected $directives; |
20
|
|
|
protected $filters; |
21
|
|
|
protected $hooks; |
22
|
|
|
|
23
|
11 |
|
public function __construct() { |
24
|
11 |
|
$this->data=[]; |
25
|
11 |
|
$this->methods=[]; |
26
|
11 |
|
$this->computeds=[]; |
27
|
11 |
|
$this->watchers=[]; |
28
|
11 |
|
$this->directives=[]; |
29
|
11 |
|
$this->filters=[]; |
30
|
11 |
|
$this->hooks=[]; |
31
|
11 |
|
} |
32
|
|
|
|
33
|
2 |
|
public function addHook(string $name,string $body):void { |
34
|
2 |
|
$this->hooks[$name] = self::generateFunction($body); |
35
|
2 |
|
} |
36
|
|
|
|
37
|
1 |
|
public function onBeforeCreate(string $body):void { |
38
|
1 |
|
$this->addHook("beforeCreate", $body); |
39
|
1 |
|
} |
40
|
|
|
|
41
|
1 |
|
public function onCreated(string $body):void { |
42
|
1 |
|
$this->addHook("created", $body); |
43
|
1 |
|
} |
44
|
|
|
|
45
|
1 |
|
public function onBeforeMount(string $body):void { |
46
|
1 |
|
$this->addHook("beforeMount", $body); |
47
|
1 |
|
} |
48
|
|
|
|
49
|
2 |
|
public function onMounted(string $body):void { |
50
|
2 |
|
$this->addHook("mounted", $body); |
51
|
2 |
|
} |
52
|
|
|
|
53
|
1 |
|
public function onBeforeUpdate(string $body):void { |
54
|
1 |
|
$this->addHook("beforeUpdate", $body); |
55
|
1 |
|
} |
56
|
|
|
|
57
|
1 |
|
public function onUpdated(string $body):void { |
58
|
1 |
|
$this->addHook("updated", $body); |
59
|
1 |
|
} |
60
|
|
|
|
61
|
1 |
|
public function onUpdatedNextTick(string $body):void { |
62
|
1 |
|
$this->addHook("updated", "this.\$nextTick(function () {".$body."})"); |
63
|
1 |
|
} |
64
|
|
|
|
65
|
1 |
|
public function onBeforeDestroy(string $body):void { |
66
|
1 |
|
$this->addHook("beforeDestroy", $body); |
67
|
1 |
|
} |
68
|
|
|
|
69
|
1 |
|
public function onDestroyed(string $body):void { |
70
|
1 |
|
$this->addHook("destroyed", $body); |
71
|
1 |
|
} |
72
|
|
|
|
73
|
3 |
|
public function addData(string $name,$value):void { |
74
|
3 |
|
$this->data["data"][$name]=$value; |
75
|
3 |
|
} |
76
|
|
|
|
77
|
2 |
|
public function addDataRaw(string $name,string $value):void { |
78
|
2 |
|
$this->data["data"][$name]=self::$removeQuote["start"].$value.self::$removeQuote["end"]; |
79
|
2 |
|
} |
80
|
|
|
|
81
|
4 |
|
public function addMethod(string $name,string $body, array $params = []):void { |
82
|
4 |
|
$this->methods["methods"][$name]=self::generateFunction($body,$params); |
83
|
4 |
|
} |
84
|
|
|
|
85
|
3 |
|
public function addComputed(string $name,string $get,string $set=null):void { |
86
|
3 |
|
$vc=(is_null($set)) ? self::generateFunction($get) : self::$removeQuote["start"]."{ get: ".self::generateFunction($get).", set: ".self::generateFunction($set,["v"])." }".self::$removeQuote["end"]; |
87
|
3 |
|
$this->computeds["computeds"][self::$removeQuote["start"].$name.self::$removeQuote["end"]]=$vc; |
88
|
3 |
|
} |
89
|
|
|
|
90
|
2 |
|
public function addWatcher(string $var,string $body,array $params=[]):void { |
91
|
2 |
|
$this->watchers["watch"][$var]=self::generateFunction($body,$params); |
92
|
2 |
|
} |
93
|
|
|
|
94
|
|
|
public function addFilter(string $name,string $body, array $params = []):void { |
95
|
|
|
$this->methods["filters"][self::$removeQuote["start"].$name.self::$removeQuote["end"]]=self::generateFunction($body,$params); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public static function addGlobalFilter(string $name,string $body, array $params = []):void { |
99
|
|
|
self::$global[]=self::$removeQuote["start"]."Vue.filter('".$name."',".self::generateFunction($body,$params).");".self::$removeQuote["end"]; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public static function addGlobalObservable(string $varName, array $object){ |
103
|
|
|
self::$global[]=self::$removeQuote["start"]."const ".$varName." = Vue.observable(". JavascriptUtils::arrayToJsObject($object) .");".self::$removeQuote["end"]; |
104
|
|
|
} |
105
|
|
|
|
106
|
4 |
|
public function getData():array { |
107
|
4 |
|
return $this->data; |
108
|
|
|
} |
109
|
|
|
|
110
|
1 |
|
public function setData(array $data):void { |
111
|
1 |
|
$this->data=$data; |
112
|
1 |
|
} |
113
|
|
|
|
114
|
3 |
|
public function getMethods():array { |
115
|
3 |
|
return $this->methods; |
116
|
|
|
} |
117
|
|
|
|
118
|
1 |
|
public function setMethods(array $methods):void { |
119
|
1 |
|
$this->methods=$methods; |
120
|
1 |
|
} |
121
|
|
|
|
122
|
3 |
|
public function getComputeds():array { |
123
|
3 |
|
return $this->computeds; |
124
|
|
|
} |
125
|
|
|
|
126
|
1 |
|
public function setComputeds(array $computeds):void { |
127
|
1 |
|
$this->computeds = $computeds; |
128
|
1 |
|
} |
129
|
|
|
|
130
|
2 |
|
public function getWatchers():array { |
131
|
2 |
|
return $this->watchers; |
132
|
|
|
} |
133
|
|
|
|
134
|
1 |
|
public function setWatchers(array $watchers):void { |
135
|
1 |
|
$this->watchers = $watchers; |
136
|
1 |
|
} |
137
|
|
|
|
138
|
2 |
|
public function getHooks():array { |
139
|
2 |
|
return $this->hooks; |
140
|
|
|
} |
141
|
|
|
|
142
|
1 |
|
public function setHooks(array $hooks):void { |
143
|
1 |
|
$this->hooks = $hooks; |
144
|
1 |
|
} |
145
|
|
|
|
146
|
6 |
|
public static function generateFunction(string $body, array $params = []):string { |
147
|
6 |
|
return self::$removeQuote["start"]."function(".implode(",",$params)."){".$body."}".self::$removeQuote["end"]; |
148
|
|
|
} |
149
|
|
|
} |