/
Manager.php
125 lines (113 loc) · 3.12 KB
/
Manager.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
namespace Concrete\Core\Area\Layout\Preset\Provider;
use Concrete\Core\Area\Layout\Preset\InvalidPresetException;
use Concrete\Core\Area\Layout\Preset\PresetInterface;
class Manager
{
/**
* The keyed (by name) list of registered providers.
*
* @var \Concrete\Core\Area\Layout\Preset\Provider\ProviderInterface[]
*/
protected $providers = [];
/**
* The list of loaded presets.
*
* @var \Concrete\Core\Area\Layout\Preset\PresetInterface[]|null
*/
protected $presets;
/**
* Register a provider.
*/
public function register(ProviderInterface $provider)
{
$this->providers[$provider->getName()] = $provider;
$this->reset();
}
/**
* Reset the list of loaded presets.
*/
public function reset()
{
$this->presets = null;
}
/**
* Unregister a provider.
*
* @param \Concrete\Core\Area\Layout\Preset\Provider\ProviderInterface|string $nameOrObject
*/
public function unregister($nameOrObject)
{
if ($nameOrObject instanceof ProviderInterface) {
$name = $nameOrObject->getName();
} else {
$name = (string) $nameOrObject;
}
unset($this->providers[$name]);
$this->reset();
}
/**
* Get a provider by its name.
*
* @param string $name
*
* @return \Concrete\Core\Area\Layout\Preset\Provider\ProviderInterface|null
*/
public function getByName($name)
{
return $this->providers[$name] ?? null;
}
/**
* Get the keyed (by name) list of registered providers.
*
* @return \Concrete\Core\Area\Layout\Preset\Provider\ProviderInterface[]
*/
public function getProviders()
{
return $this->providers;
}
/**
* Get all the presets.
*
* @throws \Concrete\Core\Area\Layout\Preset\InvalidPresetException
*
* @return \Concrete\Core\Area\Layout\Preset\PresetInterface[]|null
*/
public function getPresets()
{
if ($this->presets !== null) {
return $this->presets;
}
$presets = [];
foreach ($this->providers as $provider) {
$presets = array_merge($presets, $provider->getPresets());
}
array_map(
static function ($preset) {
if (!($preset instanceof PresetInterface)) {
throw new InvalidPresetException(t('Items returned by getPresets() must implement the PresetInterface'));
}
},
$presets
);
$this->presets = $presets;
return $this->presets;
}
/**
* Get a preset by its identifier.
*
* @param string $identifier
*
* @return \Concrete\Core\Area\Layout\Preset\PresetInterface|null
*/
public function getPresetByIdentifier($identifier)
{
$results = array_filter(
$this->getPresets(),
static function (PresetInterface $preset) use ($identifier): bool {
return $preset->getIdentifier() == $identifier;
}
);
return array_pop($results);
}
}