Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonkelly committed Jul 15, 2024
2 parents 2abf44b + 6c16479 commit 8cfa0ef
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 29 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
32 changes: 29 additions & 3 deletions src/config/GeneralConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ class GeneralConfig extends BaseConfig
public bool $addTrailingSlashesToUrls = false;

/**
* @var array Any custom Yii [aliases](https://www.yiiframework.com/doc/guide/2.0/en/concept-aliases) that should be defined for every request.
* @var array<string,string|null> Any custom Yii [aliases](https://www.yiiframework.com/doc/guide/2.0/en/concept-aliases) that should be defined for every request.
*
* ```php Static Config
* ->aliases([
Expand Down Expand Up @@ -3386,14 +3386,40 @@ public function addTrailingSlashesToUrls(bool $value = true): self
* ```
*
* @group Environment
* @param array $value
* @param array<string,string|null> $value
* @return self
* @see $aliases
* @since 4.2.0
*/
public function aliases(array $value): self
{
$this->aliases = $value;
$this->aliases = [];
foreach ($value as $name => $path) {
$this->addAlias($name, $path);
}
return $this;
}

/**
* Adds a custom Yii [alias](https://www.yiiframework.com/doc/guide/2.0/en/concept-aliases) that should be defined for every request.
*
* ```php
* ->addAlias('@webroot', '/var/www/')
* ```
*
* @group Environment
* @param string $name
* @param string|null $path
* @return self
* @see $aliases
* @since 4.2.0
*/
public function addAlias(string $name, ?string $path): self
{
if (!str_starts_with($name, '@')) {
$name = "@$name";
}
$this->aliases[$name] = $path;
return $this;
}

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 8cfa0ef

Please sign in to comment.