Skip to content
Merged
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
70 changes: 39 additions & 31 deletions src/Config/ConfigServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@
use CraftCms\Cms\Support\Typecast;
use Illuminate\Contracts\Config\Repository as ConfigRepository;
use Illuminate\Foundation\Bootstrap\LoadEnvironmentVariables;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\File;
use Illuminate\Support\ServiceProvider;
use Override;
use Throwable;

class ConfigServiceProvider extends ServiceProvider
{
Expand All @@ -32,7 +30,15 @@ public function register(): void

$this->loadEnvironmentVariablesWhenConfigIsCached();

$this->app->singleton(GeneralConfig::class, fn () => $this->app->make(ConfigRepository::class)->get('craft.general'));
if (! $this->app->bound('config')) {
return;
}

$this->app->singleton(GeneralConfig::class, function () {
$repository = $this->app->make(ConfigRepository::class);

return $this->loadGeneralConfig($repository);
});

collect($this->configFiles)->each(function (string $file) {
if ($file === 'general') {
Expand All @@ -45,8 +51,9 @@ public function register(): void

public function boot(): void
{
$this->app->make(GeneralConfig::class);

$this->bootPublishables();
$this->loadGeneralConfig();
$this->loadHtmlSanitizers();
}

Expand Down Expand Up @@ -76,46 +83,47 @@ private function bootPublishables(): void
});
}

private function loadGeneralConfig(): void
private function loadGeneralConfig(ConfigRepository $repository): GeneralConfig
{
$generalConfig = Config::get('craft.general', []);
/** @var GeneralConfig|array $staticConfig */
$staticConfig = $repository->get('craft.general', []);

/**
* When the configuration is a simple array config, load it into
* the GeneralConfig object and replace the configuration key.
*/
if (! $generalConfig instanceof GeneralConfig) {
$generalConfig = GeneralConfig::__set_state($generalConfig);
if ($staticConfig instanceof GeneralConfig) {
$config = clone $staticConfig;
} else {
Typecast::properties(GeneralConfig::class, $staticConfig);
$config = GeneralConfig::create();

Config::set('craft.general', $generalConfig);
foreach ($staticConfig as $setting => $value) {
$this->applyGeneralConfigSetting($config, $setting, $value);
}
}

$configClass = $generalConfig::class;
$envConfig = Env::config($config::class, 'CRAFT_');
Typecast::properties($config::class, $envConfig);

// Get any environment value overrides
$envConfig = Env::config($configClass, 'CRAFT_');
foreach ($envConfig as $setting => $value) {
$this->applyGeneralConfigSetting($config, $setting, $value);
}

Typecast::properties($configClass, $envConfig);
foreach ($config->aliases as $name => $value) {
Aliases::set($name, $value);
}

foreach ($envConfig as $name => $value) {
// Use the fluent methods when possible, in case it has any value normalization logic
if (! method_exists($generalConfig, $name)) {
continue;
}
$repository->set('craft.general', $config);

try {
$generalConfig->$name($value);
return $config;
}

continue;
} catch (Throwable) {
}
private function applyGeneralConfigSetting(GeneralConfig $config, string $setting, mixed $value): void
{
if (method_exists($config, $setting)) {
$config->{$setting}($value);

$generalConfig->$name = $value;
return;
}

foreach ($generalConfig->aliases as $name => $value) {
Aliases::set($name, $value);
}
$config->{$setting} = $value;
}

private function loadHtmlSanitizers(): void
Expand Down
186 changes: 1 addition & 185 deletions src/Config/GeneralConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,9 @@
use CraftCms\Cms\Auth\Enums\CpAuthPath;
use CraftCms\Cms\Support\Attributes\EnvName;
use CraftCms\Cms\Support\Config as ConfigHelper;
use CraftCms\Cms\Support\DateTimeHelper;
use CraftCms\Cms\Support\Env;
use CraftCms\Cms\Support\Facades\Deprecator;
use CraftCms\Cms\Support\Facades\I18N;
use CraftCms\Cms\Support\PHP;
use DateInterval;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Traits\Conditionable;
use InvalidArgumentException;
use Override;
Expand All @@ -37,18 +33,7 @@ class GeneralConfig extends BaseConfig
public const string SNAKE_CASE = 'snake';

#[Override]
protected static array $renamedSettings = [
'activateAccountFailurePath' => 'invalidUserTokenPath',
'allowAutoUpdates' => 'allowUpdates',
'backupDbOnUpdate' => 'backupOnUpdate',
'defaultFilePermissions' => 'defaultFileMode',
'defaultFolderPermissions' => 'defaultDirMode',
'enableGraphQlCaching' => 'enableGraphqlCaching',
'environmentVariables' => 'aliases',
'isSystemOn' => 'isSystemLive',
'restoreDbOnUpdateFailure' => 'restoreOnUpdateFailure',
'validationKey' => 'securityKey',
];
protected static array $renamedSettings = [];

/**
* @var array The default user accessibility preferences that should be applied to users that haven’t saved their preferences yet.
Expand Down Expand Up @@ -642,24 +627,6 @@ class GeneralConfig extends BaseConfig
*/
public ?string $cpTrigger = 'admin';

/**
* @var string The domain that cookies generated by Craft should be created for. If blank, it will be left up to the browser to determine
* which domain to use (almost always the current). If you want the cookies to work for all subdomains, for example, you could
* set this to `'.my-project.tld'`.
*
* ::: code
* ```php Static Config
* ->defaultCookieDomain('.my-project.tld')
* ```
* ```shell Environment Override
* CRAFT_DEFAULT_COOKIE_DOMAIN=.my-project.tld
* ```
* :::
*
* @group Environment
*/
public string $defaultCookieDomain = '';

/**
* @var string The two-letter country code that addresses will be set to by default.
*
Expand Down Expand Up @@ -2256,28 +2223,6 @@ class GeneralConfig extends BaseConfig
*/
public mixed $rememberUsernameDuration = 31536000;

/**
* @var mixed The amount of time a user stays logged if “Remember Me” is checked on the login page.
*
* Set to `0` to disable the “Remember Me” feature altogether.
*
* See {@see ConfigHelper::durationInSeconds()} for a list of supported value types.
*
* ::: code
* ```php Static Config
* ->rememberedUserSessionDuration(0)
* ```
* ```shell Environment Override
* CRAFT_REMEMBERED_USER_SESSION_DURATION=0
* ```
* :::
*
* @group Session
*
* @defaultAlt 14 days
*/
public mixed $rememberedUserSessionDuration = 1209600;

/**
* @var string The path to the root directory that should store published control panel resources.
*
Expand Down Expand Up @@ -3110,28 +3055,6 @@ class GeneralConfig extends BaseConfig
*/
public string|bool $useSslOnTokenizedUrls = 'auto';

/**
* @var mixed The amount of time a user verification code can be used before expiring.
*
* See {@see ConfigHelper::durationInSeconds()} for a list of supported value types.
*
* ::: code
* ```php Static Config
* // 1 hour
* ->verificationCodeDuration(3600)
* ```
* ```shell Environment Override
* # 1 hour
* CRAFT_VERIFICATION_CODE_DURATION=3600
* ```
* :::
*
* @group Security
*
* @defaultAlt 1 day
*/
public mixed $verificationCodeDuration = 86400;

/**
* @var mixed The URI or URL that Craft should use for email verification links on the front end.
*
Expand Down Expand Up @@ -3191,7 +3114,6 @@ public function __construct()
->purgeUnsavedDraftsDuration($this->purgeUnsavedDraftsDuration)
->rememberUsernameDuration($this->rememberUsernameDuration)
->softDeleteDuration($this->softDeleteDuration)
->verificationCodeDuration($this->verificationCodeDuration)
// locales
->defaultCpLanguage($this->defaultCpLanguage)
->extraAppLocales($this->extraAppLocales)
Expand Down Expand Up @@ -3782,32 +3704,6 @@ public function cpTrigger(?string $value): self
return $this;
}

/**
* The domain that cookies generated by Craft should be created for. If blank, it will be left up to the browser to determine
* which domain to use (almost always the current). If you want the cookies to work for all subdomains, for example, you could
* set this to `'.my-project.tld'`.
*
* ```php
* ->defaultCookieDomain('.my-project.tld')
* ```
*
* @group Environment
*
* @see $defaultCookieDomain
* @since 4.2.0
*/
public function defaultCookieDomain(string $value): self
{
$this->defaultCookieDomain = $value;

app()->booting(function () use ($value) {
Deprecator::log('generalConfig.defaultCookieDomain', 'Calling defaultCookieDomain() is deprecated.');
Config::set('session.domain', $value);
});

return $this;
}

/**
* The two-letter country code that addresses will be set to by default.
*
Expand Down Expand Up @@ -5510,47 +5406,6 @@ public function rasterizeSvgThumbs(bool $value = true): self
return $this;
}

/**
* The amount of time a user stays logged if “Remember Me” is checked on the login page.
*
* Set to `0` to disable the “Remember Me” feature altogether.
*
* See {@see ConfigHelper::durationInSeconds()} for a list of supported value types.
*
* ```php
* ->rememberedUserSessionDuration(0)
* ```
*
* @group Session
*
* @defaultAlt 14 days
*
* @throws RuntimeException
*
* @see $rememberedUserSessionDuration
* @since 4.2.0
*/
public function rememberedUserSessionDuration(mixed $value): self
{
// Store the DateInterval separately for getRememberedUserSessionDuration()
try {
$interval = DateTimeHelper::toDateInterval($value);
} catch (InvalidArgumentException $e) {
throw new RuntimeException($e->getMessage(), 0, $e);
}

$this->rememberedUserSessionDuration = $interval ? ConfigHelper::durationInSeconds($interval) : 0;

app()->booting(function () {
Config::set(
'auth.guards.craft.remember',
floor($this->rememberedUserSessionDuration / 60),
);
});

return $this;
}

/**
* The amount of time Craft will remember a username and pre-populate it on the control panel’s Login page.
*
Expand Down Expand Up @@ -6318,33 +6173,6 @@ public function useSslOnTokenizedUrls(string|bool $value): self
return $this;
}

/**
* The amount of time a user verification code can be used before expiring.
*
* See {@see ConfigHelper::durationInSeconds()} for a list of supported value types.
*
* ```php
* // 1 hour
* ->verificationCodeDuration(3600)
* ```
*
* @group Security
*
* @defaultAlt 1 day
*
* @see $verificationCodeDuration
*/
public function verificationCodeDuration(mixed $value): self
{
$this->verificationCodeDuration = ConfigHelper::durationInSeconds($value);

app()->booted(function () {
Config::set('auth.passwords.craft.expire', $this->verificationCodeDuration);
});

return $this;
}

/**
* The URI or URL that Craft should use for email verification links on the front end.
*
Expand Down Expand Up @@ -6505,18 +6333,6 @@ public function getPostLogoutRedirect(?string $siteHandle = null): string
return ConfigHelper::localizedValue($this->postLogoutRedirect, $siteHandle);
}

/**
* Returns the remembered user session duration as a [[\DateInterval]] object, if it’s set.
*
* @since 4.2.1
*/
public function getRememberedUserSessionDuration(): ?DateInterval
{
return $this->rememberedUserSessionDuration > 0
? DateTimeHelper::toDateInterval($this->rememberedUserSessionDuration)
: null;
}

/**
* Returns the localized Set Password Path value.
*
Expand Down
2 changes: 1 addition & 1 deletion src/Cp/Cp.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public static function config(): Collection
'cpTrigger' => $generalConfig->cpTrigger,
'baseCpUrl' => Url::cpUrl(),
'defaultCpLocale' => $generalConfig->defaultCpLocale,
'rememberedUserSessionDuration' => $generalConfig->rememberedUserSessionDuration,
'rememberedUserSessionDuration' => (int) config('auth.guards.craft.remember', 20160) * 60,
'runQueueAutomatically' => $generalConfig->runQueueAutomatically,
]);
}
Expand Down
Loading
Loading