|
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
|
|
|
namespace DisqusHelper; |
|
12
|
|
|
|
|
13
|
|
|
final class Code |
|
14
|
|
|
{ |
|
15
|
|
|
const INDENT = ' '; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var string |
|
19
|
|
|
*/ |
|
20
|
|
|
private $shortName; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var array |
|
24
|
|
|
*/ |
|
25
|
|
|
private $config = []; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var array |
|
29
|
|
|
*/ |
|
30
|
|
|
private $scriptFiles = []; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var string |
|
34
|
|
|
*/ |
|
35
|
|
|
private $html; |
|
36
|
|
|
|
|
37
|
|
|
private function __construct() |
|
38
|
|
|
{ |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public static function create(string $shortName) : Code |
|
42
|
|
|
{ |
|
43
|
|
|
$code = new self(); |
|
44
|
|
|
|
|
45
|
|
|
$code->shortName = $shortName; |
|
46
|
|
|
|
|
47
|
|
|
return $code; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function mergeConfig(array $config) : Code |
|
51
|
|
|
{ |
|
52
|
|
|
$this->config = array_merge($this->config, $config); |
|
53
|
|
|
|
|
54
|
|
|
return $this; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function getConfig() : array |
|
58
|
|
|
{ |
|
59
|
|
|
return $this->config; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function addScriptFile(string $fileName) : Code |
|
63
|
|
|
{ |
|
64
|
|
|
if (!isset($this->scriptFiles[$fileName])) { |
|
65
|
|
|
$this->scriptFiles[$fileName] = $fileName; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return $this; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function hasScriptFile(string $fileName) : bool |
|
72
|
|
|
{ |
|
73
|
|
|
return isset($this->scriptFiles[$fileName]); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function __toString() : string |
|
77
|
|
|
{ |
|
78
|
|
|
return $this->toHtml(); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function toHtml() : string |
|
82
|
|
|
{ |
|
83
|
|
|
$this->html = ''; |
|
84
|
|
|
|
|
85
|
|
|
$this->html = '<script>'; |
|
86
|
|
|
$this->html .= PHP_EOL; |
|
87
|
|
|
|
|
88
|
|
|
$this->renderConfigVariables(); |
|
89
|
|
|
$this->renderJsFiles(); |
|
90
|
|
|
|
|
91
|
|
|
$this->html = rtrim($this->html, PHP_EOL); |
|
92
|
|
|
$this->html .= PHP_EOL; |
|
93
|
|
|
$this->html .= '</script>'; |
|
94
|
|
|
|
|
95
|
|
|
return $this->html; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
private function renderConfigVariables() |
|
99
|
|
|
{ |
|
100
|
|
|
if (empty($this->config)) { |
|
101
|
|
|
return; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
$this->html .= self::INDENT . 'var disqus_config = function () {' . PHP_EOL; |
|
105
|
|
|
|
|
106
|
|
|
foreach ($this->config as $key => $value) { |
|
107
|
|
|
if (is_string($value)) { |
|
108
|
|
|
$value = addslashes($value); |
|
109
|
|
|
$value = "'$value'"; |
|
110
|
|
|
} |
|
111
|
|
|
$this->html .= self::INDENT . self::INDENT . "this.$key = $value;" . PHP_EOL; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
$this->html .= self::INDENT . '};' . PHP_EOL . PHP_EOL; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
private function renderJsFiles() |
|
118
|
|
|
{ |
|
119
|
|
|
foreach ($this->scriptFiles as $fileName) { |
|
120
|
|
|
$this->html .= self::INDENT . '(function() {' . PHP_EOL; |
|
121
|
|
|
$this->html .= self::INDENT . self::INDENT . 'var d = document, s = d.createElement("script");' . PHP_EOL; |
|
122
|
|
|
$this->html .= self::INDENT . self::INDENT . 's.src = "//' . $this->shortName . '.disqus.com/' . $fileName . '";' . PHP_EOL; |
|
123
|
|
|
$this->html .= self::INDENT . self::INDENT . 's.setAttribute("data-timestamp", +new Date());' . PHP_EOL; |
|
124
|
|
|
$this->html .= self::INDENT . self::INDENT . '(d.head || d.body).appendChild(s);' . PHP_EOL; |
|
125
|
|
|
$this->html .= self::INDENT . '})();' . PHP_EOL . PHP_EOL; |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
} |