Skip to content

Commit

Permalink
Support appending subpaths to environment variable names
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonkelly committed Oct 19, 2024
1 parent 6538b96 commit 203ca8e
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-WIP.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
### Development
- Added the `encodeUrl()` Twig function. ([#15838](https://github.com/craftcms/cms/issues/15838))
- Added support for passing aliased field handles into element queries’ `select()`/`addSelect()` methods. ([#15827](https://github.com/craftcms/cms/issues/15827))
- Added support for appending subpaths to environment variable names in environmental settings (e.g. `$PRIMARY_SITE_URL/uploads`).

### Extensibility
- Added `craft\base\RequestTrait::getIsWebRequest()`. ([#15690](https://github.com/craftcms/cms/pull/15690))
Expand Down
4 changes: 2 additions & 2 deletions src/helpers/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,15 +194,15 @@ public static function parseEnv(?string $value): bool|string|null
return null;
}

if (preg_match('/^\$(\w+)$/', $value, $matches)) {
if (preg_match('/^\$(\w+)(\/.*)?/', $value, $matches)) {
$env = static::env($matches[1]);

if ($env === null) {
// starts with $ but not an environment variable/constant, so just give up, it's hopeless!
return $value;
}

$value = $env;
$value = $env . ($matches[2] ?? '');
}

if (is_string($value) && str_starts_with($value, '@')) {
Expand Down
4 changes: 2 additions & 2 deletions src/helpers/Cp.php
Original file line number Diff line number Diff line change
Expand Up @@ -1102,9 +1102,9 @@ public static function autosuggestFieldHtml(array $config): string
$value = $config['value'] ?? '';
if (!isset($config['tip']) && (!isset($value[0]) || !in_array($value[0], ['$', '@']))) {
if ($config['suggestAliases'] ?? false) {
$config['tip'] = Craft::t('app', 'This can be set to an environment variable, or begin with an alias.');
$config['tip'] = Craft::t('app', 'This can begin with an environment variable or alias.');
} else {
$config['tip'] = Craft::t('app', 'This can be set to an environment variable.');
$config['tip'] = Craft::t('app', 'This can begin with an environment variable.');
}
$config['tip'] .= ' ' .
Html::a(Craft::t('app', 'Learn more'), 'https://craftcms.com/docs/4.x/config#control-panel-settings', [
Expand Down
6 changes: 3 additions & 3 deletions src/templates/_includes/forms.twig
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@
{% set config = config|merge({
tip: (config.allowedEnvValues is not defined)
? 'This can be set to an environment variable matching one of the option values.'|t('app')
: 'This can be set to an environment variable.'|t('app'),
: 'This can begin with an environment variable.'|t('app'),
}) %}
{% endif %}
{{ _self.field(config, 'template:_includes/forms/selectize') }}
Expand Down Expand Up @@ -324,8 +324,8 @@
{% if config.tip is not defined and value[0:1] not in ['$', '@'] %}
{% set config = config|merge({
tip: ((config.suggestAliases ?? false)
? 'This can be set to an environment variable, or begin with an alias.'|t('app')
: 'This can be set to an environment variable.'|t('app')) ~ ' ' ~ tag('a', {
? 'This can begin with an environment variable or alias.'|t('app')
: 'This can begin with an environment variable.'|t('app')) ~ ' ' ~ tag('a', {
href: 'https://craftcms.com/docs/4.x/config/#control-panel-settings',
class: 'go',
text: 'Learn more'|t('app'),
Expand Down
4 changes: 2 additions & 2 deletions src/translations/en/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -1547,8 +1547,8 @@
'This can be set to an environment variable with a boolean value ({examples}).' => 'This can be set to an environment variable with a boolean value ({examples}).',
'This can be set to an environment variable with a value of a [supported time zone]({url}).' => 'This can be set to an environment variable with a value of a [supported time zone]({url}).',
'This can be set to an environment variable, or a Twig template that outputs an ID.' => 'This can be set to an environment variable, or a Twig template that outputs an ID.',
'This can be set to an environment variable, or begin with an alias.' => 'This can be set to an environment variable, or begin with an alias.',
'This can be set to an environment variable.' => 'This can be set to an environment variable.',
'This can begin with an environment variable or alias.' => 'This can begin with an environment variable or alias.',
'This can begin with an environment variable.' => 'This can begin with an environment variable.',
'This element is conditional' => 'This element is conditional',
'This field has been modified.' => 'This field has been modified.',
'This field is conditional' => 'This field is conditional',
Expand Down
3 changes: 2 additions & 1 deletion tests/unit/helpers/AppHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,10 @@ public function testParseEnv(): void
{
self::assertNull(App::parseEnv(null));
self::assertSame(CRAFT_TESTS_PATH, App::parseEnv('$CRAFT_TESTS_PATH'));
self::assertSame(CRAFT_TESTS_PATH . '/foo/bar', App::parseEnv('$CRAFT_TESTS_PATH/foo/bar'));
self::assertSame('CRAFT_TESTS_PATH', App::parseEnv('CRAFT_TESTS_PATH'));
self::assertSame('$TEST_MISSING', App::parseEnv('$TEST_MISSING'));
self::assertSame(Craft::getAlias('@vendor/foo'), App::parseEnv('@vendor/foo'));
self::assertSame(Craft::getAlias('@vendor/foo/bar'), App::parseEnv('@vendor/foo/bar'));
}

/**
Expand Down

0 comments on commit 203ca8e

Please sign in to comment.