Skip to content
Open
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
67 changes: 67 additions & 0 deletions Sources/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -2914,6 +2914,73 @@ public static function checkCron(): void
}
}

/**
* Get the SettingsService instance from the container.
*
* This method provides access to the SettingsService for code that wants to use
* dependency injection instead of static methods for Settings.php operations.
*
* @return Services\SettingsService The settings service instance.
*/
public static function getSettingsService(): Services\SettingsService
{
static $service = null;

// Return cached instance if available
if ($service !== null) {
return $service;
}

// Try to get the service from the container
try {
$service = Infrastructure\Container::get(Services\SettingsService::class);

return $service;
} catch (\Throwable $e) {
// Container not available or service not registered
// Fall through to manual instantiation
}

// Fallback: create instance directly
// This ensures config works even during early bootstrap
$service = new Services\SettingsService();

return $service;
}

/**
* Get the ModSettingsService instance from the container.
*
* This method provides access to the ModSettingsService for code that wants to use
* dependency injection instead of static methods for database settings operations.
*
* @return Services\ModSettingsService The mod settings service instance.
*/
public static function getModSettingsService(): Services\ModSettingsService
{
static $service = null;

// Return cached instance if available
if ($service !== null) {
return $service;
}

// Try to get the service from the container
try {
$service = Infrastructure\Container::get(Services\ModSettingsService::class);

return $service;
} catch (\Throwable $e) {
// Container not available or service not registered
// Fall through to manual instantiation
}

// Fallback: create instance directly
$service = new Services\ModSettingsService();

return $service;
}

/*************************
* Internal static methods
*************************/
Expand Down
11 changes: 11 additions & 0 deletions Sources/Infrastructure/ServicesList.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
<?php

use SMF\Services\ErrorHandlerService;
use SMF\Services\ModSettingsService;
use SMF\Services\SettingsService;

// List of all services registered for SMF, example:
//'db' => [
// 'arguments' => [$db_server, $db_user],
// 'shared' => true // false will create a new instance everytime
//],
return [
// Settings.php configuration service
SettingsService::class => [
'shared' => true,
],
// Database settings service
ModSettingsService::class => [
'shared' => true,
],
// Error handler service
ErrorHandlerService::class => [
'shared' => true,
],
Expand Down
121 changes: 121 additions & 0 deletions Sources/Services/Contracts/ModSettingsServiceInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?php

/**
* Simple Machines Forum (SMF)
*
* @package SMF
* @author Simple Machines https://www.simplemachines.org
* @copyright 2026 Simple Machines and individual contributors
* @license https://www.simplemachines.org/about/smf/license.php BSD
*
* @version 3.0 Alpha 4
*/

declare(strict_types=1);

namespace SMF\Services\Contracts;

/**
* Interface for mod settings management service.
*
* This service handles database-based settings stored in the settings table.
* These are runtime, user-configurable settings that can be modified through
* the admin panel and by mods/extensions.
*/
interface ModSettingsServiceInterface
{
/****************
* Public methods
****************/

/**
* Get a mod setting value.
*
* @param string $key The setting key
* @param mixed $default Default value if key doesn't exist
* @return mixed The setting value
*/
public function get(string $key, mixed $default = null): mixed;

/**
* Get all mod settings.
*
* @return array All mod settings as key => value pairs
*/
public function getAll(): array;

/**
* Check if a mod setting exists.
*
* @param string $key The setting key
* @return bool True if the setting exists
*/
public function has(string $key): bool;

/**
* Set a mod setting value (in memory only).
*
* This does not persist to the database. Use update() to persist.
*
* @param string $key The setting key
* @param mixed $value The value to set
*/
public function set(string $key, mixed $value): void;

/**
* Update mod settings in the database.
*
* @param array $settings Array of setting key => value pairs
* Set value to null to delete a setting
* @param bool $update Whether to use UPDATE instead of REPLACE
* True: Use UPDATE (allows incrementing with true/false values)
* False: Use REPLACE (default, faster for bulk updates)
*/
public function update(array $settings, bool $update = false): void;

/**
* Delete one or more mod settings from the database.
*
* @param string|array $keys Setting key(s) to delete
*/
public function delete(string|array $keys): void;

/**
* Reload mod settings from the database.
*
* This clears the cache and reloads all settings from the database.
*
*/
public function reload(): void;

/**
* Clear the mod settings cache.
*
*/
public function clearCache(): void;

/**
* Get multiple settings at once.
*
* @param array $keys Array of setting keys
* @param mixed $default Default value for missing keys
* @return array Array of key => value pairs
*/
public function getMultiple(array $keys, mixed $default = null): array;

/**
* Check if any of the specified settings exist.
*
* @param array $keys Array of setting keys
* @return bool True if at least one setting exists
*/
public function hasAny(array $keys): bool;

/**
* Check if all the specified settings exist.
*
* @param array $keys Array of setting keys
* @return bool True if all settings exist
*/
public function hasAll(array $keys): bool;
}
150 changes: 150 additions & 0 deletions Sources/Services/Contracts/SettingsServiceInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
<?php

/**
* Simple Machines Forum (SMF)
*
* @package SMF
* @author Simple Machines https://www.simplemachines.org
* @copyright 2026 Simple Machines and individual contributors
* @license https://www.simplemachines.org/about/smf/license.php BSD
*
* @version 3.0 Alpha 4
*/

declare(strict_types=1);

namespace SMF\Services\Contracts;

/**
* Interface for Settings.php configuration management service.
*
* This service handles file-based configuration stored in Settings.php.
* These are system-level settings like database credentials, directory paths,
* and core configuration that rarely changes.
*/
interface SettingsServiceInterface
{
/****************
* Public methods
****************/

/**
* Get a configuration value from Settings.php.
*
* @param string $key The configuration key
* @param mixed $default Default value if key doesn't exist
* @return mixed The configuration value
*/
public function get(string $key, mixed $default = null): mixed;

/**
* Set a configuration value (in memory only).
*
* This does not persist to Settings.php. Use updateFile() to persist.
*
* @param string $key The configuration key
* @param mixed $value The value to set
*/
public function set(string $key, mixed $value): void;

/**
* Update the Settings.php file.
*
* @param array $configVars Array of configuration variables to update
* @param bool $keepQuotes Whether to keep quotes around values
* @param bool $rebuild Whether to rebuild the file
* @return bool Success status
*/
public function updateFile(array $configVars, bool $keepQuotes = false, bool $rebuild = false): bool;

/**
* Get the board URL.
*
* @return string The board URL
*/
public function getBoardUrl(): string;

/**
* Get the script URL.
*
* @return string The script URL
*/
public function getScriptUrl(): string;

/**
* Get the board directory.
*
* @return string The board directory path
*/
public function getBoardDir(): string;

/**
* Get the sources directory.
*
* @return string The sources directory path
*/
public function getSourcesDir(): string;

/**
* Get the cache directory.
*
* @return string The cache directory path
*/
public function getCacheDir(): string;

/**
* Get the languages directory.
*
* @return string The languages directory path
*/
public function getLanguagesDir(): string;

/**
* Check if maintenance mode is enabled.
*
* @return bool True if maintenance mode is enabled
*/
public function isMaintenanceMode(): bool;

/**
* Get the maintenance mode level.
*
* @return int Maintenance mode level (0, 1, or 2)
*/
public function getMaintenanceLevel(): int;

/**
* Get the forum name.
*
* @return string The forum name
*/
public function getForumName(): string;

/**
* Get the database type.
*
* @return string The database type (mysql, postgresql, etc.)
*/
public function getDatabaseType(): string;

/**
* Get the database server.
*
* @return string The database server
*/
public function getDatabaseServer(): string;

/**
* Get the database name.
*
* @return string The database name
*/
public function getDatabaseName(): string;

/**
* Get the database prefix.
*
* @return string The database table prefix
*/
public function getDatabasePrefix(): string;
}
8 changes: 8 additions & 0 deletions Sources/Services/Contracts/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

// Try to handle it with the upper level index.php. (it should know what to do.)
if (file_exists(dirname(__DIR__) . DIRECTORY_SEPARATOR . 'index.php')) {
include dirname(__DIR__) . DIRECTORY_SEPARATOR . 'index.php';
} else {
exit;
}
Loading
Loading