|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Displore\Widgets; |
|
4
|
|
|
|
|
5
|
|
|
class WidgetsProvider |
|
6
|
|
|
{ |
|
7
|
|
|
/** |
|
8
|
|
|
* Providers are the files that provide a widget. |
|
9
|
|
|
* |
|
10
|
|
|
* @var array |
|
11
|
|
|
*/ |
|
12
|
|
|
protected $providers; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* This matches widget names with their providers. |
|
16
|
|
|
* |
|
17
|
|
|
* @var array |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $widgets; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Construct a new instance. |
|
23
|
|
|
* |
|
24
|
|
|
* @param array $providers |
|
25
|
|
|
* @param array|null $cachedWidgets |
|
26
|
|
|
*/ |
|
27
|
|
|
public function __construct(array $providers, array $cachedWidgets = null) |
|
28
|
|
|
{ |
|
29
|
|
|
$this->providers = $providers; |
|
30
|
|
|
if ( ! is_null($cachedWidgets)) { |
|
31
|
|
|
$this->widgets = $cachedWidgets; |
|
32
|
|
|
} |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Set a new widget. |
|
37
|
|
|
* |
|
38
|
|
|
* @param string $name |
|
39
|
|
|
* @param string $provider |
|
40
|
|
|
*/ |
|
41
|
|
|
public function set($name, $provider) |
|
42
|
|
|
{ |
|
43
|
|
|
$this->widgets[$name] = $provider; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Get a widget. |
|
48
|
|
|
* |
|
49
|
|
|
* @param string $name |
|
50
|
|
|
* @param array|null $parameters |
|
51
|
|
|
* |
|
52
|
|
|
* @throws WidgetNotFoundException |
|
53
|
|
|
*/ |
|
54
|
|
|
public function get($name, array $parameters = null) |
|
55
|
|
|
{ |
|
56
|
|
|
if (isset($this->widgets[$name])) { |
|
57
|
|
|
return $this->widgets[$name]->getWidget($parameters); |
|
58
|
|
|
} elseif (isset($this->providers[$name])) { |
|
59
|
|
|
return $this->getProvider($name)->getWidget($parameters); |
|
60
|
|
|
} |
|
61
|
|
|
throw new \Exception('Widget not found'); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Get the provider for the widget. |
|
66
|
|
|
* |
|
67
|
|
|
* @param string $widget |
|
68
|
|
|
* |
|
69
|
|
|
* @return object |
|
70
|
|
|
*/ |
|
71
|
|
|
public function getProvider($widget) |
|
72
|
|
|
{ |
|
73
|
|
|
return $this->widgets[$widget] = new $this->providers[$widget](); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Get all registered widgets. |
|
78
|
|
|
* |
|
79
|
|
|
* @return array |
|
80
|
|
|
*/ |
|
81
|
|
|
public function getWidgets() |
|
82
|
|
|
{ |
|
83
|
|
|
return $this->widgets; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|