Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
41 changes: 31 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,13 @@ class ConfigManager
{
use MergeRecursiveTrait;

/**
* Configuration cache
*
* @var array[]
*/
protected array $cache = [];
Comment thread
ThoWagen marked this conversation as resolved.

/**
* Constructor
*
Expand All @@ -70,32 +77,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 +124,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
10 changes: 5 additions & 5 deletions module/VuFind/src/VuFind/Config/PluginFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,18 @@ public function canCreate(ContainerInterface $container, $requestedName)
*
* @param ContainerInterface $container Service container
* @param string $requestedName Name of service
* @param array $options Options (unused)
* @param array $options Options
*
* @return object
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function __invoke(
ContainerInterface $container,
$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.
);
}
}
8 changes: 7 additions & 1 deletion module/VuFind/src/VuFind/Config/PluginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ public function __construct(
array $v3config = []
) {
$this->addAbstractFactory(PluginFactory::class);
// Disable caching in the plugin manager. This is handled by the \VuFind\Config\ConfigManager.
if (!isset($v3config['shared_by_default'])) {
$v3config['shared_by_default'] = false;
}
parent::__construct($configOrContainerInstance, $v3config);
}

Expand All @@ -82,12 +86,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
164 changes: 164 additions & 0 deletions module/VuFind/src/VuFindTest/Integration/ConfigTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
<?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;

/**
* Path to local dir backup
*
* @var string
*/
protected string $localDirBackupPath;

/**
* Set local dir path.
*
* @param string $localDirPath Local dir path
*
* @return void
*/
protected function setLocalDirPath(string $localDirPath): void
{
$this->localDirPath = $localDirPath;
$this->localDirBackupPath = $localDirPath . '.bak';
}

/**
* 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);
if (is_dir($this->localDirPath)) {
self::rmDir($this->localDirPath);
}
self::cpDir($fixtureDir, $this->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->setLocalDirPath($pathResolver->getLocalConfigDirPath());
if ($this->localDirPath === null) {
$this->markTestSkipped('No local config dir configured.');
}

// create backup of local config dir
if (is_dir($this->localDirPath)) {
rename($this->localDirPath, $this->localDirBackupPath);
mkdir($this->localDirPath);
}
}

/**
* Standard teardown method.
*
* @return void
*/
public function tearDown(): void
{
// restore backup of local config dir
if (is_dir($this->localDirPath)) {
self::rmDir($this->localDirPath);
}
if (is_dir($this->localDirBackupPath)) {
rename($this->localDirBackupPath, $this->localDirPath);
}
}
}
Loading