|
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=["props"=>[]]; |
|
10
|
|
|
protected array $template; |
|
11
|
|
|
|
|
12
|
1 |
|
public function __construct(string $template) { |
|
13
|
1 |
|
parent::__construct(); |
|
14
|
1 |
|
$this->template["template"]="'".\str_replace(["\n","\r","\t"]," ",(\file_get_contents($template.'.html',true))."'"); |
|
15
|
1 |
|
$this->name=$template; |
|
16
|
1 |
|
} |
|
17
|
|
|
|
|
18
|
1 |
|
public function setProps(string ...$name):void { |
|
19
|
1 |
|
$this->props["props"]=$name; |
|
20
|
1 |
|
} |
|
21
|
|
|
|
|
22
|
1 |
|
public function onActivated(string $body):void { |
|
23
|
1 |
|
$this->addHook("activated", $body); |
|
24
|
1 |
|
} |
|
25
|
1 |
|
|
|
26
|
1 |
|
public function onDeactivated(string $body):void { |
|
27
|
1 |
|
$this->addHook("deactivated", $body); |
|
28
|
1 |
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function create(bool $global=false):string { |
|
31
|
1 |
|
$script="Vue.component('".$this->name."',"; |
|
32
|
1 |
|
$script.=JavascriptUtils::arrayToJsObject($this->props + $this->data + $this->methods + $this->computeds + $this->watchers + $this->filters + $this->hooks + $this->template); |
|
33
|
|
|
$script=JsUtils::cleanJSONFunctions($script); |
|
34
|
|
|
$script.=")"; |
|
35
|
1 |
|
if(!$global){ |
|
36
|
|
|
\file_put_contents($this->name.".js",$script); |
|
37
|
|
|
} |
|
38
|
|
|
else{ |
|
39
|
1 |
|
if(file_exists("components.js")){ |
|
40
|
|
|
\file_put_contents("components.js",PHP_EOL . $script,FILE_APPEND); |
|
41
|
|
|
} |
|
42
|
1 |
|
else{ |
|
43
|
1 |
|
\file_put_contents("components.js",$script); |
|
44
|
|
|
} |
|
45
|
1 |
|
|
|
46
|
|
|
} |
|
47
|
|
|
return $script; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function createGlobal():string{ |
|
51
|
|
|
return $this->create(true); |
|
52
|
|
|
} |
|
53
|
|
|
} |