1
|
|
|
<?php |
2
|
|
|
/** @formatter:off |
3
|
|
|
* ****************************************************************** |
4
|
|
|
* Created by Marko Kungla on Jun 27, 2016 - 9:55:09 PM |
5
|
|
|
* Contact [email protected] |
6
|
|
|
* @copyright 2016 Marko Kungla - https://github.com/mkungla |
7
|
|
|
* @license The MIT License (MIT) |
8
|
|
|
* |
9
|
|
|
* @category AframeVR |
10
|
|
|
* @package aframe-php |
11
|
|
|
* |
12
|
|
|
* Lang PHP (php version >= 7) |
13
|
|
|
* Encoding UTF-8 |
14
|
|
|
* File AframeDOMDocument.php |
15
|
|
|
* Code format PSR-2 and 12 |
16
|
|
|
* @link https://github.com/mkungla/aframe-php |
17
|
|
|
^ @issues https://github.com/mkungla/aframe-php/issues |
18
|
|
|
* ******************************************************************** |
19
|
|
|
* Contributors: |
20
|
|
|
* @author Marko Kungla <[email protected]> |
21
|
|
|
* ******************************************************************** |
22
|
|
|
* Comments: |
23
|
|
|
* @formatter:on */ |
24
|
|
|
namespace AframeVR\Core\DOM; |
25
|
|
|
|
26
|
|
|
use \AframeVR\Core\Config; |
27
|
|
|
use \DOMImplementation; |
28
|
|
|
use \DOMDocumentType; |
29
|
|
|
use \DOMDocument; |
30
|
|
|
use \AframeVR\Core\Entity; |
31
|
|
|
use \AframeVR\Interfaces\AssetsInterface; |
32
|
|
|
|
33
|
|
|
final class AframeDOMDocument extends DOMImplementation |
34
|
|
|
{ |
35
|
|
|
use AframeDOMProcessor; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* A-Frame DOM Document type |
39
|
|
|
* |
40
|
|
|
* @var \DOMDocumentType |
41
|
|
|
*/ |
42
|
|
|
protected $doctypeObj; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* A-Frame DOM Document |
46
|
|
|
* |
47
|
|
|
* @var \DOMDocument |
48
|
|
|
*/ |
49
|
|
|
protected $docObj; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Scene meta tile |
53
|
|
|
* |
54
|
|
|
* @var string $scene_title |
55
|
|
|
*/ |
56
|
|
|
protected $scene_title = 'Untitled'; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Scene meta description |
60
|
|
|
* |
61
|
|
|
* @var string $scene_description |
62
|
|
|
*/ |
63
|
|
|
protected $scene_description = ''; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* CDN Of aframe.js |
67
|
|
|
* |
68
|
|
|
* @var string |
69
|
|
|
*/ |
70
|
|
|
protected $aframe_cdn; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Whether to use CDN |
74
|
|
|
* |
75
|
|
|
* @var bool $use_cdn |
76
|
|
|
*/ |
77
|
|
|
protected $use_cdn = false; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* <head> |
81
|
|
|
* |
82
|
|
|
* @var \DOMElement |
83
|
|
|
*/ |
84
|
|
|
protected $head; |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* <body> |
88
|
|
|
* |
89
|
|
|
* @var \DOMElement |
90
|
|
|
*/ |
91
|
|
|
protected $body; |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* <a-scene> |
95
|
|
|
* |
96
|
|
|
* @var \DOMElement |
97
|
|
|
*/ |
98
|
|
|
protected $scene; |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* <a-assets> |
102
|
|
|
* |
103
|
|
|
* @var \DOMElement |
104
|
|
|
*/ |
105
|
|
|
protected $assets; |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Nicely formats output with indentation and extra space. |
109
|
|
|
* |
110
|
|
|
* @var bool |
111
|
|
|
*/ |
112
|
|
|
protected $formatOutput = false; |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* A-Frame DOM |
116
|
|
|
* |
117
|
|
|
* @param Config $config |
118
|
|
|
*/ |
119
|
71 |
|
public function __construct(Config $config) |
120
|
|
|
{ |
121
|
|
|
/* Config */ |
122
|
71 |
|
$this->formatOutput = is_bool($config->get('formatOutput')) ? $config->get('formatOutput') : false; |
123
|
71 |
|
$this->use_cdn = is_bool($config->get('useCDN')) ? $config->get('useCDN') : false; |
|
|
|
|
124
|
|
|
|
125
|
|
|
/* Create HTML5 Document type */ |
126
|
71 |
|
$this->createDocType('html'); |
127
|
|
|
|
128
|
|
|
/* Create A-Frame DOM Document */ |
129
|
71 |
|
$this->createAframeDocument(); |
130
|
|
|
|
131
|
|
|
/* Create boostrap elements */ |
132
|
71 |
|
$this->documentBootstrap(); |
133
|
|
|
|
134
|
|
|
/* Set CDN of aframe.js */ |
135
|
71 |
|
$this->setCDN(is_string($config->get('CDN')) ? $config->get('CDN') : ''); |
136
|
71 |
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Set CDN for aframe.js or min.js |
140
|
|
|
* |
141
|
|
|
* @param string $cdn |
142
|
|
|
* @return void |
143
|
|
|
*/ |
144
|
71 |
|
public function setCDN(string $cdn) |
145
|
|
|
{ |
146
|
71 |
|
$this->aframe_cdn = $cdn; |
147
|
71 |
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Render scene this DOM Object is attached to |
151
|
|
|
* |
152
|
|
|
* @return string |
153
|
|
|
*/ |
154
|
5 |
|
public function render(): string |
155
|
|
|
{ |
156
|
5 |
|
$this->docObj->formatOutput = $this->formatOutput; |
157
|
|
|
|
158
|
5 |
|
$html = $this->docObj->getElementsByTagName('html')->item(0); |
159
|
|
|
/* Make sure we do not add duplicates when render is called multiple times */ |
160
|
5 |
|
if (! $html->hasChildNodes()) { |
161
|
5 |
|
$this->renderHead(); |
162
|
|
|
|
163
|
5 |
|
$html->appendChild($this->head); |
164
|
|
|
|
165
|
5 |
|
$this->renderBody(); |
166
|
|
|
|
167
|
5 |
|
$html->appendChild($this->body); |
168
|
|
|
} |
169
|
5 |
|
return $this->formatOutput ? $this->correctOutputFormat($this->docObj->saveHTML()) : $this->docObj->saveHTML(); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Set Scene meta title |
174
|
|
|
* |
175
|
|
|
* @param string $title |
176
|
|
|
*/ |
177
|
7 |
|
public function setTitle(string $title) |
178
|
|
|
{ |
179
|
7 |
|
$this->scene_title = $title; |
180
|
7 |
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Set Scene meta description |
184
|
|
|
* |
185
|
|
|
* @param string $description |
186
|
|
|
*/ |
187
|
7 |
|
public function setDescription(string $description) |
188
|
|
|
{ |
189
|
7 |
|
$this->scene_description = $description; |
190
|
7 |
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Append entities |
194
|
|
|
* |
195
|
|
|
* @param array $entities |
196
|
|
|
* @return void |
197
|
|
|
*/ |
198
|
7 |
|
public function appendEntities(array $entities) |
199
|
|
|
{ |
200
|
7 |
|
if (! empty($entities)) { |
201
|
2 |
|
foreach ($entities as $entity) { |
202
|
2 |
|
$this->appendEntity($entity); |
203
|
|
|
} |
204
|
|
|
} |
205
|
7 |
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Append assets |
209
|
|
|
* |
210
|
|
|
* @param array $assets |
211
|
|
|
* @return void |
212
|
|
|
*/ |
213
|
2 |
|
public function appendAssets(array $assets) |
214
|
|
|
{ |
215
|
2 |
|
if (! empty($assets)) { |
216
|
2 |
|
if ($this->formatOutput) { |
217
|
2 |
|
$com = $this->docObj->createComment(''); |
218
|
2 |
|
$this->scene->appendChild($com); |
219
|
|
|
} |
220
|
2 |
|
foreach ($assets as $asset) { |
221
|
2 |
|
$this->appendAsset($asset); |
222
|
|
|
} |
223
|
2 |
|
$this->scene->appendChild($this->assets); |
224
|
|
|
} |
225
|
2 |
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Append asset |
229
|
|
|
* |
230
|
|
|
* Create asset DOMElement |
231
|
|
|
* |
232
|
|
|
* @param AssetsInterface $asset |
233
|
|
|
*/ |
234
|
2 |
|
public function appendAsset(AssetsInterface $asset) |
235
|
|
|
{ |
236
|
2 |
|
$this->appendFormatComment('assets', "\n\t"); |
237
|
2 |
|
$this->assets->appendChild($asset->domElement($this->docObj)); |
238
|
2 |
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* Create entity DOMElement |
242
|
|
|
* |
243
|
|
|
* Created entity and append it to scene |
244
|
|
|
* |
245
|
|
|
* @param Entity $entity |
246
|
|
|
* @return void |
247
|
|
|
*/ |
248
|
2 |
|
public function appendEntity(Entity $entity) |
249
|
|
|
{ |
250
|
2 |
|
$this->appendFormatComment('scene', "\n"); |
251
|
2 |
|
$this->scene->appendChild($entity->domElement($this->docObj)); |
252
|
2 |
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* Get HTML of Scene only |
256
|
|
|
* |
257
|
|
|
* @return string |
258
|
|
|
*/ |
259
|
3 |
|
public function renderSceneOnly() |
260
|
|
|
{ |
261
|
3 |
|
$html = new DOMDocument(); |
|
|
|
|
262
|
3 |
|
$html->formatOutput = $this->formatOutput; |
263
|
|
|
|
264
|
3 |
|
$html_scene = $html->importNode($this->scene, true); |
265
|
3 |
|
$html->appendChild($html_scene); |
266
|
3 |
|
return $this->formatOutput ? $this->correctOutputFormat($html->saveHTML()) : $html->saveHTML(); |
267
|
|
|
} |
268
|
|
|
} |
269
|
|
|
|
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.