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