Passed
Push — main ( f61cf1...0f2cdb )
by Guillaume
02:44
created

VueManager::addImport()   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 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 2
ccs 2
cts 2
cp 1
crap 1
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace PHPMV;
4
5
use PHPMV\js\JavascriptUtils;
6
use PHPMV\utils\JsUtils;
7
8
class VueManager {
9
10
	private static ?VueManager $instance = null;
11
	protected array $imports;
12
	protected array $vues;
13
	protected bool $useAxios;
14
15 1
	protected function __construct() {
16 1
		$this->imports = [];
17 1
		$this->vues = [];
18 1
		$this->useAxios = false;
19 1
	}
20
21 1
	public static function getInstance(): ?VueManager {
22 1
		if (!isset(self::$instance)) {
23 1
			self::$instance = new VueManager();
24
		}
25 1
		return self::$instance;
26
	}
27
28 1
	public static function deleteInstance(): void {
29 1
		VueManager::$instance = null;
30 1
	}
31
32 1
	protected function addImport($import): void {
33 1
		$this->imports[] = $import;
34 1
	}
35
36 1
	protected function importComponentObject(VueJSComponent $component): void {
37 1
		$this->addImport(JsUtils::declareVariable('const', $component->getVarName(), $component->generateObject(), false));
38 1
	}
39
40 1
	public function importComponent(VueJSComponent $component): void {
41 1
		$this->importComponentObject($component);
42 1
	}
43
44 1
	public function importMixin(VueJSComponent $mixin): void {
45 1
		$this->importComponentObject($mixin);
46 1
	}
47
48 1
	public function importExtend(VueJSComponent $extend): void {
49 1
		$this->importComponentObject($extend);
50 1
	}
51
52 1
	protected function addGlobal(string $type, string $name, string $body): void {
53 1
		$this->addImport("Vue." . $type . "('" . $name . "'," . $body . ");");
54 1
	}
55
56 1
	public function addGlobalDirective(string $name, array $hookFunction, array $configuration = []) {
57 1
		foreach ($configuration as $key => $value) {
58
			$configuration[$key] = $value;
59
		}
60 1
		foreach ($hookFunction as $key => $value) {
61 1
			$hookFunction[$key] = JsUtils::generateFunction($value, ['el', 'binding', 'vnode', 'oldVnode'], false);
62
		}
63 1
		$this->addGlobal('directive', $name, JavascriptUtils::arrayToJsObject($configuration + $hookFunction));
64 1
	}
65
66 1
	public function addGlobalFilter(string $name, string $body, array $params = []): void {
67 1
		$this->addGlobal('filter', $name, JsUtils::generateFunction($body, $params, false));
68 1
	}
69
70 1
	public function addGlobalExtend(VueJSComponent $extend): void {
71 1
		$this->addGlobal('extend', $extend->getName(), $extend->generateObject());
72 1
	}
73
74 1
	public function addGlobalMixin(VueJSComponent $mixin): void {
75 1
		$this->addGlobal('mixin', $mixin->getName(), $mixin->generateObject());
76 1
	}
77
78 1
	public function addGlobalObservable(string $varName, array $object): void {
79 1
		$this->addImport(JsUtils::declareVariable('const', $varName, "Vue.observable(" . JavascriptUtils::arrayToJsObject($object) . ")", false));
80 1
	}
81
82 1
	public function addGlobalComponent(VueJSComponent $component): void {
83 1
		$this->addImport($component->generateGlobalScript());
84 1
	}
85
86 1
	public function addVue(VueJS $vue): void {
87 1
		$this->vues[] = $vue;
88 1
	}
89
90 1
	public function __toString(): string {
91 1
		$script = "";
92 1
		if ($this->useAxios) $script = "Vue.prototype.\$http = axios;\n";
93 1
		$script .= implode(PHP_EOL, $this->imports);
94 1
		$script .= PHP_EOL . implode(PHP_EOL, $this->vues);
95 1
		return JavascriptUtils::wrapScript($script);
96
	}
97
98 1
	public function setAxios(bool $useAxios): void {
99 1
		$this->useAxios = $useAxios;
100
	}
101
}