-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMetadataConfig.php
174 lines (153 loc) · 4.29 KB
/
MetadataConfig.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php
namespace Charcoal\Model\Service;
use InvalidArgumentException;
// From 'charcoal-config'
use Charcoal\Config\AbstractConfig;
/**
* Metadata Configset
*
* Stores the metadata loader's settings, search paths, and caching service.
*/
class MetadataConfig extends AbstractConfig
{
/**
* Metadata search paths.
*
* @var array
*/
private $paths = [];
/**
* The PSR-6 caching service or cache identifier(s) to use.
*
* @var mixed
*/
private $cache = true;
/**
* Retrieve the default values.
*
* @param string|null $key Optional data key to retrieve.
* @return mixed An associative array if $key is NULL.
* If $key is specified, the value of that data key if it exists, NULL on failure.
*/
public function defaults($key = null)
{
$data = [
'paths' => [],
'cache' => true,
];
if ($key) {
return isset($data[$key]) ? $data[$key] : null;
}
return $data;
}
/**
* Add settings to configset, replacing existing settings with the same data key.
*
* @see \Charcoal\Config\AbstractConfig::merge()
* @param array|Traversable $data The data to merge.
* @return self
*/
public function merge($data)
{
foreach ($data as $key => $val) {
if ($key === 'paths') {
$this->addPaths((array)$val);
} elseif ($key === 'cache') {
$this->setCache($val);
} else {
$this->offsetReplace($key, $val);
}
}
return $this;
}
/**
* @return array
*/
public function paths()
{
return $this->paths;
}
/**
* @param string[] $paths One or more search paths.
* @return self
*/
public function setPaths(array $paths)
{
$this->paths = [];
$this->addPaths($paths);
return $this;
}
/**
* @param string[] $paths One or more search paths.
* @return self
*/
public function addPaths(array $paths)
{
foreach ($paths as $path) {
$this->addPath($path);
}
return $this;
}
/**
* @param string $path A directory path.
* @throws InvalidArgumentException If the path is not a string.
* @return self
*/
public function addPath($path)
{
if (!is_string($path)) {
throw new InvalidArgumentException(
'Metadata path must be a string'
);
}
$this->paths[] = $path;
return $this;
}
/**
* @return mixed
*/
public function cache()
{
return isset($this->cache) ? $this->cache : false;
}
/**
* @param mixed $cache The cache service for
* the {@see \Charcoal\Model\Service\MetadataLoader}. If $cache is:
* - NULL, the {@see self::defaults() default value} will be applied.
* - TRUE (default), the default "cache" service will be used.
* - FALSE, the {@see \Charcoal\Model\Service\MetadataLoader} will use
* a running memory cache.
* - one or more {@see \Charcoal\App\Config\CacheConfig::validTypes() cache driver keys},
* the first cache driver available on the system will be used.
* - a {@see \Psr\Cache\CacheItemPoolInterface PSR-6 caching service},
* that instance will be used by the {@see \Charcoal\Model\Service\MetadataLoader}.
* @throws InvalidArgumentException If the cache option is invalid.
* @return self
*/
public function setCache($cache)
{
if ($cache === null) {
$this->cache = $this->defaults('cache');
return $this;
}
if (is_bool($cache)) {
$this->cache = $cache;
return $this;
}
if (is_string($cache)) {
$this->cache = (array)$cache;
return $this;
}
if (is_array($cache)) {
$this->cache = $cache;
return $this;
}
if (is_object($cache)) {
$this->cache = $cache;
return $this;
}
throw new InvalidArgumentException(
'Metadata cache must be a cache driver key, a PSR-6 cache pool instance, or boolean'
);
}
}