diff --git a/docs/code/classes/Urisoft-App-Setup.html b/docs/code/classes/Urisoft-App-Setup.html
index 4bef24f..c934b70 100644
--- a/docs/code/classes/Urisoft-App-Setup.html
+++ b/docs/code/classes/Urisoft-App-Setup.html
@@ -243,9 +243,9 @@
config()
- : static
+ : self
-Runs config setup with default setting.
+Configures application settings.
database()
@@ -387,6 +387,20 @@
Env defaults.
+
+ determine_environment()
+
+ : mixed
+
+Determines the appropriate environment setting.
+
+
+ determine_path()
+
+ : string
+
+Determines the application path, accounting for multi-tenancy.
+
enable_error_handler()
@@ -401,6 +415,13 @@
Switches between different environments based on the value of $this->environment.
+
+ filter_existing_env_files()
+
+ : void
+
+Filters out environment files that do not exist to avoid warnings.
+
get_default_file_names()
@@ -408,6 +429,13 @@
Retrieves the default file names for environment configuration.
+
+ initialize_dotenv()
+
+ : void
+
+Initializes Dotenv with the set path and environment files.
+
is_multitenant_app()
@@ -422,6 +450,13 @@
+
+ normalize_environment()
+
+ : array<string|int, mixed>
+
+Normalizes the environment configuration.
+
set_debug_log()
@@ -808,7 +843,7 @@
@@ -816,9 +851,12 @@
public
- __construct(string $path[, array<string|int, mixed> $supported_names = [] ][, bool $short_circuit = true ]) : mixed
+ __construct(string $path[, array<string|int, mixed> $env_file_names = [] ][, bool $short_circuit = true ]) : mixed
+
+ Sets up the application path, initializes environment configuration loading with Dotenv,
+and handles multi-tenancy. It also sets up default environment types and constants.
+
-
Parameters
-
@@ -826,16 +864,16 @@
Parameters
: string
-
-
Current directory.
+ The base directory path for the application.
-
- $supported_names
+ $env_file_names
: array<string|int, mixed>
= []
-
-
An array of supported environment names and configuration.
+ Optional. Additional environment file names to support.
@@ -844,7 +882,7 @@ Parameters
: bool
= true
- Flag to control short-circuiting file loading.
+ Optional. Whether to stop loading files after the first found. Defaults to true.
@@ -937,34 +975,34 @@
Runs config setup with default setting.
+ Configures application settings.
public
- config([null|array<string|int, string> $environment = null ][, bool $setup = true ]) : static
+ config([null|array<string|int, mixed>|string $environment = null ][, null|bool $setup = true ]) : self
Parameters
-
$environment
- : null|array<string|int, string>
+ : null|array<string|int, mixed>|string
= null
-
-
.
+ Configuration settings or environment name.
-
$setup
- : bool
+ : null|bool
= true
-
-
.
+ Controls the setup process. If null, setup is bypassed.
@@ -973,7 +1011,7 @@ Parameters
Return values
- static
+ self
—
@@ -1024,7 +1062,7 @@
@@ -1265,7 +1303,7 @@
@@ -1313,7 +1351,7 @@
@@ -1346,7 +1384,7 @@
@@ -1394,7 +1432,7 @@
@@ -1503,7 +1541,7 @@
@@ -1585,7 +1623,7 @@
@@ -1618,7 +1656,7 @@
@@ -1730,6 +1768,100 @@ Return values
—
+
+
+
+ determine_environment()
+
+
+
+
+ Determines the appropriate environment setting.
+
+
+ protected
+ determine_environment(mixed $environment) : mixed
+
+
+ Parameters
+
+ -
+ $environment
+ : mixed
+
+ -
+
The environment setting from the configuration.
+
+
+
+
+
+
+
+ Return values
+ mixed
+ —
+ The determined environment value.
+
+
+
+
+
+
+ determine_path()
+
+
+
+
+ Determines the application path, accounting for multi-tenancy.
+
+
+ protected
+ determine_path(string $base_path) : string
+
+
+ Parameters
+
+ -
+ $base_path
+ : string
+
+ -
+
The base application directory path.
+
+
+
+
+
+
+
+ Return values
+ string
+ —
+ The determined application path.
+
+
+
@@ -1777,7 +1909,7 @@
@@ -1791,6 +1923,39 @@
+ Return values
+ void
+ —
+
+
+
+
+
+ filter_existing_env_files()
+
+
+
+
+ Filters out environment files that do not exist to avoid warnings.
+
+
+ protected
+ filter_existing_env_files() : void
+
+
+
+
+
Return values
void
—
@@ -1810,7 +1975,7 @@
@@ -1863,6 +2028,41 @@ Return values
+
+
+
+ initialize_dotenv()
+
+
+
+
+ Initializes Dotenv with the set path and environment files.
+
+
+ protected
+ initialize_dotenv() : void
+
+ Handles exceptions by using thewp_terminate
function to exit.
+
+
+
+
+
+ Return values
+ void
+ —
+
+
@@ -1910,7 +2110,7 @@
@@ -1929,6 +2129,53 @@ Return values
—
+
+
+
+ normalize_environment()
+
+
+
+
+ Normalizes the environment configuration.
+
+
+ protected
+ normalize_environment(mixed $environment) : array<string|int, mixed>
+
+
+ Parameters
+
+ -
+ $environment
+ : mixed
+
+ -
+
The provided environment configuration.
+
+
+
+
+
+
+
+ Return values
+ array<string|int, mixed>
+ —
+ The normalized configuration array.
+
+
+
@@ -2021,7 +2268,7 @@
diff --git a/docs/code/js/searchIndex.js b/docs/code/js/searchIndex.js
index e43cdfd..28743f5 100644
--- a/docs/code/js/searchIndex.js
+++ b/docs/code/js/searchIndex.js
@@ -553,7 +553,7 @@ Search.appendIndex(
}, {
"fqsen": "\\Urisoft\\App\\Setup\u003A\u003Aconfig\u0028\u0029",
"name": "config",
- "summary": "Runs\u0020config\u0020setup\u0020with\u0020default\u0020setting.",
+ "summary": "Configures\u0020application\u0020settings.",
"url": "classes/Urisoft-App-Setup.html#method_config"
}, {
"fqsen": "\\Urisoft\\App\\Setup\u003A\u003Aset_environment\u0028\u0029",
@@ -590,6 +590,31 @@ Search.appendIndex(
"name": "required",
"summary": "Ensure\u0020that\u0020a\u0020specific\u0020constant\u0020is\u0020defined\u0020and\u0020not\u0020empty.",
"url": "classes/Urisoft-App-Setup.html#method_required"
+ }, {
+ "fqsen": "\\Urisoft\\App\\Setup\u003A\u003Anormalize_environment\u0028\u0029",
+ "name": "normalize_environment",
+ "summary": "Normalizes\u0020the\u0020environment\u0020configuration.",
+ "url": "classes/Urisoft-App-Setup.html#method_normalize_environment"
+ }, {
+ "fqsen": "\\Urisoft\\App\\Setup\u003A\u003Adetermine_environment\u0028\u0029",
+ "name": "determine_environment",
+ "summary": "Determines\u0020the\u0020appropriate\u0020environment\u0020setting.",
+ "url": "classes/Urisoft-App-Setup.html#method_determine_environment"
+ }, {
+ "fqsen": "\\Urisoft\\App\\Setup\u003A\u003Adetermine_path\u0028\u0029",
+ "name": "determine_path",
+ "summary": "Determines\u0020the\u0020application\u0020path,\u0020accounting\u0020for\u0020multi\u002Dtenancy.",
+ "url": "classes/Urisoft-App-Setup.html#method_determine_path"
+ }, {
+ "fqsen": "\\Urisoft\\App\\Setup\u003A\u003Afilter_existing_env_files\u0028\u0029",
+ "name": "filter_existing_env_files",
+ "summary": "Filters\u0020out\u0020environment\u0020files\u0020that\u0020do\u0020not\u0020exist\u0020to\u0020avoid\u0020warnings.",
+ "url": "classes/Urisoft-App-Setup.html#method_filter_existing_env_files"
+ }, {
+ "fqsen": "\\Urisoft\\App\\Setup\u003A\u003Ainitialize_dotenv\u0028\u0029",
+ "name": "initialize_dotenv",
+ "summary": "Initializes\u0020Dotenv\u0020with\u0020the\u0020set\u0020path\u0020and\u0020environment\u0020files.",
+ "url": "classes/Urisoft-App-Setup.html#method_initialize_dotenv"
}, {
"fqsen": "\\Urisoft\\App\\Setup\u003A\u003Aget_default_file_names\u0028\u0029",
"name": "get_default_file_names",
diff --git a/src/App/Setup.php b/src/App/Setup.php
index b3ed71a..ce29e4a 100644
--- a/src/App/Setup.php
+++ b/src/App/Setup.php
@@ -87,61 +87,22 @@ class Setup implements ConfigInterface
/**
* Constructor for initializing the application environment and configuration.
*
- * @param string $path Current directory.
- * @param array $supported_names An array of supported environment names and configuration.
- * @param bool $short_circuit Flag to control short-circuiting file loading.
+ * Sets up the application path, initializes environment configuration loading with Dotenv,
+ * and handles multi-tenancy. It also sets up default environment types and constants.
+ *
+ * @param string $path The base directory path for the application.
+ * @param array $env_file_names Optional. Additional environment file names to support.
+ * @param bool $short_circuit Optional. Whether to stop loading files after the first found. Defaults to true.
*/
- public function __construct( string $path, array $supported_names = [], bool $short_circuit = true )
+ public function __construct( string $path, array $env_file_names = [], bool $short_circuit = true )
{
- $this->path = $path;
-
- // Check if running multi-tenant mode
- $this->is_multi_tenant = $this->is_multitenant_app();
-
- /*
- * By default, we'll stop looking for files as soon as we find one.
- *
- * To disable this behaviour, and load all files in order,
- * we can disable the file loading with a new parameter.
- *
- * @link https://github.com/vlucas/phpdotenv/pull/394
- */
+ $this->path = $this->determine_path( $path );
$this->short_circuit = $short_circuit;
+ $this->env_files = array_merge( $this->get_default_file_names(), $env_file_names );
- /*
- * Available env type settings.
- *
- * If we cant find a supported env type we will set to production.
- */
+ $this->filter_existing_env_files();
$this->env_types = EnvTypes::get();
-
- // use multiple filenames.
- if ( $this->is_multi_tenant && \defined( 'APP_TENANT_ID' ) ) {
- $tenant_id = APP_TENANT_ID;
- // Start Dotenv bootstrap the web application.
- $this->dotenv = Dotenv::createImmutable( $this->path, "site/{$tenant_id}/.env" );
- } else {
- $tenant_id = null;
-
- $this->env_files = array_merge( $this->get_default_file_names(), $supported_names );
-
- // Verify files to avoid Dotenv warning.
- foreach ( $this->env_files as $key => $file ) {
- if ( ! file_exists( $this->path . '/' . $file ) ) {
- unset( $this->env_files[ $key ] );
- }
- }
-
- // Start Dotenv bootstrap the web application.
- $this->dotenv = Dotenv::createImmutable( $this->path, $this->env_files, $short_circuit );
- }// end if
-
- try {
- $this->dotenv->load();
- } catch ( Exception $e ) {
- wp_terminate( $e->getMessage() );
- exit;
- }
+ $this->initialize_dotenv();
$this->set_constant_map();
}
@@ -161,50 +122,18 @@ public static function init( string $path ): self
}
/**
- * Runs config setup with default setting.
+ * Configures application settings.
*
- * @param null|string[] $environment .
- * @param bool $setup .
+ * @param null|array|string $environment Configuration settings or environment name.
+ * @param null|bool $setup Controls the setup process. If null, setup is bypassed.
*
- * @return static
+ * @return self
*/
- public function config( $environment = null, bool $setup = true ): ConfigInterface
+ public function config( $environment = null, ?bool $setup = true ): ConfigInterface
{
// check required vars.
$this->is_required();
- // self::init( __DIR__ )->config('production')
- if ( ! \is_array( $environment ) ) {
- $environment = [ 'environment' => $environment ];
- }
-
- // default setup.
- $environment = array_merge(
- [
- 'environment' => null,
- 'error_log' => null,
- 'debug' => false,
- // set error handler framework 'symfony' or 'oops'
- 'errors' => false,
- ],
- $environment
- );
-
- // set error logs dir.
- $this->error_log_dir = $environment['error_log'] ?? false;
-
- // symfony error handler.
- $this->error_handler = $environment['errors'];
-
- // environment.
- if ( \is_bool( $environment['environment'] ) ) {
- $this->environment = $environment['environment'];
- } elseif ( \is_string( $environment['environment'] ) ) {
- $this->environment = trim( (string) $environment['environment'] );
- } else {
- $this->environment = $environment['environment'];
- }
-
// set $setup to null allows us to short-circuit and bypass setup for more granular control.
// Setup::init(__DIR__)->config( 'development', false )->set_environment()>database()->salts()->apply();
if ( \is_null( $setup ) ) {
@@ -213,6 +142,12 @@ public function config( $environment = null, bool $setup = true ): ConfigInterfa
return $this;
}
+ // self::init( __DIR__ )->config('production')
+ $environment = $this->normalize_environment( $environment );
+ $this->error_log_dir = $environment['error_log'] ?? false;
+ $this->error_handler = $environment['errors'] ?? null;
+ $this->environment = $this->determine_environment( $environment['environment'] );
+
// $setup = false allows for bypass of default setup.
if ( false === $setup ) {
$this->set_environment()
@@ -225,7 +160,7 @@ public function config( $environment = null, bool $setup = true ): ConfigInterfa
}
// do default setup.
- if ( $setup ) {
+ if ( true === $setup ) {
$this->set_environment()
->debug( $this->error_log_dir )
->set_error_handler()
@@ -381,6 +316,91 @@ public function required( string $name ): void
}
}
+ /**
+ * Normalizes the environment configuration.
+ *
+ * @param mixed $environment The provided environment configuration.
+ *
+ * @return array The normalized configuration array.
+ */
+ protected function normalize_environment( $environment ): array
+ {
+ if ( ! \is_array( $environment ) ) {
+ $environment = [ 'environment' => $environment ];
+ }
+
+ return array_merge(
+ [
+ 'environment' => null,
+ 'error_log' => null,
+ 'debug' => false,
+ 'errors' => false,
+ ],
+ $environment
+ );
+ }
+
+ /**
+ * Determines the appropriate environment setting.
+ *
+ * @param mixed $environment The environment setting from the configuration.
+ *
+ * @return mixed The determined environment value.
+ */
+ protected function determine_environment( $environment )
+ {
+ if ( \is_bool( $environment ) || \is_string( $environment ) ) {
+ return $environment;
+ }
+
+ return trim( (string) $environment );
+
+ return $environment;
+ }
+
+ /**
+ * Determines the application path, accounting for multi-tenancy.
+ *
+ * @param string $base_path The base application directory path.
+ *
+ * @return string The determined application path.
+ */
+ protected function determine_path( $base_path ): string
+ {
+ if ( $this->is_multitenant_app() && \defined( 'APP_TENANT_ID' ) ) {
+ return "{$base_path}/site/" . APP_TENANT_ID;
+ }
+
+ return $base_path;
+ }
+
+ /**
+ * Filters out environment files that do not exist to avoid warnings.
+ */
+ protected function filter_existing_env_files(): void
+ {
+ foreach ( $this->env_files as $key => $file ) {
+ if ( ! file_exists( $this->path . '/' . $file ) ) {
+ unset( $this->env_files[ $key ] );
+ }
+ }
+ }
+
+ /**
+ * Initializes Dotenv with the set path and environment files.
+ * Handles exceptions by using the`wp_terminate` function to exit.
+ */
+ protected function initialize_dotenv(): void
+ {
+ $this->dotenv = Dotenv::createImmutable( $this->path, $this->env_files, $this->short_circuit );
+
+ try {
+ $this->dotenv->load();
+ } catch ( Exception $e ) {
+ wp_terminate( $e->getMessage() );
+ }
+ }
+
/**
* Retrieves the default file names for environment configuration.
*
diff --git a/tests/stubs.php b/tests/stubs.php
index cb313dd..e692fb3 100644
--- a/tests/stubs.php
+++ b/tests/stubs.php
@@ -1,28 +1,98 @@