1
|
|
|
<?php |
2
|
|
|
/** @formatter:off |
3
|
|
|
* ****************************************************************** |
4
|
|
|
* Created by Marko Kungla on Jun 29, 2016 - 8:06:17 AM |
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 AframeDOMProcessor.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
|
|
|
trait AframeDOMProcessor |
27
|
|
|
{ |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Add document comment for formatting |
31
|
|
|
* |
32
|
|
|
* @param string $element |
33
|
|
|
* @param string $comment |
34
|
|
|
*/ |
35
|
4 |
|
protected function appendFormatComment(string $element, string $comment) |
36
|
|
|
{ |
37
|
4 |
|
if ($this->formatOutput) { |
|
|
|
|
38
|
4 |
|
$com = $this->docObj->createComment($comment); |
|
|
|
|
39
|
4 |
|
$this->{$element}->appendChild($com); |
40
|
|
|
} |
41
|
4 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Correct html format for tags which are not supported by DOMDocument |
45
|
|
|
* |
46
|
|
|
* @param string $html |
47
|
|
|
* @return string |
48
|
|
|
*/ |
49
|
4 |
|
protected function correctOutputFormat($html) |
50
|
|
|
{ |
51
|
|
|
$tags = array( |
|
|
|
|
52
|
4 |
|
'<!--', |
53
|
|
|
'-->', |
54
|
|
|
'<a-assets>', |
55
|
|
|
'</a-assets>', |
56
|
|
|
'</a-scene>' |
57
|
|
|
); |
58
|
|
|
$values = array( |
59
|
4 |
|
'', |
60
|
|
|
"\t", |
61
|
|
|
"\n\t<a-assets>", |
62
|
|
|
"\n\t</a-assets>", |
63
|
|
|
"\n</a-scene>" |
64
|
|
|
); |
65
|
4 |
|
return str_ireplace($tags, $values, $html); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Prepeare head |
70
|
|
|
* |
71
|
|
|
* @return void |
72
|
|
|
*/ |
73
|
5 |
|
protected function renderHead() |
74
|
|
|
{ |
75
|
5 |
|
$title = $this->docObj->createElement('title', $this->scene_title); |
|
|
|
|
76
|
5 |
|
$this->head->appendChild($title); |
|
|
|
|
77
|
5 |
|
$this->appendDefaultMetaTags(); |
78
|
5 |
|
$this->appendCDN(); |
79
|
5 |
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Append deffault metatags |
83
|
|
|
* |
84
|
|
|
* @return void |
85
|
|
|
*/ |
86
|
5 |
|
protected function appendDefaultMetaTags() |
87
|
|
|
{ |
88
|
5 |
|
$this->appendMetaTag(array( |
89
|
5 |
|
'name' => 'description', |
90
|
5 |
|
'content' => $this->scene_description |
|
|
|
|
91
|
|
|
)); |
92
|
5 |
|
foreach ($this->getDefaultMetaTags() as $tag) |
93
|
5 |
|
$this->appendMetaTag($tag); |
94
|
5 |
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Get default meta tags |
98
|
|
|
* |
99
|
|
|
* @return array |
100
|
|
|
*/ |
101
|
5 |
|
protected function getDefaultMetaTags(): array |
102
|
|
|
{ |
103
|
5 |
|
$dmt = array(); |
|
|
|
|
104
|
5 |
|
$dmt[0]['charset'] = 'utf-8'; |
105
|
|
|
|
106
|
5 |
|
$dmt[1]['name'] = 'viewport'; |
107
|
|
|
|
108
|
5 |
|
$vp = 'width=device-width,initial-scale=1,maximum-scale=1,shrink-to-fit=no,user-scalable=no,minimal-ui'; |
109
|
|
|
|
110
|
5 |
|
$dmt[1]['content'] = $vp; |
111
|
5 |
|
$dmt[2]['name'] = 'mobile-web-app-capable'; |
|
|
|
|
112
|
5 |
|
$dmt[2]['content'] = 'yes'; |
113
|
5 |
|
$dmt[3]['name'] = 'theme-color'; |
|
|
|
|
114
|
5 |
|
$dmt[3]['content'] = 'black'; |
115
|
|
|
|
116
|
5 |
|
return $dmt; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* If requested by user use aframe CDN |
121
|
|
|
* |
122
|
|
|
* @return void |
123
|
|
|
*/ |
124
|
5 |
|
protected function appendCDN() |
125
|
|
|
{ |
126
|
5 |
|
if ($this->use_cdn) { |
|
|
|
|
127
|
3 |
|
$cdn_script = $this->docObj->createElement('script'); |
128
|
3 |
|
$cdn_script->setAttribute('src', $this->aframe_cdn); |
|
|
|
|
129
|
3 |
|
$this->head->appendChild($cdn_script); |
130
|
|
|
} |
131
|
5 |
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Prepare body |
135
|
|
|
* |
136
|
|
|
* @return void |
137
|
|
|
*/ |
138
|
5 |
|
protected function renderBody() |
139
|
|
|
{ |
140
|
5 |
|
$this->body->appendChild($this->scene); |
|
|
|
|
141
|
5 |
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Create meta tags |
145
|
|
|
* |
146
|
|
|
* @param array $attr |
147
|
|
|
*/ |
148
|
5 |
|
protected function appendMetaTag(array $attr) |
149
|
|
|
{ |
150
|
5 |
|
$metatag = $this->docObj->createElement('meta'); |
151
|
5 |
|
foreach ($attr as $key => $val) |
152
|
5 |
|
$metatag->setAttribute($key, $val); |
153
|
5 |
|
$this->head->appendChild($metatag); |
154
|
5 |
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Creates an empty DOMDocumentType object |
158
|
|
|
* |
159
|
|
|
* @param string $doctype |
160
|
|
|
* @return void |
161
|
|
|
*/ |
162
|
71 |
|
protected function createDocType(string $doctype) |
163
|
|
|
{ |
164
|
71 |
|
$this->doctypeObj = $this->createDocumentType($doctype); |
|
|
|
|
165
|
71 |
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Creates a DOMDocument object of the specified type with its document element |
169
|
|
|
* |
170
|
|
|
* @return void |
171
|
|
|
*/ |
172
|
71 |
|
protected function createAframeDocument() |
173
|
|
|
{ |
174
|
71 |
|
$this->docObj = $this->createDocument(null, 'html', $this->doctypeObj); |
|
|
|
|
175
|
71 |
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Create dom elements for DOMDocument |
179
|
|
|
* |
180
|
|
|
* @return void |
181
|
|
|
*/ |
182
|
71 |
|
protected function documentBootstrap() |
183
|
|
|
{ |
184
|
|
|
/* Create <head> element */ |
185
|
71 |
|
$this->head = $this->docObj->createElement('head'); |
186
|
|
|
/* Create <body> element */ |
187
|
71 |
|
$this->body = $this->docObj->createElement('body', $this->formatOutput ? "\n" : ''); |
188
|
|
|
/* Create <a-scene> element */ |
189
|
71 |
|
$this->scene = $this->docObj->createElement('a-scene'); |
190
|
|
|
/* Create <a-assets> element */ |
191
|
71 |
|
$this->assets = $this->docObj->createElement('a-assets'); |
|
|
|
|
192
|
71 |
|
} |
193
|
|
|
} |
194
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: