Passed
Push — main ( 976a1c...cb0b4a )
by Guillaume
03:00
created

VueJSComponent::generateGlobalScript()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 5
ccs 5
cts 5
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
namespace PHPMV;
3
4
use PHPMV\js\JavascriptUtils;
5
use PHPMV\utils\JsUtils;
6
7
class VueJSComponent extends AbstractVueJS{
8
    protected string $name;
9
    protected array $props;
10
    protected array $template;
11
12 2
    public function __construct(string $template, array $props = []) {
13 2
        parent::__construct();
14 2
        $this->props["props"] = $props;
15 2
        $this->template["template"] = "'".\str_replace(["\n","\r","\t"]," ",(\file_get_contents($template.'.html',true))."'");
16 2
        $this->name = $template;
17 2
    }
18
19 1
    public function generateLocalScript():string {
20 1
        $script = JavascriptUtils::arrayToJsObject($this->props + $this->components + $this->filters +$this->data + $this->computeds + $this->watchers = $this->hooks + $this->methods + $this->template);
21 1
        $script = JsUtils::cleanJSONFunctions($script);
22 1
        return $script;
23
    }
24
25 1
    public function generateGlobalScript():string {
26 1
        $script = "Vue.component('".$this->name."',";
27 1
        $script .= $this->generateLocalScript();
28 1
        $script .= ");\n";
29 1
        return $script;
30
    }
31
32
    public function generateFile($global = false):void {
33
        $script = $this->generateGlobalScript();
34
        if(!$global){
35
            \file_put_contents($this->name.".js",$script);
36
        }
37
        else{
38
            if(file_exists("components.js")){
39
                \file_put_contents("components.js",PHP_EOL . $script,FILE_APPEND);
40
            }
41
            else{
42
                \file_put_contents("components.js",$script);
43
            }
44
        }
45
    }
46
47 1
    public function onActivated(string $body):void {
48 1
        $this->addHook("activated", $body);
49 1
    }
50
51 1
    public function onDeactivated(string $body):void {
52 1
        $this->addHook("deactivated", $body);
53 1
    }
54
55 1
    public function getName():string {
56 1
        return $this->name;
57
    }
58
}