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\Widget; |
14
|
|
|
|
15
|
|
|
use DisqusHelper\Exception\InvalidWidgetConfigurationException; |
16
|
|
|
use DisqusHelper\Exception\InvalidWidgetException; |
17
|
|
|
use DisqusHelper\Exception\WidgetNotFoundException; |
18
|
|
|
|
19
|
|
|
class WidgetManager implements WidgetLocatorInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
protected $widgets; |
25
|
|
|
|
26
|
17 |
|
private function __construct() |
27
|
|
|
{ |
28
|
17 |
|
} |
29
|
|
|
|
30
|
17 |
|
public static function create(array $widgets) : WidgetManager |
31
|
|
|
{ |
32
|
17 |
|
$widgetManager = new self(); |
33
|
|
|
|
34
|
17 |
|
foreach ($widgets as $widgetId => $widget) { |
35
|
16 |
|
$widgetManager->registerWidget($widgetId, $widget); |
36
|
|
|
} |
37
|
|
|
|
38
|
17 |
|
return $widgetManager; |
39
|
|
|
} |
40
|
|
|
|
41
|
10 |
|
public static function createWithDefaultWidgets() : WidgetManager |
42
|
|
|
{ |
43
|
10 |
|
return self::create([ |
44
|
10 |
|
'thread' => ThreadWidget::class, |
45
|
|
|
'commentscount' => CommentsCountWidget::class, |
46
|
|
|
]); |
47
|
|
|
} |
48
|
|
|
|
49
|
17 |
|
public function registerWidget(string $widgetId, $widget) |
50
|
|
|
{ |
51
|
17 |
|
if (!($widget instanceof WidgetInterface || is_string($widget) || is_callable($widget))) { |
52
|
1 |
|
throw InvalidWidgetConfigurationException::forConfiguration($widgetId, $widget); |
53
|
|
|
} |
54
|
|
|
|
55
|
16 |
|
$widgetId = strtolower($widgetId); |
56
|
|
|
|
57
|
16 |
|
$this->widgets[$widgetId] = $widget; |
58
|
16 |
|
} |
59
|
|
|
|
60
|
4 |
|
public function has(string $widgetId) : bool |
61
|
|
|
{ |
62
|
4 |
|
$widgetId = strtolower($widgetId); |
63
|
|
|
|
64
|
4 |
|
return isset($this->widgets[$widgetId]); |
65
|
|
|
} |
66
|
|
|
|
67
|
10 |
|
public function get(string $widgetId) : WidgetInterface |
68
|
|
|
{ |
69
|
10 |
|
$widgetId = strtolower($widgetId); |
70
|
|
|
|
71
|
10 |
|
if (!isset($this->widgets[$widgetId])) { |
72
|
2 |
|
throw WidgetNotFoundException::forWidgetId($widgetId); |
73
|
|
|
} |
74
|
|
|
|
75
|
8 |
|
$widget = $this->createWidget($widgetId); |
76
|
|
|
|
77
|
8 |
|
if (!$widget instanceof WidgetInterface) { |
78
|
1 |
|
throw InvalidWidgetException::forWidget($widget); |
79
|
|
|
} |
80
|
|
|
|
81
|
7 |
|
return $widget; |
82
|
|
|
} |
83
|
|
|
|
84
|
8 |
|
protected function createWidget($widgetId) |
85
|
|
|
{ |
86
|
8 |
|
$widget = $this->widgets[$widgetId]; |
87
|
|
|
|
88
|
8 |
|
if (is_string($widget)) { |
89
|
5 |
|
return new $widget(); |
90
|
|
|
} |
91
|
|
|
|
92
|
3 |
|
if (is_callable($widget)) { |
93
|
2 |
|
return $widget(); |
94
|
|
|
} |
95
|
|
|
|
96
|
1 |
|
return $widget; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|