Test Failed
Push — main ( 624f5f...baf8be )
by Jean-Christophe
04:08
created

VueManager::__toString()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 7
Bugs 1 Features 2
Metric Value
cc 4
eloc 9
c 7
b 1
f 2
nc 8
nop 0
dl 0
loc 15
ccs 9
cts 9
cp 1
crap 4
rs 9.9666
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['imports'][] = $import;
47 1
	}
48
49
	public function importComponentObject(VueJSComponent $component): void { //component, mixin, or extend
50 1
		$this->addImport($component);
51
	}
52 1
53
	public function addGlobalDirective(string $name, array $hookFunction) {
54 1
		foreach ($hookFunction as $key => $value) {
55 1
			$hookFunction[$key] = JavascriptUtils::generateFunction($value, ['el', 'binding', 'vnode', 'oldVnode']);
56 1
		}
57
		$this->addImport("Vue.directive('$name',".JavascriptUtils::arrayToJsObject($hookFunction).");");
58 1
	}
59 1
60 1
	public function addGlobalFilter(string $name, string $body, array $params = []): void {
61
		$this->addImport("Vue.filter('$name',". JavascriptUtils::generateFunction($body, $params, false) .");");
62 1
	}
63 1
64
	public function addGlobalObservable(string $varName, array $object): void {
65 1
		$this->addImport(JavascriptUtils::declareVariable('const', $varName, "Vue.observable(" . JavascriptUtils::arrayToJsObject($object) . ")", false));
66 1
	}
67 1
68
	public function addGlobalMixin(VueJSComponent $mixin): void {
69 1
		$mixin->setTypeAndGlobal('mixin');
70 1
		$this->addImport($mixin);
71 1
	}
72
73 1
	public function addGlobalExtend(VueJSComponent $extend): void {
74 1
		$extend->setTypeAndGlobal('extend');
75 1
		$this->addImport($extend);
76
	}
77 1
78 1
	public function addGlobalComponent(VueJSComponent $component): void {
79 1
		$component->setGlobal(true);
80
		$this->addImport($component);
81 1
	}
82 1
83 1
	public function addVue(VueJS $vue): void {
84
		$this->vues[] = $vue;
85 1
	}
86 1
87 1
	/**
88
	 * Creates and returns a new VueJS instance.
89
	 *
90
	 * @param string $element
91
	 * @param string|null $varName
92
	 * @param false $useVuetify
93
	 * @return VueJS
94
	 */
95
	public function createVue(string $element,?string $varName=null,bool $useVuetify=false): VueJS {
96
		$config=$this->config;
97
		$config['el']=$element;
98
		$varName??='app'.(\count($this->vues)+1);
99
		return $this->vues[]=new VueJS($config,$varName,$useVuetify);
100
	}
101
102
	public function __toString(): string {
103
		$script = '';
104 1
		if ($this->useAxios){
105 1
			$script = 'Vue.prototype.$http = axios;' . \PHP_EOL;
106 1
		}
107 1
108
		foreach($this->imports as $import) {
109 1
			$script.=$import.\PHP_EOL;
110 1
		}
111
		foreach($this->vues as $vue) {
112
			$script.=$vue.\PHP_EOL;
113 1
		}
114 1
115
		$script = JavascriptUtils::cleanJSONFunctions($script);
116 1
		return JavascriptUtils::wrapScript($script);
117 1
	}
118
119
	public function setAxios(bool $useAxios): void {
120
		$this->useAxios = $useAxios;
121 1
	}
122 1
123 1
	/**
124 1
	 * Sets the global VueJS configuration array with el, delimiters, useAxios.
125 1
	 *
126 1
	 * @param array $config
127
	 */
128
	public function setConfig(array $config): void {
129 1
		if(isset($config['useAxios'])) {
130 1
			$this->setAxios($config['useAxios']);
131 1
			unset($config['useAxios']);
132
		}
133
		$this->config=$config;
134
	}
135
}