|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Charcoal\Model\Service; |
|
4
|
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
|
6
|
|
|
|
|
7
|
|
|
// From 'charcoal-config' |
|
8
|
|
|
use Charcoal\Config\AbstractConfig; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Metadata Configset |
|
12
|
|
|
* |
|
13
|
|
|
* Stores the metadata loader's settings, search paths, and caching service. |
|
14
|
|
|
*/ |
|
15
|
|
|
class MetadataConfig extends AbstractConfig |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* Metadata search paths. |
|
19
|
|
|
* |
|
20
|
|
|
* @var array |
|
21
|
|
|
*/ |
|
22
|
|
|
private $paths = []; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* The PSR-6 caching service or cache identifier(s) to use. |
|
26
|
|
|
* |
|
27
|
|
|
* @var mixed |
|
28
|
|
|
*/ |
|
29
|
|
|
private $cache = true; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Retrieve the default values. |
|
33
|
|
|
* |
|
34
|
|
|
* @param string|null $key Optional data key to retrieve. |
|
35
|
|
|
* @return mixed An associative array if $key is NULL. |
|
36
|
|
|
* If $key is specified, the value of that data key if it exists, NULL on failure. |
|
37
|
|
|
*/ |
|
38
|
|
|
public function defaults($key = null) |
|
39
|
|
|
{ |
|
40
|
|
|
$data = [ |
|
41
|
|
|
'paths' => [], |
|
42
|
|
|
'cache' => true, |
|
43
|
|
|
]; |
|
44
|
|
|
|
|
45
|
|
|
if ($key) { |
|
46
|
|
|
return isset($data[$key]) ? $data[$key] : null; |
|
|
|
|
|
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
return $data; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Add settings to configset, replacing existing settings with the same data key. |
|
54
|
|
|
* |
|
55
|
|
|
* @see \Charcoal\Config\AbstractConfig::merge() |
|
56
|
|
|
* @param array|Traversable $data The data to merge. |
|
|
|
|
|
|
57
|
|
|
* @return self |
|
58
|
|
|
*/ |
|
59
|
|
|
public function merge($data) |
|
60
|
|
|
{ |
|
61
|
|
|
foreach ($data as $key => $val) { |
|
62
|
|
|
if ($key === 'paths') { |
|
63
|
|
|
$this->addPaths((array)$val); |
|
64
|
|
|
} elseif ($key === 'cache') { |
|
65
|
|
|
$this->setCache($val); |
|
66
|
|
|
} else { |
|
67
|
|
|
$this->offsetReplace($key, $val); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
return $this; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @return array |
|
76
|
|
|
*/ |
|
77
|
|
|
public function paths() |
|
78
|
|
|
{ |
|
79
|
|
|
return $this->paths; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @param string[] $paths One or more search paths. |
|
84
|
|
|
* @return self |
|
85
|
|
|
*/ |
|
86
|
|
|
public function setPaths(array $paths) |
|
87
|
|
|
{ |
|
88
|
|
|
$this->paths = []; |
|
89
|
|
|
$this->addPaths($paths); |
|
90
|
|
|
return $this; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @param string[] $paths One or more search paths. |
|
95
|
|
|
* @return self |
|
96
|
|
|
*/ |
|
97
|
|
|
public function addPaths(array $paths) |
|
98
|
|
|
{ |
|
99
|
|
|
foreach ($paths as $path) { |
|
100
|
|
|
$this->addPath($path); |
|
101
|
|
|
} |
|
102
|
|
|
return $this; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @param string $path A directory path. |
|
107
|
|
|
* @throws InvalidArgumentException If the path is not a string. |
|
108
|
|
|
* @return self |
|
109
|
|
|
*/ |
|
110
|
|
|
public function addPath($path) |
|
111
|
|
|
{ |
|
112
|
|
|
if (!is_string($path)) { |
|
|
|
|
|
|
113
|
|
|
throw new InvalidArgumentException( |
|
114
|
|
|
'Metadata path must be a string' |
|
115
|
|
|
); |
|
116
|
|
|
} |
|
117
|
|
|
$this->paths[] = $path; |
|
118
|
|
|
return $this; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @return mixed |
|
123
|
|
|
*/ |
|
124
|
|
|
public function cache() |
|
125
|
|
|
{ |
|
126
|
|
|
return isset($this->cache) ? $this->cache : false; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @param mixed $cache The cache service for |
|
131
|
|
|
* the {@see \Charcoal\Model\Service\MetadataLoader}. If $cache is: |
|
132
|
|
|
* - NULL, the {@see self::defaults() default value} will be applied. |
|
133
|
|
|
* - TRUE (default), the default "cache" service will be used. |
|
134
|
|
|
* - FALSE, the {@see \Charcoal\Model\Service\MetadataLoader} will use |
|
135
|
|
|
* a running memory cache. |
|
136
|
|
|
* - one or more {@see \Charcoal\App\Config\CacheConfig::validTypes() cache driver keys}, |
|
137
|
|
|
* the first cache driver available on the system will be used. |
|
138
|
|
|
* - a {@see \Psr\Cache\CacheItemPoolInterface PSR-6 caching service}, |
|
139
|
|
|
* that instance will be used by the {@see \Charcoal\Model\Service\MetadataLoader}. |
|
140
|
|
|
* @throws InvalidArgumentException If the cache option is invalid. |
|
141
|
|
|
* @return self |
|
142
|
|
|
*/ |
|
143
|
|
|
public function setCache($cache) |
|
144
|
|
|
{ |
|
145
|
|
|
if ($cache === null) { |
|
146
|
|
|
$this->cache = $this->defaults('cache'); |
|
147
|
|
|
return $this; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
if (is_bool($cache)) { |
|
151
|
|
|
$this->cache = $cache; |
|
152
|
|
|
return $this; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
if (is_string($cache)) { |
|
156
|
|
|
$this->cache = (array)$cache; |
|
157
|
|
|
return $this; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
if (is_array($cache)) { |
|
161
|
|
|
$this->cache = $cache; |
|
162
|
|
|
return $this; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
if (is_object($cache)) { |
|
166
|
|
|
$this->cache = $cache; |
|
167
|
|
|
return $this; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
throw new InvalidArgumentException( |
|
171
|
|
|
'Metadata cache must be a cache driver key, a PSR-6 cache pool instance, or boolean' |
|
172
|
|
|
); |
|
173
|
|
|
} |
|
174
|
|
|
} |
|
175
|
|
|
|