Test Failed
Push — main ( c284eb...6e070f )
by Jean-Christophe
05:10
created

VueManager::createComponent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 3
dl 0
loc 4
rs 10
ccs 2
cts 2
cp 1
crap 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
	protected function getTemplateComponentDirectory(): string {
32 2
		return $this->config['templateDir'] ?? 'vuejs/';
33 2
	}
34
35 2
	public static function getInstance(?object $container = null): ?VueManager {
36
		if (!isset(self::$instance)) {
37
			self::$instance = new static();
38 2
		}
39
		if (isset($container)) {
40
			self::$instance->container = $container;
41 2
		}
42 2
		return self::$instance;
43 2
	}
44
45 1
	public static function deleteInstance(): void {
46 1
		VueManager::$instance = null;
47 1
	}
48
49 1
	public function importComponentObject(VueJSComponent $component): void { //component, mixin, or extend
50 1
		$this->imports[] = $component;
51 1
	}
52
53 1
	public function addGlobalDirective(string $name, array $hookFunction) {
54 1
		foreach ($hookFunction as $key => $value) {
55
			$hookFunction[$key] = JavascriptUtils::generateFunction($value, ['el', 'binding', 'vnode', 'oldVnode']);
56 1
		}
57 1
		$this->imports[] = "Vue.directive('$name'," . JavascriptUtils::arrayToJsObject($hookFunction) . ");";
58 1
	}
59
60 1
	public function addGlobalFilter(string $name, string $body, array $params = []): void {
61 1
		$this->imports[] = "Vue.filter('$name'," . JavascriptUtils::generateFunction($body, $params, false) . ");";
62 1
	}
63
64 1
	public function addGlobalObservable(string $varName, array $object): void {
65 1
		$this->imports[] = 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->imports[] = $mixin;
71 1
	}
72 1
73
	public function addGlobalExtend(VueJSComponent $extend): void {
74 1
		$extend->setTypeAndGlobal('extend');
75 1
		$this->imports[] = $extend;
76 1
	}
77 1
78
	public function addGlobalComponent(VueJSComponent $component): void {
79 1
		$component->setGlobal(true);
80 1
		$this->imports[] = $component;
81 1
	}
82
83
	public function addVue(VueJS $vue): void {
84
		$this->vues[] = $vue;
85
	}
86
87
	/**
88
	 * Creates, adds and returns a new VueJS instance.
89
	 *
90
	 * @param string $element The dom selector associated with this Vue
91
	 * @param string|null $varName The vue variable name
92
	 * @param false $useVuetify True if Vuutify is used
93
	 * @return VueJS The created vue
94
	 */
95
	public function createVue(string $element, ?string $varName = null, bool $useVuetify = false): VueJS {
96
		$config = $this->config;
97
		$config['el'] = $element;
98 1
		$varName ??= 'app' . (\count($this->vues) + 1);
99 1
		return $this->vues[] = new VueJS($config, $varName, $useVuetify);
100 1
	}
101 1
102
	/**
103
	 * Creates, adds and returns a new VueJSComponent instance (component, mixin or extend).
104 1
	 * @param string $name The component name
105 1
	 * @param string|null $varName The component varName
106
	 * @param string $type The object type : one of component (default value), extend or mixin
107 1
	 * @return VueJSComponent The created component
108 1
	 */
109
	public function createComponent(string $name, string $varName = null, string $type='component'): VueJSComponent {
110
		$compo=new VueJSComponent($name,$varName,$type);
111 1
		$this->addGlobalComponent($compo);
112 1
		return $compo;
113
	}
114
115 1
	public function __toString(): string {
116 1
		$script = '';
117 1
		if ($this->useAxios) {
118
			$script = 'Vue.prototype.$http = axios;' . \PHP_EOL;
119
		}
120
121
		if(isset($this->config['delimiters'])){
122
			$script.='Vue.options.delimiters = '.\json_encode($this->config['delimiters']).';'.\PHP_EOL;
123
		}
124
125
		$script .= \implode(\PHP_EOL, $this->imports);
126
		$script .= \PHP_EOL.\implode(\PHP_EOL, $this->vues);
127
128
		$script = JavascriptUtils::cleanJSONFunctions($script);
129
		return JavascriptUtils::wrapScript($script);
130
	}
131 1
132
	public function setAxios(bool $useAxios): void {
133
		$this->useAxios = $useAxios;
134
	}
135
136
	/**
137
	 * Sets the global VueJS configuration array with el, delimiters, useAxios, templateDir.
138
	 *
139
	 * @param array $config
140
	 */
141
	public function setConfig(array $config): void {
142
		if (isset($config['useAxios'])) {
143
			$this->setAxios($config['useAxios']);
144
			unset($config['useAxios']);
145
		}
146
		$this->config = $config;
147
	}
148
}