|
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
|
|
|
|
|
32
|
|
|
final class AframeDOMDocument extends DOMImplementation |
|
33
|
|
|
{ |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* A-Frame DOM Document type |
|
37
|
|
|
* |
|
38
|
|
|
* @var \DOMDocumentType |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $doctypeObj; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* A-Frame DOM Document |
|
44
|
|
|
* |
|
45
|
|
|
* @var \DOMDocument |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $docObj; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Scene meta tile |
|
51
|
|
|
* |
|
52
|
|
|
* @var string $scene_title |
|
53
|
|
|
*/ |
|
54
|
|
|
protected $scene_title = 'Untitled'; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Scene meta description |
|
58
|
|
|
* |
|
59
|
|
|
* @var string $scene_description |
|
60
|
|
|
*/ |
|
61
|
|
|
protected $scene_description = ''; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* CDN Of aframe.js |
|
65
|
|
|
* |
|
66
|
|
|
* @var string |
|
67
|
|
|
*/ |
|
68
|
|
|
protected $aframe_cdn; |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Whether to use CDN |
|
72
|
|
|
* |
|
73
|
|
|
* @var bool $use_cdn |
|
74
|
|
|
*/ |
|
75
|
|
|
protected $use_cdn = false; |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* <head> |
|
79
|
|
|
* |
|
80
|
|
|
* @var \DOMElement $head |
|
81
|
|
|
*/ |
|
82
|
|
|
protected $head; |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* <body> |
|
86
|
|
|
* |
|
87
|
|
|
* @var \DOMElement $body |
|
88
|
|
|
*/ |
|
89
|
|
|
protected $body; |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* <a-scene> |
|
93
|
|
|
* |
|
94
|
|
|
* @var \DOMElement $scene |
|
95
|
|
|
*/ |
|
96
|
|
|
protected $scene; |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Nicely formats output with indentation and extra space. |
|
100
|
|
|
* |
|
101
|
|
|
* @var bool |
|
102
|
|
|
*/ |
|
103
|
|
|
public $formatOutput = true; |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* A-Frame DOM |
|
107
|
|
|
* |
|
108
|
|
|
* @param Config $config |
|
109
|
|
|
* @return void |
|
|
|
|
|
|
110
|
|
|
*/ |
|
111
|
59 |
|
public function __construct(Config $config) |
|
112
|
|
|
{ |
|
113
|
|
|
/* Create HTML5 Document type */ |
|
114
|
59 |
|
$this->createDocType('html'); |
|
115
|
|
|
/* Create A-Frame DOM Document */ |
|
116
|
59 |
|
$this->createAframeDocument(); |
|
117
|
|
|
/* Create <head> element */ |
|
118
|
59 |
|
$this->createHead(); |
|
119
|
|
|
/* Create <body> element */ |
|
120
|
59 |
|
$this->createBody(); |
|
121
|
|
|
/* Create <a-scene> element */ |
|
122
|
59 |
|
$this->createScene(); |
|
123
|
|
|
/* Set CDN of aframe.js */ |
|
124
|
59 |
|
$this->setCDN($config->get('CDN')); |
|
125
|
59 |
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Load aframe.in.js from CDN |
|
129
|
|
|
* |
|
130
|
|
|
* @return void |
|
131
|
|
|
*/ |
|
132
|
2 |
|
public function useCDN() |
|
133
|
|
|
{ |
|
134
|
2 |
|
$this->use_cdn = true; |
|
135
|
2 |
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Set CDN for aframe.js or min.js |
|
139
|
|
|
* |
|
140
|
|
|
* @param string $cdn |
|
141
|
|
|
* @return void |
|
142
|
|
|
*/ |
|
143
|
59 |
|
public function setCDN(string $cdn) |
|
144
|
|
|
{ |
|
145
|
59 |
|
$this->aframe_cdn = $cdn; |
|
146
|
59 |
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Render scene this DOM Object is attached to |
|
150
|
|
|
* |
|
151
|
|
|
* @param bool $only_scene |
|
|
|
|
|
|
152
|
|
|
* @return string |
|
153
|
|
|
*/ |
|
154
|
3 |
|
public function render(): string |
|
155
|
|
|
{ |
|
156
|
3 |
|
$this->docObj->formatOutput = $this->formatOutput; |
|
157
|
3 |
|
$html = $this->docObj->getElementsByTagName('html')[0]; |
|
158
|
|
|
|
|
159
|
3 |
|
$this->renderHead(); |
|
160
|
3 |
|
$html->appendChild($this->head); |
|
161
|
|
|
|
|
162
|
3 |
|
$this->renderBody(); |
|
163
|
3 |
|
$html->appendChild($this->body); |
|
164
|
3 |
|
return $this->docObj->saveHTML(); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* Set Scene meta title |
|
169
|
|
|
* |
|
170
|
|
|
* @param string $title |
|
171
|
|
|
*/ |
|
172
|
2 |
|
public function setTitle(string $title) |
|
173
|
|
|
{ |
|
174
|
2 |
|
$this->scene_title = $title; |
|
175
|
2 |
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* Set Scene meta description |
|
179
|
|
|
* |
|
180
|
|
|
* @param string $title |
|
|
|
|
|
|
181
|
|
|
*/ |
|
182
|
2 |
|
public function setDescription(string $description) |
|
183
|
|
|
{ |
|
184
|
2 |
|
$this->scene_description = $description; |
|
185
|
2 |
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* Append entities |
|
189
|
|
|
* |
|
190
|
|
|
* @param array $entities |
|
191
|
|
|
* @return void |
|
192
|
|
|
*/ |
|
193
|
3 |
|
public function appendEntities(array $entities) |
|
194
|
|
|
{ |
|
195
|
3 |
|
if (! empty($entities)) { |
|
196
|
1 |
|
foreach ($entities as $entity) { |
|
197
|
1 |
|
$this->appendEntity($entity); |
|
198
|
|
|
} |
|
199
|
|
|
} |
|
200
|
3 |
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* Add entity |
|
204
|
|
|
* |
|
205
|
|
|
* @param Entity $entity |
|
206
|
|
|
* @return void |
|
207
|
|
|
*/ |
|
208
|
1 |
|
public function appendEntity(Entity $entity) |
|
209
|
|
|
{ |
|
210
|
1 |
|
$this->scene->appendChild($entity->DOMElement($this->docObj)); |
|
211
|
1 |
|
} |
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* Get HTML of Scene only |
|
215
|
|
|
* |
|
216
|
|
|
* @return string |
|
217
|
|
|
*/ |
|
218
|
1 |
|
public function renderSceneOnly() |
|
219
|
|
|
{ |
|
220
|
1 |
|
$html = new DOMDocument(); |
|
221
|
1 |
|
$html_scene = $html->importNode($this->scene, true); |
|
222
|
1 |
|
$html->appendChild($html_scene); |
|
223
|
1 |
|
return $html->saveHTML(); |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
/** |
|
227
|
|
|
* Prepeare head |
|
228
|
|
|
* |
|
229
|
|
|
* @return void |
|
230
|
|
|
*/ |
|
231
|
3 |
|
protected function renderHead() |
|
232
|
|
|
{ |
|
233
|
3 |
|
$this->appendTitle(); |
|
234
|
3 |
|
$this->appendMetaTag(array( |
|
235
|
3 |
|
'name' => 'description', |
|
236
|
3 |
|
'content' => $this->scene_description |
|
237
|
|
|
)); |
|
238
|
3 |
|
$this->appendMetaTag(array( |
|
239
|
|
|
'charset' => 'utf-8' |
|
240
|
3 |
|
)); |
|
241
|
3 |
|
$this->appendMetaTag(array( |
|
242
|
3 |
|
'name' => 'viewport', |
|
243
|
|
|
'content' => 'width=device-width,initial-scale=1,maximum-scale=1,shrink-to-fit=no,user-scalable=no,minimal-ui' |
|
244
|
|
|
)); |
|
245
|
3 |
|
$this->appendMetaTag(array( |
|
246
|
3 |
|
'name' => 'mobile-web-app-capable', |
|
247
|
|
|
'content' => 'yes' |
|
248
|
|
|
)); |
|
249
|
3 |
|
$this->appendMetaTag(array( |
|
250
|
3 |
|
'name' => 'theme-color', |
|
251
|
|
|
'content' => 'black' |
|
252
|
|
|
)); |
|
253
|
|
|
|
|
254
|
3 |
|
$this->appendCDN(); |
|
255
|
3 |
|
} |
|
256
|
|
|
|
|
257
|
|
|
/** |
|
258
|
|
|
* If requested by user use aframe CDN |
|
259
|
|
|
* |
|
260
|
|
|
* @return void |
|
261
|
|
|
*/ |
|
262
|
3 |
|
protected function appendCDN() |
|
263
|
|
|
{ |
|
264
|
3 |
|
if ($this->use_cdn) { |
|
265
|
2 |
|
$cdn_script = $this->docObj->createElement('script'); |
|
266
|
2 |
|
$cdn_script->setAttribute('src', $this->aframe_cdn); |
|
267
|
2 |
|
$this->head->appendChild($cdn_script); |
|
268
|
|
|
} |
|
269
|
3 |
|
} |
|
270
|
|
|
|
|
271
|
|
|
/** |
|
272
|
|
|
* Prepare body |
|
273
|
|
|
* |
|
274
|
|
|
* @return void |
|
275
|
|
|
*/ |
|
276
|
3 |
|
protected function renderBody() |
|
277
|
|
|
{ |
|
278
|
3 |
|
$this->body->appendChild($this->scene); |
|
279
|
3 |
|
} |
|
280
|
|
|
|
|
281
|
|
|
/** |
|
282
|
|
|
* Create meta tags |
|
283
|
|
|
* |
|
284
|
|
|
* @param array $attr |
|
285
|
|
|
*/ |
|
286
|
3 |
|
protected function appendMetaTag(array $attr) |
|
287
|
|
|
{ |
|
288
|
3 |
|
$metatag = $this->docObj->createElement('meta'); |
|
289
|
3 |
|
foreach ($attr as $key => $val) |
|
290
|
3 |
|
$metatag->setAttribute($key, $val); |
|
291
|
3 |
|
$this->head->appendChild($metatag); |
|
292
|
3 |
|
} |
|
293
|
|
|
|
|
294
|
|
|
/** |
|
295
|
|
|
* Create title tag |
|
296
|
|
|
* |
|
297
|
|
|
* @return void |
|
298
|
|
|
*/ |
|
299
|
3 |
|
protected function appendTitle() |
|
300
|
|
|
{ |
|
301
|
3 |
|
$title = $this->docObj->createElement('title', $this->scene_title); |
|
302
|
3 |
|
$this->head->appendChild($title); |
|
303
|
3 |
|
} |
|
304
|
|
|
|
|
305
|
|
|
/** |
|
306
|
|
|
* Creates an empty DOMDocumentType object |
|
307
|
|
|
* |
|
308
|
|
|
* @param string $doctype |
|
309
|
|
|
* @return void |
|
310
|
|
|
*/ |
|
311
|
59 |
|
protected function createDocType(string $doctype) |
|
|
|
|
|
|
312
|
|
|
{ |
|
313
|
59 |
|
$this->doctypeObj = parent::createDocumentType('html'); |
|
|
|
|
|
|
314
|
59 |
|
} |
|
315
|
|
|
|
|
316
|
|
|
/** |
|
317
|
|
|
* Creates a DOMDocument object of the specified type with its document element |
|
318
|
|
|
* |
|
319
|
|
|
* @return void |
|
320
|
|
|
*/ |
|
321
|
59 |
|
protected function createAframeDocument() |
|
322
|
|
|
{ |
|
323
|
59 |
|
$this->docObj = parent::createDocument(null, 'html', $this->doctypeObj); |
|
|
|
|
|
|
324
|
59 |
|
} |
|
325
|
|
|
|
|
326
|
|
|
/** |
|
327
|
|
|
* Create <head> element node |
|
328
|
|
|
* |
|
329
|
|
|
* @return void |
|
330
|
|
|
*/ |
|
331
|
59 |
|
protected function createHead() |
|
332
|
|
|
{ |
|
333
|
59 |
|
$this->head = $this->docObj->createElement('head'); |
|
334
|
59 |
|
} |
|
335
|
|
|
|
|
336
|
|
|
/** |
|
337
|
|
|
* Create <body> element node |
|
338
|
|
|
* |
|
339
|
|
|
* @return void |
|
340
|
|
|
*/ |
|
341
|
59 |
|
protected function createBody() |
|
342
|
|
|
{ |
|
343
|
59 |
|
$this->body = $this->docObj->createElement('body'); |
|
344
|
59 |
|
} |
|
345
|
|
|
|
|
346
|
|
|
/** |
|
347
|
|
|
* Create <a-scene> element node |
|
348
|
|
|
* |
|
349
|
|
|
* @return void |
|
350
|
|
|
*/ |
|
351
|
59 |
|
protected function createScene() |
|
352
|
|
|
{ |
|
353
|
59 |
|
$this->scene = $this->docObj->createElement('a-scene'); |
|
354
|
59 |
|
} |
|
355
|
|
|
} |
|
356
|
|
|
|
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.