Skip to content

Commit 6016168

Browse files
authored
Add parent_config_name directive to YAML reader (#4930)
1 parent 04addfd commit 6016168

File tree

13 files changed

+117
-14
lines changed

13 files changed

+117
-14
lines changed

config/vufind/searchspecs.yaml

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -227,14 +227,22 @@
227227
#
228228
# "@parent_yaml": "/path/to/my/file.yaml"
229229
#
230-
# Only sections not found in this file will be loaded in from the parent file.
230+
# If @parent_yaml cannot be accessed as an absolute path, it will also be tried
231+
# relative to the path of the file defining it.
232+
233+
# You can also use "@parent_config_name" instead, to use another configuration as
234+
# parent. The whole configuration directory stack will be searched for this configuration
235+
# instead of just using a specific file. For example:
236+
#
237+
# "@parent_config_name": "myconfig"
238+
#
239+
# Only one of the parent directives can be used at a time.
240+
#
241+
# In both cases, only sections not found in this file will be loaded in from the parent file.
231242
# In some complex scenarios, this can be a useful way of sharing settings
232243
# between multiple configured VuFind instances. You can create a chain of parent
233244
# files if necessary.
234245
#
235-
# If @parent_yaml cannot be accessed as an absolute path, it will also be tried
236-
# relative to the path of the file defining it.
237-
#
238246
#-----------------------------------------------------------------------------------
239247

240248
# These searches use Dismax when possible:

config/vufind/searchspecs2.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# This is the search configuration for the secondary search handler.
22
# By default, it simply inherits rules from the parent handler.
3-
"@parent_yaml": "searchspecs.yaml"
3+
"@parent_yaml": "searchspecs.yaml"

module/VuFind/src/VuFind/Config/YamlReader.php

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ public function get($filename, $useLocalConfig = true, $forceReload = false)
9999
$baseConfigPath = $this->pathResolver->getBaseConfigPath($filename);
100100
$this->files[$filename] = $this->getFromPaths(
101101
$baseConfigPath,
102-
$localConfigPath
102+
$localConfigPath,
103+
$useLocalConfig
103104
);
104105
}
105106

@@ -109,13 +110,14 @@ public function get($filename, $useLocalConfig = true, $forceReload = false)
109110
/**
110111
* Given core and local filenames, retrieve the configuration data.
111112
*
112-
* @param string $defaultFile Full path to file containing default YAML
113-
* @param string $customFile Full path to file containing local customizations
113+
* @param string $defaultFile Full path to file containing default YAML
114+
* @param string $customFile Full path to file containing local customizations
114115
* (may be null if no local file exists).
116+
* @param bool $useLocalConfig Use local configuration if available
115117
*
116118
* @return array
117119
*/
118-
protected function getFromPaths($defaultFile, $customFile = null)
120+
protected function getFromPaths($defaultFile, $customFile = null, $useLocalConfig = true)
119121
{
120122
// Connect to the cache:
121123
$cache = (null !== $this->cacheManager)
@@ -139,7 +141,7 @@ protected function getFromPaths($defaultFile, $customFile = null)
139141

140142
// Generate data if not found in cache:
141143
if ($cache === false || !($results = $cache->getItem($cacheKey))) {
142-
$results = $this->parseYaml($customFile, $defaultFile);
144+
$results = $this->parseYaml($customFile, $defaultFile, $useLocalConfig);
143145
if ($cache !== false) {
144146
$cache->setItem($cacheKey, $results);
145147
}
@@ -151,20 +153,28 @@ protected function getFromPaths($defaultFile, $customFile = null)
151153
/**
152154
* Process a YAML file (and its parent, if necessary).
153155
*
154-
* @param string $file YAML file to load (will evaluate to null
156+
* @param string $file YAML file to load (will evaluate to null
155157
* if file does not exist).
156-
* @param string $defaultParent Parent YAML file from which $file should
158+
* @param string $defaultParent Parent YAML file from which $file should
157159
* inherit (unless overridden by a specific directive in $file). None by
158160
* default.
161+
* @param bool $useLocalConfig Use local configuration if available
159162
*
160163
* @return array
161164
*/
162-
protected function parseYaml($file, $defaultParent = null)
165+
protected function parseYaml($file, $defaultParent = null, $useLocalConfig = true)
163166
{
164167
// First load current file:
165168
$results = (!empty($file) && file_exists($file))
166169
? Yaml::parse(file_get_contents($file)) : [];
167170

171+
if (isset($results['@parent_yaml']) && isset($results['@parent_config_name'])) {
172+
error_log(
173+
'Cannot use both directives @parent_yaml and '
174+
. '@parent_config_name at the same time in one file.'
175+
);
176+
}
177+
168178
// Override default parent with explicitly-defined parent, if present:
169179
if (isset($results['@parent_yaml'])) {
170180
// First try parent as absolute path, then as relative:
@@ -178,6 +188,22 @@ protected function parseYaml($file, $defaultParent = null)
178188
// Swallow the directive after processing it:
179189
unset($results['@parent_yaml']);
180190
}
191+
// Override default parent with a named configuration, if present:
192+
if (isset($results['@parent_config_name'])) {
193+
$parentConfigName = $results['@parent_config_name'] . '.yaml';
194+
$defaultParent = $useLocalConfig
195+
? $this->pathResolver->getLocalConfigPath($parentConfigName)
196+
: null;
197+
if ($defaultParent === null || !file_exists($defaultParent)) {
198+
$defaultParent = $this->pathResolver->getBaseConfigPath($parentConfigName);
199+
}
200+
if (!file_exists($defaultParent)) {
201+
$defaultParent = null;
202+
error_log('Cannot find parent config: ' . $parentConfigName);
203+
}
204+
// Swallow the directive after processing it:
205+
unset($results['@parent_config_name']);
206+
}
181207
// Check for sections to merge instead of overriding:
182208
$mergedSections = [];
183209
if (isset($results['@merge_sections'])) {
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
"@parent_config_name": "base_parent"
2+
All: base-child
3+
ChildOnly: base-child
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
"@parent_config_name": "local_parent"
2+
All: base-child
3+
ChildOnly: base-child
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
All: Will not exist
2+
ParentOnly: base-parent
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
"@parent_config_name": "base_parent"
2+
All: Will not exist
3+
ChildOnly: Will not exist
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
"@parent_config_name": "local_parent"
2+
All: Will not exist
3+
ChildOnly: Will not exist
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
All: Will not exist
2+
ParentOnly: Will not exist
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
"@parent_config_name": "base_parent"
2+
All: local-child
3+
ChildOnly: local-child

0 commit comments

Comments
 (0)