|
1
|
|
|
<?php |
|
2
|
|
|
namespace PHPMV; |
|
3
|
|
|
|
|
4
|
|
|
use PHPMV\js\JavascriptUtils; |
|
5
|
|
|
use PHPMV\utils\JsUtils; |
|
6
|
|
|
|
|
7
|
|
|
class VueJSComponent extends AbstractVueJS{ |
|
8
|
|
|
protected string $name; |
|
9
|
|
|
protected array $props; |
|
10
|
|
|
protected array $template; |
|
11
|
|
|
|
|
12
|
2 |
|
public function __construct(string $template, array $props = []) { |
|
13
|
2 |
|
parent::__construct(); |
|
14
|
2 |
|
$this->props["props"] = $props; |
|
15
|
2 |
|
$this->template["template"] = "'".\str_replace(["\n","\r","\t"]," ",(\file_get_contents($template.'.html',true))."'"); |
|
16
|
2 |
|
$this->name = $template; |
|
17
|
|
|
} |
|
18
|
2 |
|
|
|
19
|
2 |
|
public function generateLocalScript():string { |
|
20
|
2 |
|
$script = JavascriptUtils::arrayToJsObject($this->props + $this->components + $this->filters +$this->data + $this->computeds + $this->watchers = $this->hooks + $this->methods + $this->template); |
|
21
|
|
|
$script = JsUtils::cleanJSONFunctions($script); |
|
22
|
1 |
|
return $script; |
|
23
|
1 |
|
} |
|
24
|
1 |
|
|
|
25
|
|
|
public function generateGlobalScript():string { |
|
26
|
1 |
|
$script = "Vue.component('".$this->name."',"; |
|
27
|
1 |
|
$script .= $this->generateLocalScript(); |
|
28
|
1 |
|
$script .= ");\n"; |
|
29
|
|
|
return $script; |
|
30
|
2 |
|
} |
|
31
|
2 |
|
|
|
32
|
2 |
|
public function generateFile($global = false):void { |
|
33
|
2 |
|
$script = $this->generateGlobalScript(); |
|
34
|
2 |
|
if(!$global){ |
|
35
|
2 |
|
\file_put_contents($this->name.".js",$script); |
|
36
|
2 |
|
} |
|
37
|
|
|
else{ |
|
38
|
|
|
if(file_exists("components.js")){ |
|
39
|
2 |
|
\file_put_contents("components.js",PHP_EOL . $script,FILE_APPEND); |
|
40
|
1 |
|
} |
|
41
|
|
|
else{ |
|
42
|
|
|
\file_put_contents("components.js",$script); |
|
43
|
1 |
|
} |
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
2 |
|
public function onActivated(string $body):void { |
|
48
|
|
|
$this->addHook("activated", $body); |
|
49
|
|
|
} |
|
50
|
2 |
|
|
|
51
|
2 |
|
public function onDeactivated(string $body):void { |
|
52
|
|
|
$this->addHook("deactivated", $body); |
|
53
|
1 |
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function getName():string { |
|
56
|
|
|
return $this->name; |
|
57
|
|
|
} |
|
58
|
|
|
} |