Passed
Push — main ( 7b0cda...aa7d91 )
by Jean-Christophe
02:46
created

VueManager   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Test Coverage

Coverage 83.82%

Importance

Changes 6
Bugs 1 Features 2
Metric Value
eloc 46
dl 0
loc 114
ccs 57
cts 68
cp 0.8382
rs 10
c 6
b 1
f 2
wmc 23

17 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 createVue() 0 5 1
A __toString() 0 7 2
A setAxios() 0 2 1
A setConfig() 0 6 2
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
	protected static ?VueManager $instance = null;
19
	protected ?object $container;
20
	protected array $imports;
21
	protected array $vues;
22
	protected bool $useAxios;
23
	protected array $config;
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
	/**
93
	 * Creates and returns a new VueJS instance.
94
	 *
95
	 * @param string $element
96
	 * @param string|null $varName
97
	 * @param false $useVuetify
98
	 * @return VueJS
99
	 */
100
	public function createVue(string $element,?string $varName=null,$useVuetify=false): VueJS {
101
		$config=$this->config;
102
		$config['el']=$element;
103
		$varName??='app'.(\count($this->vues)+1);
104
		return $this->vues[]=new VueJS($config,$varName,$useVuetify);
105
	}
106
107 1
	public function __toString(): string {
108 1
		$script = '';
109 1
		if ($this->useAxios) $script = 'Vue.prototype.$http = axios;' . PHP_EOL;
110 1
		$script .= \implode(PHP_EOL, $this->imports);
111 1
		$script .= PHP_EOL . \implode(PHP_EOL, $this->vues);
112 1
		$script = JavascriptUtils::cleanJSONFunctions($script);
113 1
		return JavascriptUtils::wrapScript($script);
114
	}
115
116 1
	public function setAxios(bool $useAxios): void {
117 1
		$this->useAxios = $useAxios;
118 1
	}
119
120
	/**
121
	 * Sets the global VueJS configuration array with el, delimiters, useAxios.
122
	 *
123
	 * @param array $config
124
	 */
125
	public function setConfig(array $config): void {
126
		if(isset($config['useAxios'])) {
127
			$this->setAxios($config['useAxios']);
128
			unset($config['useAxios']);
129
		}
130
		$this->config=$config;
131
	}
132
}