Skip to content

Commit

Permalink
fix: update configuration file path
Browse files Browse the repository at this point in the history
  • Loading branch information
devuri committed Apr 9, 2024
1 parent 67f38f2 commit 1a89052
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 26 deletions.
4 changes: 4 additions & 0 deletions docs/.vitepress/config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ export default defineConfig({
{
text: 'Configs',
link: '/reference/configuration'
},
{
text: 'Constants',
link: '/reference/app-constants'
},
{
text: 'Install Protection',
Expand Down
2 changes: 1 addition & 1 deletion docs/src/customization/constants.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ For further customization, Raydium offers the `configs/app.php` file for [config

### Using `configs/config.php`

The `config.php` file in the Raydium Framework is a dedicated space for defining additional constants and customizing your WordPress application settings. This flexibility allows you to tailor application behavior, aligning it with specific requirements.
The `config.php` file in the Raydium Framework is a dedicated space for defining additional [application constants](../reference/app-constants) and customizing your WordPress application settings. This flexibility allows you to tailor application behavior, aligning it with specific requirements.

For constants that extend beyond the foundational setups provided by Raydium and the `.env` file, you can use the `configs/config.php` file.

Expand Down
66 changes: 66 additions & 0 deletions docs/src/reference/app-constants.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Constants Config Guide

This guide provides an overview of the framework's constants overrides configuration system, designed to support a range of setups from simple, single-instance applications to complex, multi-tenant platforms. Understanding how the framework selects/overrides constant file and the flexibility built into this process will help you tailor the framework to your specific needs.

## Constants File Selection Process

The framework intelligently selects the most appropriate config file based on the operational context, ensuring optimal settings for every scenario.

### Multi-Tenant and Single-Tenant Modes

- **Multi-Tenant Mode**: In environments hosting multiple tenants, the framework looks for tenant-specific config files within a dedicated directory structure, typically following the pattern: `/path/to/app/configs/<tenant_id>/config.php`.

- **Single-Tenant Mode**: For single-tenant or simpler setups, the framework defaults to a standard config file located at: `/path/to/app/config.php`.

### Fallback Mechanism

If the specified config file is not found, the framework will attempt to use an alternate default file from a secondary configs directory: `/path/to/app/configs/config.php`. This step ensures the application has a configuration to fall back on, maintaining smooth operation.

## Security Considerations

### Handling Sensitive Information

> [!CAUTION]
It's important to handle sensitive information, such as API keys, secrets and other credentials, with utmost caution.

Sensitive information should **never** be hard-coded directly into your constants configuration files. Instead:

- Store sensitive information within the `.env` file, a secure and environment-specific file that is not committed to version control.
- Reference these sensitive values in your application by utilizing the `env()` function. This approach ensures that sensitive details are securely managed and easily configurable across different environments without altering your application's core configuration files.


## Constants File Flexibility

The framework is designed with built-in defaults that allow the application to run smoothly without the need for a custom config file. This design choice emphasizes ease of use and simplicity for straightforward setups.

### Optional Constants Files

- **Ease of Setup**: For many applications, especially those that don't require customization beyond the default settings, no `config.php` file is needed. The framework will operate with its built-in defaults.

- **Conditional Loading**: Config `config.php` files are loaded only if present. This approach ensures that the absence of a config file does not impact the application's functionality.

## Customizing Your Application

While the default settings are suitable for many scenarios, you might need to override these settings as your application grows or your needs become more specific.

### When to Add Constants `config.php` File

Consider adding a config file if you need to:

- **Override Defaults**: Customize SSL settings established by the framework, or other application-specific parameters.

- **Accommodate Multiple Tenants**: In a multi-tenant setup, provide unique config for each tenant without altering the framework's core functionality.

### Getting Started

- **Initial Setup**: Begin with the default settings to get your application up and running quickly. Add config files as your customization needs evolve.

- **Advanced Configuration**: For more complex setups, including multi-tenant environments, organize your config files in the tenant-specific directory structure to maintain clarity and ease of management.

## Example Use Cases

- **Simple Website**: Running a standard website with no special configuration needs? The framework is ready to go as is, no additional config required.

- **Multi-Tenant Platform**: Operating a multi-tenant platform? Create a unique `config.php` file for each tenant within their respective directories to ensure customized experiences.

> This guide aims to provide you with a clear understanding of the framework's flexible and intelligent const configuration system. By following these guidelines, you can tailor the framework to meet your exact requirements, whether you're managing a simple site or a complex, multi-tenant platform.
65 changes: 41 additions & 24 deletions src/Component/Http/AbstractKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -372,37 +372,17 @@ public function get_app_config(): array
}

/**
* Loads tenant-specific or default configuration based on the application's multi-tenant status.
*
* This function first checks for a tenant-specific configuration file in multi-tenant mode. If not found,
* or if not in multi-tenant mode, it falls back to the default configuration file. The configuration is applied
* by requiring the respective file, if it exists.
*
* @return void
* Determines the configuration file to use based on the application's mode and tenant ID.
* Falls back to the default configuration if no tenant-specific configuration is found.
*/
public function overrides(): void
{
$config_override_file = null;

// Check if multi-tenant mode is enabled and a tenant ID is set
if ( $this->is_multitenant_app() && ! empty( $this->tenant_id ) ) {
$tenant_config_file = $this->app_path . "/{$this->configs_dir}/{$this->tenant_id}/{$this->config_file}.php";

// Check if the tenant-specific config file exists
if ( file_exists( $tenant_config_file ) ) {
$config_override_file = $tenant_config_file;
}
}
$config_override_file = $this->get_tenant_config_file();

// If no tenant-specific file found, use the default config file
if ( empty( $config_override_file ) ) {
$default_config_file = $this->app_path . "/{$this->config_file}.php";
if ( file_exists( $default_config_file ) ) {
$config_override_file = $default_config_file;
}
$config_override_file = $this->get_default_config_file();
}

// If a valid config override file is found, require it
if ( ! empty( $config_override_file ) ) {
require_once $config_override_file;
}
Expand Down Expand Up @@ -532,6 +512,43 @@ public function get_user_constants(): ?array
return self::encrypt_secret( $user_constants, self::env_secrets() );
}

/**
* Attempts to get the tenant-specific configuration file if multi-tenant mode is active.
*
* @return null|string Path to the tenant-specific configuration file or null if not found/applicable.
*/
protected function get_tenant_config_file(): ?string
{
if ( $this->is_multitenant_app() && ! empty( $this->tenant_id ) ) {
$tenant_config_file = "{$this->app_path}/{$this->configs_dir}/{$this->tenant_id}/{$this->config_file}.php";
if ( file_exists( $tenant_config_file ) ) {
return $tenant_config_file;
}
}

return null;
}

/**
* Gets the default configuration file, preferring the one in the configs directory.
*
* @return string Path to the default configuration file.
*/
protected function get_default_config_file(): ?string
{
$default_config_file = "{$this->app_path}/{$this->config_file}.php";
$configs_config_file = "{$this->app_path}/{$this->configs_dir}/{$this->config_file}.php";

if ( file_exists( $configs_config_file ) ) {
return $configs_config_file;
}
if ( file_exists( $default_config_file ) ) {
return $default_config_file;
}

return null;
}

/**
* Checks for maintenance mode across different scopes and terminates execution if enabled.
*
Expand Down
2 changes: 1 addition & 1 deletion src/inc/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ function env( $name, $default = null, $encrypt = false, $strtolower = false )
// Instance of the Env class with your predefined settings
static $env = null;
if (null === $env) {
$env = new Env($whitelisted, $encryptionPath, false );
$env = new Env( $whitelisted, $encryptionPath, false );
}

// Get the environment variable value
Expand Down

0 comments on commit 1a89052

Please sign in to comment.