Passed
Push — main ( 482e0e...ad07de )
by Guillaume
04:07
created

VueJSComponent::createGlobal()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
ccs 2
cts 2
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=["props"=>[]];
10
    protected array $template;
11
    
12 2
    public function __construct(string $template) {
13 2
        parent::__construct();
14 2
        $this->template["template"]="'".\str_replace(["\n","\r","\t"]," ",(\file_get_contents($template.'.html',true))."'");
15 2
        $this->name=$template;
16 2
    }
17
    
18 2
    public function setProps(string ...$name):void {
19 2
        $this->props["props"]=$name;
20 2
    }
21
22 1
    public function onActivated(string $body):void {
23 1
        $this->addHook("activated", $body);
24 1
    }
25
26 1
    public function onDeactivated(string $body):void {
27 1
        $this->addHook("deactivated", $body);
28 1
    }
29
30 2
    public function create(bool $global=false):string {
31 2
        $script="Vue.component('".$this->name."',";
32 2
        $script.=JavascriptUtils::arrayToJsObject($this->props + $this->data + $this->methods + $this->computeds + $this->watchers + $this->filters + $this->hooks + $this->template);
33 2
        $script=JsUtils::cleanJSONFunctions($script);
34 2
        $script.=")";
35 2
        if(!$global){
36 2
            \file_put_contents($this->name.".js",$script);
37
        }
38
        else{
39 2
            if(file_exists("components.js")){
40 1
                \file_put_contents("components.js",PHP_EOL . $script,FILE_APPEND);
41
            }
42
            else{
43 1
                \file_put_contents("components.js",$script);
44
            }
45
            
46
        }
47 2
        return $script;
48
    }
49
    
50 2
    public function createGlobal():string{
51 2
        return $this->create(true);   
52
    }
53
}