1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PHPMV; |
4
|
|
|
|
5
|
|
|
use PHPMV\js\JavascriptUtils; |
6
|
|
|
|
7
|
|
|
class VueJSComponent extends AbstractVueJS { |
8
|
|
|
protected string $name; |
9
|
|
|
protected array $props; |
10
|
|
|
protected array $template; |
11
|
|
|
protected array $extends; |
12
|
|
|
protected string $varName; |
13
|
|
|
protected string $type; |
14
|
1 |
|
protected bool $global; |
15
|
1 |
|
|
16
|
1 |
|
public function __construct(string $name, string $varName = null,string $type='component') { |
|
|
|
|
17
|
1 |
|
parent::__construct([]); |
18
|
1 |
|
$this->name = $name; |
19
|
1 |
|
$this->props = []; |
20
|
1 |
|
$this->extends = []; |
21
|
1 |
|
$this->template = []; |
22
|
|
|
if (!$varName) { |
23
|
1 |
|
$varName = JavascriptUtils::kebabToPascal($name); |
24
|
1 |
|
} |
25
|
|
|
$this->varName = $varName; |
26
|
1 |
|
$this->global=false; |
27
|
1 |
|
} |
28
|
1 |
|
|
29
|
|
|
public function addData(string $name, $value): void { |
30
|
1 |
|
$this->data["data"][$name] = $value; |
31
|
1 |
|
} |
32
|
1 |
|
|
33
|
|
|
public function addDataRaw(string $name, string $value): void { |
34
|
1 |
|
$this->data["data"][$name] = JavascriptUtils::removeQuotes($value); |
35
|
1 |
|
} |
36
|
1 |
|
|
37
|
|
|
public function extends(VueJSComponent $component): void { |
38
|
1 |
|
$this->extends['extends'] = $component->getVarName(); |
39
|
1 |
|
} |
40
|
1 |
|
|
41
|
1 |
|
public function generateObject(): string { |
42
|
|
|
$data = []; |
43
|
1 |
|
if (isset($this->data["data"])) { |
44
|
1 |
|
$data["data"] = JavascriptUtils::generateFunction("return " . JavascriptUtils::arrayToJsObject($this->data["data"])); |
45
|
|
|
} |
46
|
|
|
return JavascriptUtils::arrayToJsObject($this->components + $this->filters + $this->extends + $this->mixins + $this->configuration + $this->props + $data + $this->computeds + $this->watchers + $this->hooks + $this->methods + $this->template); |
47
|
1 |
|
} |
48
|
1 |
|
|
49
|
1 |
|
public function generateGlobalScript(): string { |
50
|
1 |
|
return "Vue.{$this->type}({$this->name},{$this->generateObject()});"; |
51
|
1 |
|
|
52
|
|
|
} |
53
|
|
|
|
54
|
1 |
|
public function generateFile(bool $inVariable = false, bool $global = false): void { |
55
|
1 |
|
$script = ($inVariable) ? JavascriptUtils::declareVariable("const", $this->varName, $this->generateGlobalScript(), false) : $this->generateGlobalScript(); |
56
|
1 |
|
if (!$global) { |
57
|
1 |
|
\file_put_contents("$this->name.js", $script); |
58
|
|
|
} elseif (file_exists("components.js")) { |
59
|
|
|
\file_put_contents("components.js", PHP_EOL . $script, FILE_APPEND); |
60
|
|
|
} else { |
61
|
|
|
\file_put_contents("components.js", $script); |
62
|
|
|
} |
63
|
1 |
|
} |
64
|
|
|
|
65
|
1 |
|
public function addProps(string ...$props): void { |
66
|
1 |
|
$this->props["props"] = $props; |
67
|
1 |
|
} |
68
|
|
|
|
69
|
1 |
|
public function setInheritAttrs(bool $inheritAttrs): void { |
70
|
1 |
|
$this->configuration['inheritAttrs'] = $inheritAttrs; |
71
|
1 |
|
} |
72
|
|
|
|
73
|
1 |
|
public function setModel(string $prop, string $event): void { |
74
|
1 |
|
$this->configuration['model'] = JavascriptUtils::removeQuotes("{ prop: '$prop', event: '$event ' }"); |
75
|
1 |
|
} |
76
|
|
|
|
77
|
1 |
|
public function addTemplate(string $template): void { |
78
|
1 |
|
$this->template["template"] = $template; |
79
|
1 |
|
} |
80
|
|
|
|
81
|
1 |
|
public function onActivated(string $body): void { |
82
|
1 |
|
$this->addHook("activated", $body); |
83
|
1 |
|
} |
84
|
|
|
|
85
|
1 |
|
public function onDeactivated(string $body): void { |
86
|
1 |
|
$this->addHook("deactivated", $body); |
87
|
1 |
|
} |
88
|
|
|
|
89
|
1 |
|
public function getName(): string { |
90
|
1 |
|
return $this->name; |
91
|
|
|
} |
92
|
|
|
|
93
|
1 |
|
public function getVarName(): string { |
94
|
1 |
|
return $this->varName; |
95
|
|
|
} |
96
|
1 |
|
|
97
|
|
|
/** |
98
|
|
|
* @param string $type |
99
|
|
|
* @param bool $global |
100
|
|
|
*/ |
101
|
|
|
public function setTypeAndGlobal(string $type='component',bool $global=true): void { |
102
|
|
|
$this->type = $type; |
103
|
|
|
$this->global=$global; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param bool $global |
108
|
|
|
*/ |
109
|
|
|
public function setGlobal(bool $global): void { |
110
|
|
|
$this->global = $global; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function __toString(): string { |
114
|
|
|
if($this->global){ |
115
|
|
|
return $this->generateGlobalScript(); |
116
|
|
|
} |
117
|
|
|
return JavascriptUtils::declareVariable('const', $this->varName, $this->generateObject(), false); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
} |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.