|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PHPMV; |
|
4
|
|
|
|
|
5
|
|
|
use PHPMV\js\JavascriptUtils; |
|
6
|
|
|
use PHPMV\utils\JsUtils; |
|
7
|
|
|
|
|
8
|
|
|
class VueJSComponent extends AbstractVueJS { |
|
9
|
|
|
protected string $name; |
|
10
|
|
|
protected array $configuration; |
|
11
|
|
|
protected array $props; |
|
12
|
2 |
|
protected array $template; |
|
13
|
2 |
|
protected array $extends; |
|
14
|
2 |
|
protected ?string $varName; |
|
15
|
2 |
|
|
|
16
|
2 |
|
public function __construct(string $name, string $varName = null) { |
|
17
|
2 |
|
parent::__construct(); |
|
18
|
|
|
$this->name = $name; |
|
19
|
1 |
|
$this->configuration = []; |
|
20
|
1 |
|
$this->props = []; |
|
21
|
1 |
|
$this->extends = []; |
|
22
|
1 |
|
$this->template = []; |
|
23
|
|
|
if (!$varName) { |
|
24
|
|
|
$varName = JsUtils::kebabToPascal($name); |
|
25
|
1 |
|
} |
|
26
|
1 |
|
$this->varName = $varName; |
|
27
|
1 |
|
} |
|
28
|
1 |
|
|
|
29
|
1 |
|
public function extends(VueJSComponent $component): void { |
|
30
|
|
|
$this->extends['extends'] = $component->getVarName(); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function generateObject(): string { |
|
34
|
|
|
$script = JavascriptUtils::arrayToJsObject($this->components + $this->filters + $this->extends + $this->mixins + $this->configuration + $this->props + $this->data + $this->computeds + $this->watchers + $this->hooks + $this->methods + $this->template); |
|
35
|
|
|
$script = JsUtils::cleanJSONFunctions($script); |
|
36
|
|
|
return $script; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function generateGlobalScript(): string { |
|
40
|
|
|
$script = "Vue.component('" . $this->name . "',"; |
|
41
|
|
|
$script .= $this->generateObject(); |
|
42
|
|
|
$script .= ");"; |
|
43
|
|
|
return $script; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function generateFile(bool $inVariable = false, bool $global = false): void { |
|
47
|
1 |
|
$script = ($inVariable) ? JsUtils::declareVariable("const", $this->varName, $this->generateGlobalScript(), false) : $this->generateGlobalScript(); |
|
|
|
|
|
|
48
|
1 |
|
if (!$global) { |
|
49
|
1 |
|
\file_put_contents($this->name . ".js", $script); |
|
50
|
|
|
} elseif (file_exists("components.js")) { |
|
51
|
1 |
|
\file_put_contents("components.js", PHP_EOL . $script, FILE_APPEND); |
|
52
|
1 |
|
} else { |
|
53
|
1 |
|
\file_put_contents("components.js", $script); |
|
54
|
|
|
} |
|
55
|
1 |
|
} |
|
56
|
1 |
|
|
|
57
|
|
|
public function addProps(string ...$props): void { |
|
58
|
1 |
|
$this->props["props"] = $props; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function setInheritAttrs(bool $inheritAttrs): void { |
|
62
|
|
|
$this->configuration['inheritAttrs'] = $inheritAttrs; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function setModel(string $prop, string $event): void { |
|
66
|
|
|
$this->configuration['model'] = "{ prop: '" . $prop . "', event: '" . $event . "' }"; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function addTemplate(string $template): void { |
|
70
|
|
|
$this->template["template"] = $template; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function importTemplate(string $template): void { |
|
74
|
|
|
$this->template["template"] = "'" . \str_replace(["\n", "\r", "\t"], " ", (\file_get_contents($template . '.html', true)) . "'"); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function onActivated(string $body): void { |
|
78
|
|
|
$this->addHook("activated", $body); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function onDeactivated(string $body): void { |
|
82
|
|
|
$this->addHook("deactivated", $body); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function getName(): string { |
|
86
|
|
|
return $this->name; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public function getVarName(): string { |
|
90
|
|
|
return $this->varName; |
|
|
|
|
|
|
91
|
|
|
} |
|
92
|
|
|
} |