Passed
Push — main ( 293413...7524e7 )
by Guillaume
03:58
created

VueManager::__toString()   B

Complexity

Conditions 7
Paths 12

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 7

Importance

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