1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Disqus Helper package. |
4
|
|
|
* |
5
|
|
|
* Copyright (c) Nikola Posa <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For full copyright and license information, please refer to the LICENSE file, |
8
|
|
|
* located at the package root folder. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
declare(strict_types=1); |
12
|
|
|
|
13
|
|
|
namespace DisqusHelper; |
14
|
|
|
|
15
|
|
|
final class Code |
16
|
|
|
{ |
17
|
|
|
const INDENT = ' '; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
private $shortName; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
private $config = []; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
private $scriptFiles = []; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var array |
36
|
|
|
*/ |
37
|
|
|
private $htmlFragments = []; |
38
|
|
|
|
39
|
17 |
|
private function __construct() |
40
|
|
|
{ |
41
|
17 |
|
} |
42
|
|
|
|
43
|
17 |
|
public static function create(string $shortName) : Code |
44
|
|
|
{ |
45
|
17 |
|
$code = new self(); |
46
|
|
|
|
47
|
17 |
|
$code->shortName = $shortName; |
48
|
|
|
|
49
|
17 |
|
return $code; |
50
|
|
|
} |
51
|
|
|
|
52
|
4 |
|
public function mergeConfig(array $config) : Code |
53
|
|
|
{ |
54
|
4 |
|
$this->config = array_merge($this->config, $config); |
55
|
|
|
|
56
|
4 |
|
return $this; |
57
|
|
|
} |
58
|
|
|
|
59
|
1 |
|
public function getConfig() : array |
60
|
|
|
{ |
61
|
1 |
|
return $this->config; |
62
|
|
|
} |
63
|
|
|
|
64
|
12 |
|
public function addScriptFile(string $name, array $options = []) : Code |
65
|
|
|
{ |
66
|
12 |
|
if (!isset($this->scriptFiles[$name])) { |
67
|
12 |
|
$data = array_merge([ |
68
|
12 |
|
'name' => $name, |
69
|
|
|
'id' => null, |
70
|
|
|
'lazy_load' => false, |
71
|
|
|
], $options); |
72
|
|
|
|
73
|
12 |
|
$this->scriptFiles[$name] = $data; |
74
|
|
|
} |
75
|
|
|
|
76
|
12 |
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
3 |
|
public function hasScriptFile(string $fileName) : bool |
80
|
|
|
{ |
81
|
3 |
|
return isset($this->scriptFiles[$fileName]); |
82
|
|
|
} |
83
|
|
|
|
84
|
1 |
|
public function __toString() : string |
85
|
|
|
{ |
86
|
1 |
|
return $this->toHtml(); |
87
|
|
|
} |
88
|
|
|
|
89
|
9 |
|
public function toHtml() : string |
90
|
|
|
{ |
91
|
9 |
|
$this->htmlFragments = []; |
92
|
|
|
|
93
|
9 |
|
$this->buildConfigVariablesHtml(); |
94
|
9 |
|
$this->buildJsFilesHtml(); |
95
|
|
|
|
96
|
9 |
|
return PHP_EOL . implode(PHP_EOL . PHP_EOL, $this->htmlFragments) . PHP_EOL; |
97
|
|
|
} |
98
|
|
|
|
99
|
9 |
|
private function buildConfigVariablesHtml() |
100
|
|
|
{ |
101
|
9 |
|
if (empty($this->config)) { |
102
|
6 |
|
return; |
103
|
|
|
} |
104
|
|
|
|
105
|
3 |
|
$script = '<script>' . PHP_EOL; |
106
|
3 |
|
$script .= self::INDENT . 'var disqus_config = function () {' . PHP_EOL; |
107
|
|
|
|
108
|
3 |
|
foreach ($this->config as $key => $value) { |
109
|
3 |
|
if (is_string($value)) { |
110
|
3 |
|
$value = addslashes($value); |
111
|
3 |
|
$value = "'$value'"; |
112
|
|
|
} |
113
|
3 |
|
$script .= self::INDENT . self::INDENT . "this.$key = $value;" . PHP_EOL; |
114
|
|
|
} |
115
|
|
|
|
116
|
3 |
|
$script .= self::INDENT . '};' . PHP_EOL ; |
117
|
3 |
|
$script .= '</script>'; |
118
|
|
|
|
119
|
3 |
|
$this->htmlFragments[] = $script; |
120
|
3 |
|
} |
121
|
|
|
|
122
|
9 |
|
private function buildJsFilesHtml() |
123
|
|
|
{ |
124
|
9 |
|
foreach ($this->scriptFiles as $fileData) { |
125
|
8 |
|
if ($fileData['lazy_load']) { |
126
|
3 |
|
$this->htmlFragments[] = $this->renderLazyLoadedJsFile($fileData); |
127
|
3 |
|
continue; |
128
|
|
|
} |
129
|
|
|
|
130
|
5 |
|
$this->htmlFragments[] = $this->renderJsFile($fileData); |
131
|
|
|
} |
132
|
9 |
|
} |
133
|
|
|
|
134
|
5 |
|
private function renderJsFile(array $fileData) : string |
135
|
|
|
{ |
136
|
5 |
|
return sprintf( |
137
|
5 |
|
'<script id="%2$s" src="%1$s" async></script>', |
138
|
5 |
|
$this->getJsFileUrl($fileData['name']), |
139
|
5 |
|
$fileData['id'] |
140
|
|
|
); |
141
|
|
|
} |
142
|
|
|
|
143
|
3 |
|
private function renderLazyLoadedJsFile(array $fileData) : string |
144
|
|
|
{ |
145
|
3 |
|
$script = '<script>' . PHP_EOL; |
146
|
3 |
|
$script .= self::INDENT . '(function() {' . PHP_EOL; |
147
|
3 |
|
$script .= self::INDENT . self::INDENT . 'var d = document, s = d.createElement("script");' . PHP_EOL; |
148
|
3 |
|
$script .= self::INDENT . self::INDENT . 's.src = "' . $this->getJsFileUrl($fileData['name']) . '";' . PHP_EOL; |
149
|
3 |
|
$script .= self::INDENT . self::INDENT . 's.setAttribute("data-timestamp", +new Date());' . PHP_EOL; |
150
|
3 |
|
$script .= self::INDENT . self::INDENT . '(d.head || d.body).appendChild(s);' . PHP_EOL; |
151
|
3 |
|
$script .= self::INDENT . '})();' . PHP_EOL; |
152
|
3 |
|
$script .= '</script>'; |
153
|
|
|
|
154
|
3 |
|
return $script; |
155
|
|
|
} |
156
|
|
|
|
157
|
8 |
|
private function getJsFileUrl(string $fileName) : string |
158
|
|
|
{ |
159
|
8 |
|
return '//' . $this->shortName . '.disqus.com/' . $fileName; |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|