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
|
|
|
protected bool $global; |
15
|
|
|
|
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
|
1 |
|
$this->varName = $varName??JavascriptUtils::kebabToPascal($name); |
23
|
1 |
|
$this->type=$type; |
24
|
1 |
|
$this->global=false; |
25
|
1 |
|
} |
26
|
|
|
|
27
|
1 |
|
public function addData(string $name, $value): void { |
28
|
1 |
|
$this->data['data'][$name] = $value; |
29
|
1 |
|
} |
30
|
|
|
|
31
|
1 |
|
public function addDataRaw(string $name, string $value): void { |
32
|
1 |
|
$this->data['data'][$name] = JavascriptUtils::removeQuotes($value); |
33
|
1 |
|
} |
34
|
|
|
|
35
|
1 |
|
public function extends(VueJSComponent $component): void { |
36
|
1 |
|
$this->extends['extends'] = $component->getVarName(); |
37
|
1 |
|
} |
38
|
|
|
|
39
|
1 |
|
public function generateObject(): string { |
40
|
1 |
|
$data = []; |
41
|
1 |
|
if (isset($this->data['data'])) { |
42
|
1 |
|
$data['data'] = JavascriptUtils::generateFunction("return " . JavascriptUtils::arrayToJsObject($this->data["data"])); |
43
|
|
|
} |
44
|
1 |
|
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); |
45
|
|
|
} |
46
|
|
|
|
47
|
1 |
|
public function generateGlobalScript(): string { |
48
|
1 |
|
return "Vue.{$this->type}(\"{$this->name}\",{$this->generateObject()});"; |
49
|
|
|
|
50
|
|
|
} |
51
|
|
|
|
52
|
1 |
|
public function generateFile(bool $inVariable = false, bool $global = false): void { |
53
|
1 |
|
$script = ($inVariable) ? JavascriptUtils::declareVariable("const", $this->varName, $this->generateGlobalScript(), false) : $this->generateGlobalScript(); |
54
|
1 |
|
if (!$global) { |
55
|
1 |
|
\file_put_contents("$this->name.js", $script); |
56
|
|
|
} elseif (\file_exists("components.js")) { |
57
|
|
|
\file_put_contents("components.js", \PHP_EOL . $script, \FILE_APPEND); |
58
|
|
|
} else { |
59
|
|
|
\file_put_contents("components.js", $script); |
60
|
|
|
} |
61
|
1 |
|
} |
62
|
|
|
|
63
|
1 |
|
public function addProps(string ...$props): void { |
64
|
1 |
|
$this->props['props'] = $props; |
65
|
1 |
|
} |
66
|
|
|
|
67
|
1 |
|
public function setInheritAttrs(bool $inheritAttrs): void { |
68
|
1 |
|
$this->configuration['inheritAttrs'] = $inheritAttrs; |
69
|
1 |
|
} |
70
|
|
|
|
71
|
1 |
|
public function setModel(string $prop, string $event): void { |
72
|
1 |
|
$this->configuration['model'] = JavascriptUtils::removeQuotes("{ prop: '$prop', event: '$event ' }"); |
73
|
1 |
|
} |
74
|
|
|
|
75
|
1 |
|
public function addTemplate(string $template): void { |
76
|
1 |
|
$this->template['template'] = $template; |
77
|
1 |
|
} |
78
|
|
|
|
79
|
1 |
|
public function onActivated(string $body): void { |
80
|
1 |
|
$this->addHook('activated', $body); |
81
|
1 |
|
} |
82
|
|
|
|
83
|
1 |
|
public function onDeactivated(string $body): void { |
84
|
1 |
|
$this->addHook('deactivated', $body); |
85
|
1 |
|
} |
86
|
|
|
|
87
|
1 |
|
public function getName(): string { |
88
|
1 |
|
return $this->name; |
89
|
|
|
} |
90
|
|
|
|
91
|
1 |
|
public function getVarName(): string { |
92
|
1 |
|
return $this->varName; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param string $type |
97
|
|
|
* @param bool $global |
98
|
|
|
*/ |
99
|
1 |
|
public function setTypeAndGlobal(string $type='component',bool $global=true): void { |
100
|
1 |
|
$this->type = $type; |
101
|
1 |
|
$this->global=$global; |
102
|
1 |
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param bool $global |
106
|
|
|
*/ |
107
|
1 |
|
public function setGlobal(bool $global): void { |
108
|
1 |
|
$this->global = $global; |
109
|
1 |
|
} |
110
|
|
|
|
111
|
1 |
|
public function __toString(): string { |
112
|
1 |
|
if($this->global){ |
113
|
1 |
|
return $this->generateGlobalScript(); |
114
|
|
|
} |
115
|
1 |
|
return JavascriptUtils::declareVariable('const', $this->varName, $this->generateObject(), false); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
} |