Cms: Bring back support for environmentVariables (sort of)

Created on 30 Dec 2017  路  5Comments  路  Source: craftcms/cms

We should bring back support for the environmentVariables config setting, but only as a way to define _actual_ environment variables (using setenv()). Then discard them.

We should also bring back environment variable tag parsing in things like asset volume settings, but for the _actual_ environment variables; not a Craft-specific concept of them. So if a volume鈥檚 File System Path is set to {FOO}/images, {FOO} would be replaced with the value of a FOO environment variable (fetched via getenv() 鈥撀爏o it doesn鈥檛 matter how it was set).

enhancement

Most helpful comment

Took this in a slightly different direction. Instead of re-adding support for the environmentVariables config setting (and redefining what it means), I鈥檝e added support for a new aliases config setting, which can be used to define new Yii aliases. Plus, asset paths and URLs can now begin with an alias (as well as site URLs). And for backwards compatibility, if no aliases config setting was defined, Craft will fallback to environmentVariables (and log a deprecation error).

I鈥檝e also added a migration that will automatically convert volume settings that begin with a Craft 2-style environment variable tag, so that they follow the new @alias/subpath format instead.

In summary, Craft 2鈥檚 environmentVariables are now replaced with aliases, and the syntax is a bit different (@alias/subpath instead of {variable}subpath), but for all intents and purposes they basically work the same.

All 5 comments

I like the idea and think it will reduce quite a bit of confusion that people are having. I wish it wasn't coming post-RC, but better than post-GA!

Will this environmental variable parsing work everywhere that a micro-template is parse via renderObjectTemplate() or will it only work in specific places?

I could see it being useful beyond just Asset Volumes and Assets... maybe even with a renderEnvironmentObjectTemplate() method that does it automatically, and is used for any AdminCP fields that use micro-template rendering?

It would need to be separate from renderObjectTemplate(), as that assumes that all {singleBraceTags} are references to properties of the passed-in object.

Right, that's what I'm suggesting. A renderEnvironmentObjectTemplate() method that first parses any matching ENV vars, and then calls renderObjectTemplate() on the result.

p-code:

foreach $curlyBraceMatches as $curlyBraceMatch {
    if ($matchEnv = getEnv($curlyBraceMatch)) {
        replaceEnvVars($curlyBraceMatch, $matchEnv);
    }
}
$this->renderObjectTemplate();

Took this in a slightly different direction. Instead of re-adding support for the environmentVariables config setting (and redefining what it means), I鈥檝e added support for a new aliases config setting, which can be used to define new Yii aliases. Plus, asset paths and URLs can now begin with an alias (as well as site URLs). And for backwards compatibility, if no aliases config setting was defined, Craft will fallback to environmentVariables (and log a deprecation error).

I鈥檝e also added a migration that will automatically convert volume settings that begin with a Craft 2-style environment variable tag, so that they follow the new @alias/subpath format instead.

In summary, Craft 2鈥檚 environmentVariables are now replaced with aliases, and the syntax is a bit different (@alias/subpath instead of {variable}subpath), but for all intents and purposes they basically work the same.

Very clever.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mccombs picture mccombs  路  3Comments

leigeber picture leigeber  路  3Comments

lukebailey picture lukebailey  路  3Comments

bitboxfw picture bitboxfw  路  3Comments

benface picture benface  路  3Comments