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

VueManager::getInstance()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.0416

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 3
eloc 5
c 2
b 0
f 1
nc 4
nop 1
dl 0
loc 8
ccs 5
cts 6
cp 0.8333
crap 3.0416
rs 10
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
}