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

VueManager   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Test Coverage

Coverage 98.41%

Importance

Changes 4
Bugs 1 Features 0
Metric Value
eloc 35
dl 0
loc 92
ccs 62
cts 63
cp 0.9841
rs 10
c 4
b 1
f 0
wmc 22

18 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getInstance() 0 5 2
A deleteInstance() 0 2 1
A addGlobalFilter() 0 2 1
A importExtend() 0 2 1
A addVue() 0 2 1
A addGlobalDirective() 0 8 3
A addGlobalObservable() 0 2 1
A addImport() 0 2 1
A addGlobalExtend() 0 2 1
A importComponentObject() 0 2 1
A addGlobal() 0 2 1
A __toString() 0 6 2
A setAxios() 0 2 1
A importComponent() 0 2 1
A addGlobalComponent() 0 2 1
A addGlobalMixin() 0 2 1
A importMixin() 0 2 1
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
}