Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 26 additions & 10 deletions module/VuFind/src/VuFind/Config/ConfigManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ class ConfigManager
{
use MergeRecursiveTrait;

protected array $cache = [];
Comment thread
ThoWagen marked this conversation as resolved.

/**
* Constructor
*
Expand All @@ -70,32 +72,44 @@ public function __construct(
*
* The path consists of a base configuration name and a path to a subsection of that configuration.
*
* @param string $configPath Config path
* @param string $configPath Config path
* @param boolean $forceReload If cache should be ignored
* @param boolean $useLocalConfig Use local configuration if available
*
* @return mixed
*/
public function getConfig(string $configPath): mixed
public function getConfig(string $configPath, bool $forceReload = false, bool $useLocalConfig = true): mixed
{
$cacheKey = ($useLocalConfig ? 'local_' : 'base_') . $configPath;
if (!$forceReload && isset($this->cache[$cacheKey])) {
return $this->cache[$cacheKey];
}
$subsection = explode('/', $configPath);
$configName = array_shift($subsection);
$configLocation = $this->pathResolver->getConfigLocation($configName);
$configLocation = $useLocalConfig
? $this->pathResolver->getConfigLocation($configName)
: $this->pathResolver->getBaseConfigLocation($configName);
if (!$configLocation) {
return [];
}
$configLocation->setSubsection($subsection);
return $this->loadConfigFromLocation($configLocation);
$config = $this->loadConfigFromLocation($configLocation);
$this->cache[$cacheKey] = $config;
return $config;
}

/**
* Get config as array by path.
*
* @param string $configPath Config path
* @param string $configPath Config path
* @param boolean $forceReload If cache should be ignored
* @param boolean $useLocalConfig Use local configuration if available
*
* @return array
*/
public function getConfigArray(string $configPath): array
public function getConfigArray(string $configPath, bool $forceReload = false, bool $useLocalConfig = true): array
{
$config = $this->getConfig($configPath);
$config = $this->getConfig($configPath, $forceReload, $useLocalConfig);
if (!is_array($config)) {
throw new ConfigException('Configuration on path ' . $configPath . ' is not an array.');
}
Expand All @@ -105,13 +119,15 @@ public function getConfigArray(string $configPath): array
/**
* Get config as object by path.
*
* @param string $configPath Config path
* @param string $configPath Config path
* @param boolean $forceReload If cache should be ignored
* @param boolean $useLocalConfig Use local configuration if available
*
* @return Config
*/
public function getConfigObject(string $configPath): Config
public function getConfigObject(string $configPath, bool $forceReload = false, bool $useLocalConfig = true): Config
{
return new Config($this->getConfigArray($configPath));
return new Config($this->getConfigArray($configPath, $forceReload, $useLocalConfig));
}

/**
Expand Down
6 changes: 4 additions & 2 deletions module/VuFind/src/VuFind/Config/PluginFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ public function __invoke(
$requestedName,
?array $options = null
) {
$configManager = $container->get(ConfigManager::class);
return $configManager->getConfigObject($requestedName);
return $container->get(ConfigManager::class)->getConfigObject(
$requestedName,
forceReload: $options['forceReload'] ?? false
Comment thread
demiankatz marked this conversation as resolved.
);
}
}
4 changes: 3 additions & 1 deletion module/VuFind/src/VuFind/Config/PluginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,14 @@ public function validate($plugin)
* @param string $id Service identifier
*
* @return \VuFind\Config\Config
*
* @deprecated Use \VuFind\Config\ConfigManager::getConfig with forceReload=true directly
Comment thread
demiankatz marked this conversation as resolved.
*/
public function reload($id)
{
$oldOverrideSetting = $this->getAllowOverride();
$this->setAllowOverride(true);
$this->setService($id, $this->build($id));
$this->setService($id, $this->build($id, ['forceReload' => true]));
$this->setAllowOverride($oldOverrideSetting);
return $this->get($id);
}
Expand Down
148 changes: 148 additions & 0 deletions module/VuFind/src/VuFindTest/Integration/ConfigTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
<?php

/**
* Abstract base class for config integration test cases.
*
* PHP version 8
*
* Copyright (C) Hebis Verbundzentrale 2025.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* @category VuFind
* @package Tests
* @author Thomas Wagener <wagener@hebis.uni-frankfurt.de>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link https://vufind.org/wiki/development:testing:unit_tests Wiki
*/

namespace VuFindTest\Integration;

use VuFind\Exception\FileAccess;
use VuFind\Feature\DirUtilityTrait;
use VuFindTest\Feature\ConfigRelatedServicesTrait;
use VuFindTest\Feature\FixtureTrait;
use VuFindTest\Feature\LiveDetectionTrait;

/**
* Abstract base class for config integration test cases.
*
* @category VuFind
* @package Tests
* @author Thomas Wagener <wagener@hebis.uni-frankfurt.de>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link https://vufind.org/wiki/development:testing:unit_tests Wiki
*/
abstract class ConfigTestCase extends \PHPUnit\Framework\TestCase
{
use LiveDetectionTrait;
use FixtureTrait;
use DirUtilityTrait;
use ConfigRelatedServicesTrait;

/**
* Path to base configurations
*
* @var string
*/
protected string $baseDirPath;

/**
* Path to local dir configurations
*
* @var string
*/
protected string $localDirPath;

/**
* Setup local dir with fixture.
*
* @param string $fixture Fixture to use
*
* @return void
*/
protected function setUpLocalConfigDir(string $fixture): void
{
$fixtureDir = realpath($this->getFixtureDir() . 'configs/' . $fixture);
$localDirPath = $this->localDirPath;
if (is_dir($localDirPath)) {
self::rmDir($localDirPath);
}
self::cpDir($fixtureDir, $localDirPath);
}

/**
* Read the current config.
*
* @param string $config Config name
* @param ?string $path Optional alternative path to config directory
*
* @return array
*/
protected function readConfig(string $config, ?string $path = null): array
{
$configFile = ($path ?? $this->localDirPath) . '/' . $config . '.ini';
$result = parse_ini_file($configFile, true);
if ($result === false) {
throw new FileAccess('Could not read config file: ' . $configFile);
}
return $result;
}

/**
* Standard setup method.
*
* @return void
*/
public function setUp(): void
{
// Give up if we're not running in CI:
if (!$this->continuousIntegrationRunning()) {
$this->markTestSkipped('Continuous integration not running.');
return;
}

$pathResolver = $this->getPathResolver();
$this->baseDirPath = $pathResolver->getBaseConfigDirPath();
$this->localDirPath = $pathResolver->getLocalConfigDirPath();
if ($this->localDirPath === null) {
$this->markTestSkipped('No local config dir configured.');
}

// create backup of local config dir
if (is_dir($this->localDirPath)) {
$backUpDir = $this->localDirPath . '.bak';
Comment thread
demiankatz marked this conversation as resolved.
Outdated
rename($this->localDirPath, $backUpDir);
mkdir($this->localDirPath);
}
}

/**
* Standard teardown method.
*
* @return void
*/
public function tearDown(): void
{
// restore backup of local config dir
$localDirPath = $this->localDirPath;
Comment thread
demiankatz marked this conversation as resolved.
Outdated
$backUpDir = $localDirPath . '.bak';
if (is_dir($localDirPath)) {
self::rmDir($localDirPath);
}
if (is_dir($backUpDir)) {
rename($backUpDir, $localDirPath);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

/**
* ConfigManager Integration Test Class
*
* PHP version 8
*
* Copyright (C) Hebis Verbundzentrale 2025.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* @category VuFind
* @package Tests
* @author Thomas Wagener <wagener@hebis.uni-frankfurt.de>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link https://vufind.org/wiki/development:testing:unit_tests Wiki
*/

namespace VuFindTest\Config;

use VuFind\Config\ConfigManager;
use VuFindTest\Integration\ConfigTestCase;

/**
* ConfigManager Integration Test Class
*
* @category VuFind
* @package Tests
* @author Thomas Wagener <wagener@hebis.uni-frankfurt.de>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link https://vufind.org/wiki/development:testing:unit_tests Wiki
*/
class ConfigManagerIntegrationTest extends ConfigTestCase
{
/**
* Test caching.
*
* @return void
*/
public function testCaching(): void
{
$container = $this->getContainerWithConfigRelatedServices();
$configManager = $container->get(ConfigManager::class);
Comment thread
demiankatz marked this conversation as resolved.
Outdated
$this->setUpLocalConfigDir('defaultgenerator');
$config = $configManager->getConfig('config');
$this->assertEquals('VuFind 1.0', $config['Site']['generator']);
// check that the cache is used and the change in the config files is ignored
$this->setUpLocalConfigDir('customgenerator');
$config = $configManager->getConfig('config');
$this->assertEquals('VuFind 1.0', $config['Site']['generator']);
// check that the cache is ignored
$config = $configManager->getConfig('config', forceReload: true);
$this->assertEquals('Custom Generator', $config['Site']['generator']);
}

/**
* Test the userLocalConfig parameter.
*
* @return void
*/
public function testUseLocalConfigParameter(): void
{
$container = $this->getContainerWithConfigRelatedServices(
baseDir: $this->getFixtureDir() . 'configs/defaultgenerator',
baseSubDir: ''
);
$this->setUpLocalConfigDir('customgenerator');
$configManager = $container->get(ConfigManager::class);
$config = $configManager->getConfig('config');
$this->assertEquals('Custom Generator', $config['Site']['generator']);
$config = $configManager->getConfig('config', useLocalConfig: false);
$this->assertEquals('VuFind 1.0', $config['Site']['generator']);
}
}
Loading