Passed
Push — main ( 44af98...a27059 )
by Jean-Christophe
13:07
created

VueManager   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Test Coverage

Coverage 98.25%

Importance

Changes 5
Bugs 1 Features 1
Metric Value
eloc 37
c 5
b 1
f 1
dl 0
loc 86
ccs 56
cts 57
cp 0.9825
rs 10
wmc 20

15 Methods

Rating   Name   Duplication   Size   Complexity  
A addGlobalFilter() 0 2 1
A addVue() 0 2 1
A addGlobalDirective() 0 5 2
A addGlobalExtend() 0 2 1
A addGlobalComponent() 0 2 1
A addGlobalMixin() 0 2 1
A addGlobalObservable() 0 2 1
A __construct() 0 4 1
A getInstance() 0 8 3
A addImport() 0 2 1
A deleteInstance() 0 2 1
A importComponentObject() 0 2 1
A addGlobal() 0 5 2
A __toString() 0 7 2
A setAxios() 0 2 1
1
<?php
2
3
namespace PHPMV;
4
5
use PHPMV\js\JavascriptUtils;
6
7
/**
8
 * php-vueJS Manager class.
9
 *
10
 * PHPMV$VueManager
11
 * This class is part of php-vuejs
12
 *
13
 * @author jguillaumesio
14
 * @version 1.0.0
15
 *
16
 */
17
class VueManager {
18
19
	protected static ?VueManager $instance = null;
20
	protected ?object $container;
21
	protected array $imports;
22
	protected array $vues;
23
	protected bool $useAxios;
24
25 2
	protected function __construct() {
26 2
		$this->imports = [];
27 2
		$this->vues = [];
28 2
		$this->useAxios = false;
29 2
	}
30
31 2
	public static function getInstance(?object $container=null): ?VueManager {
32 2
		if (!isset(self::$instance)) {
33 2
			self::$instance = new static();
34
		}
35 2
		if(isset($container)){
36
			self::$instance->container=$container;
37
		}
38 2
		return self::$instance;
39
	}
40
41 2
	public static function deleteInstance(): void {
42 2
		VueManager::$instance = null;
43 2
	}
44
45 1
	protected function addImport($import): void {
46 1
		$this->imports[] = $import;
47 1
	}
48
49 1
	protected function addGlobal(string $type, string $body, string $name = null): void {
50 1
		if ($name) {
51 1
			$this->addImport("Vue.$type('$name',$body);");
52
		} else {
53 1
			$this->addImport("Vue.$type($body);");
54
		}
55 1
	}
56
57 1
	public function importComponentObject(VueJSComponent $component): void { //component, mixin, or extend
58 1
		$this->addImport(JavascriptUtils::declareVariable('const', $component->getVarName(), $component->generateObject(), false));
59 1
	}
60
61 1
	public function addGlobalDirective(string $name, array $hookFunction) {
62 1
		foreach ($hookFunction as $key => $value) {
63 1
			$hookFunction[$key] = JavascriptUtils::generateFunction($value, ['el', 'binding', 'vnode', 'oldVnode']);
64
		}
65 1
		$this->addGlobal('directive', JavascriptUtils::arrayToJsObject($hookFunction), $name);
66 1
	}
67
68 1
	public function addGlobalFilter(string $name, string $body, array $params = []): void {
69 1
		$this->addGlobal('filter', JavascriptUtils::generateFunction($body, $params, false), $name);
70 1
	}
71
72 1
	public function addGlobalExtend(VueJSComponent $extend): void {
73 1
		$this->addGlobal('extend', $extend->generateObject());
74 1
	}
75
76 1
	public function addGlobalMixin(VueJSComponent $mixin): void {
77 1
		$this->addGlobal('mixin', $mixin->generateObject());
78 1
	}
79
80 1
	public function addGlobalObservable(string $varName, array $object): void {
81 1
		$this->addImport(JavascriptUtils::declareVariable('const', $varName, "Vue.observable(" . JavascriptUtils::arrayToJsObject($object) . ")", false));
82 1
	}
83
84 1
	public function addGlobalComponent(VueJSComponent $component): void {
85 1
		$this->addImport($component->generateGlobalScript());
86 1
	}
87
88 1
	public function addVue(VueJS $vue): void {
89 1
		$this->vues[] = $vue;
90 1
	}
91
92 1
	public function __toString(): string {
93 1
		$script = '';
94 1
		if ($this->useAxios) $script = 'Vue.prototype.$http = axios;' . PHP_EOL;
95 1
		$script .= \implode(PHP_EOL, $this->imports);
96 1
		$script .= PHP_EOL . \implode(PHP_EOL, $this->vues);
97 1
		$script = JavascriptUtils::cleanJSONFunctions($script);
98 1
		return JavascriptUtils::wrapScript($script);
99
	}
100
101 1
	public function setAxios(bool $useAxios): void {
102 1
		$this->useAxios = $useAxios;
103
	}
104
}