Skip to content

Configuration Refactor & .env Integration#653

Merged
JohnVillalovos merged 4 commits intoLibreBooking:developfrom
lucs7:refactor-config
Aug 5, 2025
Merged

Configuration Refactor & .env Integration#653
JohnVillalovos merged 4 commits intoLibreBooking:developfrom
lucs7:refactor-config

Conversation

@lucs7
Copy link
Collaborator

@lucs7 lucs7 commented Jul 3, 2025

config.*.php

  • Refactors the config.php structure to support a modern return-based PHP array style
  • Improves error reporting setup based on LB_APP_DEBUG

configuration.php

  • Adds support for environment variables via .env using phpdotenv
  • Introduces a new env() helper function that mimics Laravel-style environment resolution
  • Maintains backwards compatibility with the legacy $conf['settings'] format
  • Updates Configurator class to output clean, pretty-formatted config files
  • Creates timestamped backups (*.bck.php) of overwritten config files
  • Adds a comment with timestamp to written config files
  • Adds alert message to configuration page when .env file is found

ConfigKeys.php

  • updated format to fit categories
  • added legacy support and warning message

.env

  • all settings are available as 'LB_' environment variables through environment on start or .env
  • Configuration::Register now accepts an additional .env file (mostly important for testing as default .env should be in root folder)
  • env vars are taken from environment or .env file and overwrite config.php settings
  • .env implementation is mutable so later .envs can overwrite them (set environment vars e.g. docker will be overwritten by .env)

plugins

  • updated Configuration::Register
  • updated *.config.dist.php files

@JohnVillalovos
Copy link
Collaborator

@lucs7 Thanks.

One question. What do you think about having all of the environment variables prefixed with LB_ ? That way it is easy to know they are for LibreBooking and also less likely to have a conflict with some other program.

I'll work on finding some time over the weekend to review the whole PR.

@lucs7
Copy link
Collaborator Author

lucs7 commented Jul 4, 2025

Added the prefix 'LB_' similar to how the docker imgage works

@lucs7 lucs7 force-pushed the refactor-config branch from 023d972 to 871ddb5 Compare July 4, 2025 11:01
@JohnVillalovos JohnVillalovos requested a review from Copilot July 4, 2025 18:04

This comment was marked as outdated.

@lucs7 lucs7 force-pushed the refactor-config branch from 3f7621c to 2ed28fc Compare July 12, 2025 21:26
@lucs7 lucs7 closed this Jul 12, 2025
@lucs7 lucs7 reopened this Jul 12, 2025
@JohnVillalovos JohnVillalovos requested a review from Copilot July 13, 2025 15:53

This comment was marked as outdated.

@lucs7 lucs7 marked this pull request as draft July 15, 2025 07:51
@lucs7 lucs7 force-pushed the refactor-config branch 2 times, most recently from 98a5f09 to fd0ff77 Compare July 15, 2025 12:52
@lucs7 lucs7 requested a review from Copilot July 15, 2025 12:54

This comment was marked as outdated.

@lucs7 lucs7 force-pushed the refactor-config branch 4 times, most recently from b3a3e32 to 3e78076 Compare July 18, 2025 19:48
@JohnVillalovos JohnVillalovos requested a review from Copilot July 18, 2025 20:23

This comment was marked as outdated.

@lucs7 lucs7 force-pushed the refactor-config branch 2 times, most recently from 8912b58 to d8d8443 Compare August 2, 2025 09:50
@lucs7 lucs7 requested a review from Copilot August 2, 2025 09:53

This comment was marked as outdated.

@lucs7 lucs7 force-pushed the refactor-config branch 2 times, most recently from b9e9499 to 92a08d1 Compare August 2, 2025 12:53
@lucs7
Copy link
Collaborator Author

lucs7 commented Aug 2, 2025

@JohnVillalovos
Sorry for the large change in the codebase. I tried to to divide it but it is hard split the changes in the function calls from the change in the logic. Fortunately more than half the files are just single line changes from GetSectionKey to GetKey

@lucs7 lucs7 marked this pull request as ready for review August 2, 2025 12:56
@lucs7 lucs7 force-pushed the refactor-config branch 2 times, most recently from b7efc00 to 195423b Compare August 5, 2025 10:10
Copy link
Collaborator

@JohnVillalovos JohnVillalovos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @lucs7

I did my best to review this but it was very large. In the future we should try to have smaller PRs and smaller commits.

But I will approve this.

lucs7 added 4 commits August 5, 2025 15:03
1. Support for .env file loading via phpdotenv
    - Added EnvHelper to load environment variables
    - Example .env file provided in .env.example
    - Configurator now loads environment variables from .env

2. Major restructure of config to nested array for main config and plugin configs
    - Renamed configs keys to be more consistent
    - Support of legacy structure and naming through ConfigKeys::$legacyKeyMap
    - Scheme and validation through ConfigKeys
    - Updated Configurator to use ConfigKeys for templating

Example ConfigKeys
public const APP_TITLE = [
    'key' => 'app.title',
    'legacy' => 'old.key',
    'type' => 'string',
    'default' => 'LibreBooking',
    'label' => 'App title',
    'description' => '...',
    'choices' => ['option 1' => 'Option 1', 'option 2' => 'Option 2'],
];
keys are now called by their Config key without section through getKey.
all section magic is handled automatically
ConfigTest.php and FakeConfig.php previously had methodnotfound errors
that have been fixed
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR refactors LibreBooking's configuration system to modernize PHP config files and integrate environment variable support using .env files. The changes enable better deployment practices and maintain backward compatibility.

  • Modernizes config files from legacy $conf array format to return-based PHP arrays
  • Introduces .env file support for environment-based configuration overrides
  • Updates plugin configuration system with new ConfigKeys classes for better type safety

Reviewed Changes

Copilot reviewed 267 out of 268 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
tpl/Admin/Configuration/manage_configuration.tpl Updates admin configuration UI with dynamic field rendering and environment variable awareness
tests/fakes/FakeConfig.php Refactors test configuration to support new config definition format
tests/data/* Adds test data files for new config formats and environment variable testing
tests/WebServices/* Updates test files to use new ConfigKeys constants
tests/Presenters/* Updates presenter tests to use new configuration format
tests/Infrastructure/Config/ConfigTest.php Adds comprehensive tests for new configuration system
tests/Domain/* Updates domain tests for new config format
tests/Application/* Updates application tests for new config format
plugins/Authentication/* Refactors all authentication plugins to use new ConfigKeys classes
plugins/PreReservation/* Updates pre-reservation plugins for new config format
Comments suppressed due to low confidence (3)

tpl/Admin/Configuration/manage_configuration.tpl:37

  • [nitpick] The property name 'hasEnv' doesn't follow consistent naming conventions. Consider renaming to 'HasEnv' to match the other property naming pattern used in the template.
                {assign var="disabled" value=$setting->hasEnv ? 'disabled' : ''}

tests/fakes/FakeConfig.php:65

  • The error message 'Config definition not found' is misleading. It should clarify that the config definition must be an array with a 'key' field, not that it wasn't found.
        if (!is_array($configDef) || !isset($configDef['key'])) {

tests/Infrastructure/Config/ConfigTest.php:97

  • The test should clean up environment variables after the test runs to avoid affecting other tests. Consider using tearDown() or try-finally to ensure putenv() changes are reverted.
        putenv('DEFAULT_TIMEZONE=Europe/Berlin');

Comment on lines +39 to +41
private function GetConfig($configDef, $converter = null)
{
return Configuration::Instance()->File(self::CONFIG_ID)->GetKey($keyName, $converter);
return Configuration::Instance()->File(WordPressConfigKeys::CONFIG_ID)->GetKey($configDef, $converter);
Copy link

Copilot AI Aug 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The method signature change from GetConfig($keyName) to GetConfig($configDef) is inconsistent across plugins. Some plugins still use $keyName parameter name while others use $configDef. This could confuse developers and should be standardized.

Copilot uses AI. Check for mistakes.
return true;
};
}
;
Copy link

Copilot AI Aug 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary semicolon after closing brace. This is likely a leftover from refactoring and should be removed for cleaner code.

Suggested change
;

Copilot uses AI. Check for mistakes.
$pair = explode('=', trim($attributePair));
$attributes[trim($pair[0])] = trim($pair[1]);
if (count($pair) === 2) {
$attributes[trim($pair[0])] = trim($pair[1]);
Copy link

Copilot AI Aug 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code assumes explode will always return an array with at least 2 elements when checking count($pair) === 2, but the closing brace and return statement are outside this if block. This could lead to incomplete attribute mapping if the format is incorrect.

Suggested change
$attributes[trim($pair[0])] = trim($pair[1]);
$attributes[trim($pair[0])] = trim($pair[1]);
} else {
error_log("Invalid attribute mapping pair: '{$attributePair}' in CAS attribute mapping configuration.");

Copilot uses AI. Check for mistakes.
@JohnVillalovos
Copy link
Collaborator

@lucs7 I rebased this and fixed the conflicts.

@JohnVillalovos JohnVillalovos enabled auto-merge (rebase) August 5, 2025 22:06
@JohnVillalovos JohnVillalovos disabled auto-merge August 5, 2025 22:07
@JohnVillalovos JohnVillalovos merged commit 03dce68 into LibreBooking:develop Aug 5, 2025
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

Comments