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
|
|
|
use DisqusHelper\Widget\WidgetLocatorInterface; |
14
|
|
|
use DisqusHelper\Widget\WidgetManager; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @author Nikola Posa <[email protected]> |
18
|
|
|
*/ |
19
|
|
|
final class Disqus |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
private $shortName; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var WidgetLocatorInterface |
28
|
|
|
*/ |
29
|
|
|
private $widgetLocator; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var Code |
33
|
|
|
*/ |
34
|
|
|
private $code; |
35
|
|
|
|
36
|
|
|
public function __construct() |
37
|
|
|
{ |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param string $shortName Unique identifier of some Disqus website. |
42
|
|
|
* @param WidgetLocatorInterface $widgetLocator OPTIONAL |
43
|
|
|
* |
44
|
|
|
* @return Disqus |
45
|
|
|
*/ |
46
|
|
|
public static function create( |
47
|
|
|
string $shortName, |
48
|
|
|
WidgetLocatorInterface $widgetLocator = null |
49
|
|
|
) : Disqus { |
50
|
|
|
$disqusHelper = new self(); |
51
|
|
|
|
52
|
|
|
$disqusHelper->shortName = $shortName; |
53
|
|
|
|
54
|
|
|
if (is_null($widgetLocator)) { |
55
|
|
|
$widgetLocator = WidgetManager::createWithDefaultWidgets(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$disqusHelper->widgetLocator = $widgetLocator; |
59
|
|
|
|
60
|
|
|
$disqusHelper->code = Code::create($shortName); |
61
|
|
|
|
62
|
|
|
return $disqusHelper; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function getShortName() : string |
66
|
|
|
{ |
67
|
|
|
return $this->shortName; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function configure(array $config) |
71
|
|
|
{ |
72
|
|
|
$this->code->mergeConfig($config); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Proxies calls to some Disqus widget and returns HTML output. |
77
|
|
|
* |
78
|
|
|
* @param string $widgetId |
79
|
|
|
* @param array $args |
80
|
|
|
* |
81
|
|
|
* @throws Exception\InvalidArgumentException |
82
|
|
|
* |
83
|
|
|
* @return string |
84
|
|
|
*/ |
85
|
|
|
public function __call($widgetId, $args) : string |
86
|
|
|
{ |
87
|
|
|
$widget = $this->widgetLocator->get($widgetId); |
88
|
|
|
|
89
|
|
|
if (($options = array_shift($args)) !== null) { |
90
|
|
|
if (!is_array($options)) { |
91
|
|
|
throw new Exception\InvalidArgumentException("Widget options argument should be array"); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$widget->visit($this->code); |
96
|
|
|
|
97
|
|
|
return $widget->render($options ?: []); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Builds JS code which loads configuration and necessary assets. |
102
|
|
|
* |
103
|
|
|
* This method should be called after using and rendering widgets, usually before closing </body> tag. |
104
|
|
|
* |
105
|
|
|
* @return string |
106
|
|
|
*/ |
107
|
|
|
public function getCode() : string |
108
|
|
|
{ |
109
|
|
|
return $this->code->toHtml(); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function __toString() : string |
113
|
|
|
{ |
114
|
|
|
return $this->getCode(); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|