Skip to content

Commit

Permalink
App type-based config support
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonkelly committed Jul 13, 2024
1 parent 519481f commit eb47a44
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 26 deletions.
18 changes: 8 additions & 10 deletions bootstrap/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,18 @@
// -----------------------------------------------------------------------------

$configService = new Config();
$configService->appType = $appType;
$configService->env = $environment;
$configService->configDir = $configPath;
$configService->appDefaultsDir = dirname(__DIR__) . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'defaults';
$generalConfig = $configService->getConfigFromFile('general');
$generalConfig = $configService->getGeneral();

// Validation
// -----------------------------------------------------------------------------

$createFolder = function($path) use ($generalConfig) {
if (!is_dir($path)) {
FileHelper::createDirectory($path, $generalConfig['defaultDirMode'] ?? 0775);
FileHelper::createDirectory($path, $generalConfig->defaultDirMode ?? 0775);
}
};

Expand Down Expand Up @@ -160,7 +161,7 @@
// Determine if Craft is running in Dev Mode
// -----------------------------------------------------------------------------

$devMode = App::env('CRAFT_DEV_MODE') ?? $generalConfig['devMode'] ?? false;
$devMode = App::env('CRAFT_DEV_MODE') ?? $generalConfig->devMode;

if ($devMode) {
ini_set('display_errors', '1');
Expand Down Expand Up @@ -215,12 +216,9 @@
}

// Set any custom aliases
$customAliases = $generalConfig['aliases'] ?? $generalConfig['environmentVariables'] ?? null;
if (is_array($customAliases)) {
foreach ($customAliases as $name => $value) {
if (is_string($value)) {
Craft::setAlias($name, $value);
}
foreach ($generalConfig->aliases as $name => $value) {
if (is_string($value)) {
Craft::setAlias($name, $value);
}
}

Expand All @@ -244,7 +242,7 @@
$configService->getConfigFromFile("app.{$appType}")
);

$safeMode = App::env('CRAFT_SAFE_MODE') ?? $generalConfig['safeMode'] ?? false;
$safeMode = App::env('CRAFT_SAFE_MODE') ?? $generalConfig->safeMode;

if ($safeMode) {
ArrayHelper::remove($localConfig, 'bootstrap');
Expand Down
53 changes: 37 additions & 16 deletions src/services/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ class Config extends Component
public const CATEGORY_DB = 'db';
public const CATEGORY_GENERAL = 'general';

/**
* @var string The application type (`web` or `console`).
*/
public string $appType;

/**
* @var string|null The environment ID Craft is currently running in.
*
Expand Down Expand Up @@ -94,21 +99,27 @@ class Config extends Component
public function getConfigSettings(string $category): object
{
if (!isset($this->_configSettings[$category])) {
$this->_configSettings[$category] = $this->_createConfigObj($category);
$config = $this->_createConfigObj($category, $category, null);

if (isset($this->appType)) {
// See if an application type-specific config exists (general.web.php / general.console.php)
/** @var GeneralConfig|DbConfig $config */
$config = $this->_createConfigObj($category, "$category.$this->appType", $config);
}

$this->_configSettings[$category] = $config;
}

return $this->_configSettings[$category];
}

/**
* Creates a new config object.
*
* @param string $category The config category
* @return object
*/
private function _createConfigObj(string $category): object
private function _createConfigObj(string $category, string $filename, ?BaseConfig $existingConfig): object
{
$config = $this->getConfigFromFile($category);
$config = $this->getConfigFromFile($filename);

if ($existingConfig && empty($config)) {
return $existingConfig;
}

switch ($category) {
case self::CATEGORY_CUSTOM:
Expand All @@ -125,6 +136,10 @@ private function _createConfigObj(string $category): object
throw new InvalidArgumentException("Invalid config category: $category");
}

if (is_callable($config)) {
$config = $config($existingConfig ?? $configClass::create());
}

// Get any environment value overrides
$envConfig = App::envConfig($configClass, $envPrefix);

Expand All @@ -148,12 +163,18 @@ private function _createConfigObj(string $category): object
}

$loadingConfig = $this->_loadingConfigFile;
$this->_loadingConfigFile = $category;
$this->_loadingConfigFile = $filename;

$config = array_merge($config, $envConfig);
Typecast::properties($configClass, $config);
/** @var BaseObject $config */
$config = new $configClass($config);

if ($existingConfig !== null) {
Craft::configure($existingConfig, $config);
$config = $existingConfig;
} else {
/** @var BaseObject $config */
$config = new $configClass($config);
}

$this->_loadingConfigFile = $loadingConfig;
return $config;
Expand Down Expand Up @@ -244,9 +265,9 @@ public function getConfigFilePath(string $filename): string
* ```
*
* @param string $filename
* @return array|BaseConfig
* @return array|callable|BaseConfig
*/
public function getConfigFromFile(string $filename): array|BaseConfig
public function getConfigFromFile(string $filename): array|callable|BaseConfig
{
$path = $this->getConfigFilePath($filename);

Expand All @@ -263,11 +284,11 @@ public function getConfigFromFile(string $filename): array|BaseConfig
return $config;
}

private function _configFromFileInternal(string $path): array|BaseConfig
private function _configFromFileInternal(string $path): array|callable|BaseConfig
{
$config = @include $path;

if ($config instanceof BaseConfig) {
if ($config instanceof BaseConfig || is_callable($config)) {
return $config;
}

Expand Down

0 comments on commit eb47a44

Please sign in to comment.