Skip to content

Allow env config for attributes - #13666

Closed
timkelty wants to merge 3 commits into
5.xfrom
allow-env-config-for-attributes
Closed

Allow env config for attributes#13666
timkelty wants to merge 3 commits into
5.xfrom
allow-env-config-for-attributes

Conversation

@timkelty

@timkelty timkelty commented Sep 7, 2023

Copy link
Copy Markdown
Contributor

Description

Detects if you pass \craft\helpers\App::envConfig a Model, and if so, uses the the model's attributes instead of just getting public properties.

This allows config overrides to work if you have an attribute with a getter/setter.

Another option could be to just allow you to pass an array of properties to override, so when you called \craft\helpers\App::envConfig, you could just pass if your model's attributes.

One potential issue here might be if you have a read-only attribute (no setter), and you try to set the corresponding env for it, it will throw.

@brandonkelly

Copy link
Copy Markdown
Member

This allows config overrides to work if you have an attribute with a getter/setter.

Typecast would need to be updated to support that as well.

@timkelty
timkelty marked this pull request as draft January 24, 2024 02:57
…ttributes

# Conflicts:
#	src/helpers/App.php
Copilot AI review requested due to automatic review settings August 4, 2026 02:01

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Updates craft\helpers\App::envConfig() to support Yii models by deriving configurable keys from model attributes (instead of only reflecting public properties), enabling environment overrides to work for attribute-backed settings.

Changes:

  • Expand envConfig() to accept an object or class-string and detect Yii models.
  • Build env override keys from either model attributes() or reflected public properties.
  • Factor out EnvName attribute lookup into a dedicated helper (envNameForProperty()).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/helpers/App.php
$envName = strtoupper(StringHelper::toSnakeCase($prop->getName()));
}
$reflection = new ReflectionClass($class);
$isModel = $reflection->isSubclassOf(Model::class);
Comment thread src/helpers/App.php
Comment on lines +217 to +218
// CONFLICT-REVIEW: model attributes() can include names that aren’t backed by a real property
// (behaviors, magic properties), so only reflect on the ones that are.
Comment thread src/helpers/App.php
Comment on lines +171 to 173
* @param object|string $class The class name or object
* @phpstan-param class-string|object $class
* @param string|null $envPrefix The environment variable name prefix
@brandonkelly

Copy link
Copy Markdown
Member

Closing

@brandonkelly
brandonkelly deleted the allow-env-config-for-attributes branch August 4, 2026 14:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants