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 $configuration; |
10
|
|
|
protected array $props; |
11
|
|
|
protected array $template; |
12
|
|
|
protected array $extends; |
13
|
|
|
protected string $varName; |
14
|
|
|
|
15
|
|
|
public function __construct(string $name, string $varName = null) { |
16
|
1 |
|
parent::__construct(); |
17
|
1 |
|
$this->name = $name; |
18
|
1 |
|
$this->configuration = []; |
19
|
1 |
|
$this->props = []; |
20
|
1 |
|
$this->extends = []; |
21
|
1 |
|
$this->template = []; |
22
|
1 |
|
if (!$varName) { |
23
|
1 |
|
$varName = JavascriptUtils::kebabToPascal($name); |
|
|
|
|
24
|
1 |
|
} |
25
|
|
|
$this->varName = $varName; |
26
|
1 |
|
} |
27
|
1 |
|
|
28
|
|
|
public function addData(string $name, $value): void { |
29
|
1 |
|
$this->data["data"][$name] = $value; |
30
|
1 |
|
} |
31
|
1 |
|
|
32
|
|
|
public function addDataRaw(string $name, string $value): void { |
33
|
1 |
|
$this->data["data"][$name] = JavascriptUtils::removeQuotes($value); |
|
|
|
|
34
|
1 |
|
} |
35
|
1 |
|
|
36
|
1 |
|
public function extends(VueJSComponent $component): void { |
37
|
|
|
$this->extends['extends'] = $component->getVarName(); |
38
|
|
|
} |
39
|
1 |
|
|
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
|
|
|
$script = 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
|
1 |
|
return $script; |
47
|
1 |
|
} |
48
|
1 |
|
|
49
|
1 |
|
public function generateGlobalScript(): string { |
50
|
|
|
$script = "Vue.component('" . $this->name . "',"; |
51
|
|
|
$script .= $this->generateObject(); |
52
|
|
|
$script .= ");"; |
53
|
|
|
return $script; |
54
|
|
|
} |
55
|
1 |
|
|
56
|
|
|
public function generateFile(bool $inVariable = false, bool $global = false): void { |
57
|
1 |
|
$script = ($inVariable) ? JavascriptUtils::declareVariable("const", $this->varName, $this->generateGlobalScript(), false) : $this->generateGlobalScript(); |
|
|
|
|
58
|
1 |
|
if (!$global) { |
59
|
1 |
|
\file_put_contents($this->name . ".js", $script); |
60
|
|
|
} elseif (file_exists("components.js")) { |
61
|
1 |
|
\file_put_contents("components.js", PHP_EOL . $script, FILE_APPEND); |
62
|
1 |
|
} else { |
63
|
1 |
|
\file_put_contents("components.js", $script); |
64
|
|
|
} |
65
|
1 |
|
} |
66
|
1 |
|
|
67
|
1 |
|
public function addProps(string ...$props): void { |
68
|
|
|
$this->props["props"] = $props; |
69
|
1 |
|
} |
70
|
1 |
|
|
71
|
1 |
|
public function setInheritAttrs(bool $inheritAttrs): void { |
72
|
|
|
$this->configuration['inheritAttrs'] = $inheritAttrs; |
73
|
1 |
|
} |
74
|
1 |
|
|
75
|
1 |
|
public function setModel(string $prop, string $event): void { |
76
|
|
|
$this->configuration['model'] = JavascriptUtils::removeQuotes("{ prop: '" . $prop . "', event: '" . $event . "' }"); |
77
|
1 |
|
} |
78
|
1 |
|
|
79
|
1 |
|
public function addTemplate(string $template): void { |
80
|
|
|
$this->template["template"] = $template; |
81
|
1 |
|
} |
82
|
1 |
|
|
83
|
|
|
public function onActivated(string $body): void { |
84
|
|
|
$this->addHook("activated", $body); |
85
|
1 |
|
} |
86
|
1 |
|
|
87
|
|
|
public function onDeactivated(string $body): void { |
88
|
1 |
|
$this->addHook("deactivated", $body); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function getName(): string { |
92
|
|
|
return $this->name; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function getVarName(): string { |
96
|
|
|
return $this->varName; |
97
|
|
|
} |
98
|
|
|
} |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.