Passed
Push — main ( 7524e7...624f5f )
by Guillaume
02:43
created

VueManager   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 127
Duplicated Lines 0 %

Test Coverage

Coverage 85.14%

Importance

Changes 10
Bugs 2 Features 2
Metric Value
eloc 55
c 10
b 2
f 2
dl 0
loc 127
ccs 63
cts 74
cp 0.8514
rs 10
wmc 27

16 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getInstance() 0 8 3
A deleteInstance() 0 2 1
A addGlobalExtend() 0 2 1
A addImport() 0 6 2
A addGlobalFilter() 0 2 1
A addVue() 0 2 1
A addGlobalDirective() 0 5 2
A addGlobalObservable() 0 2 1
A createVue() 0 5 1
A importComponentObject() 0 2 1
B __toString() 0 23 7
A setAxios() 0 2 1
A setConfig() 0 6 2
A addGlobalComponent() 0 2 1
A addGlobalMixin() 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
	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,bool $component = false, string $type = null): void {
46 1
		if($component){
47 1
			$this->imports['components'][] = ['type' => $type, 'object' => $import];
48
		}
49
		else{
50 1
			$this->imports['imports'][] = $import;
51
		}
52 1
	}
53
54 1
	public function importComponentObject(VueJSComponent $component): void { //component, mixin, or extend
55 1
		$this->addImport($component, true, 'local');
56 1
	}
57
58 1
	public function addGlobalDirective(string $name, array $hookFunction) {
59 1
		foreach ($hookFunction as $key => $value) {
60 1
			$hookFunction[$key] = JavascriptUtils::generateFunction($value, ['el', 'binding', 'vnode', 'oldVnode']);
61
		}
62 1
		$this->addImport("Vue.directive('$name',".JavascriptUtils::arrayToJsObject($hookFunction).");");
63 1
	}
64
65 1
	public function addGlobalFilter(string $name, string $body, array $params = []): void {
66 1
		$this->addImport("Vue.filter('$name',". JavascriptUtils::generateFunction($body, $params, false) .");");
67 1
	}
68
69 1
	public function addGlobalObservable(string $varName, array $object): void {
70 1
		$this->addImport(JavascriptUtils::declareVariable('const', $varName, "Vue.observable(" . JavascriptUtils::arrayToJsObject($object) . ")", false));
71 1
	}
72
73 1
	public function addGlobalMixin(VueJSComponent $mixin): void {
74 1
		$this->addImport($mixin, true, 'mixin');
75 1
	}
76
77 1
	public function addGlobalExtend(VueJSComponent $extend): void {
78 1
		$this->addImport($extend, true, 'extend');
79 1
	}
80
81 1
	public function addGlobalComponent(VueJSComponent $component): void {
82 1
		$this->addImport($component, true, 'global');
83 1
	}
84
85 1
	public function addVue(VueJS $vue): void {
86 1
		$this->vues[] = $vue;
87 1
	}
88
89
	/**
90
	 * Creates and returns a new VueJS instance.
91
	 *
92
	 * @param string $element
93
	 * @param string|null $varName
94
	 * @param false $useVuetify
95
	 * @return VueJS
96
	 */
97
	public function createVue(string $element,?string $varName=null,bool $useVuetify=false): VueJS {
98
		$config=$this->config;
99
		$config['el']=$element;
100
		$varName??='app'.(\count($this->vues)+1);
101
		return $this->vues[]=new VueJS($config,$varName,$useVuetify);
102
	}
103
104 1
	public function __toString(): string {
105 1
		foreach($this->imports['components'] as $component) {
106 1
			if ($component['type'] == 'global') {
107 1
				$this->addImport($component['object']->generateGlobalScript());
108
			}
109 1
			else if($component['type'] == 'local'){
110 1
				$this->addImport(JavascriptUtils::declareVariable('const', $component['object']->getVarName(), $component['object']->generateObject(), false));
111
			}
112
			else {
113 1
				if ($component['type'] == 'extend') {
114 1
					$this->addImport("Vue.extend(". $component['object']->generateObject() .");");
115
				}
116 1
				if ($component['type'] == 'mixin') {
117 1
					$this->addImport("Vue.mixin(". $component['object']->generateObject() .");");
118
				}
119
			}
120
		}
121 1
		$script = '';
122 1
		if ($this->useAxios) $script = 'Vue.prototype.$http = axios;' . PHP_EOL;
123 1
		$script .= \implode(PHP_EOL, $this->imports['imports']);
124 1
		$script .= PHP_EOL . \implode(PHP_EOL, $this->vues);
125 1
		$script = JavascriptUtils::cleanJSONFunctions($script);
126 1
		return JavascriptUtils::wrapScript($script);
127
	}
128
129 1
	public function setAxios(bool $useAxios): void {
130 1
		$this->useAxios = $useAxios;
131 1
	}
132
133
	/**
134
	 * Sets the global VueJS configuration array with el, delimiters, useAxios.
135
	 *
136
	 * @param array $config
137
	 */
138
	public function setConfig(array $config): void {
139
		if(isset($config['useAxios'])) {
140
			$this->setAxios($config['useAxios']);
141
			unset($config['useAxios']);
142
		}
143
		$this->config=$config;
144
	}
145
}