Skip to content

Commit

Permalink
Allow the System Name setting to be set to an env var (#3529)
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonkelly committed Jan 22, 2019
1 parent dac3349 commit 534b069
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 6 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG-v3.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Release Notes for Craft CMS 3.x

## Unreleased

### Added
- The System Name setting can now be set to an environment variable. ([#3529](https://github.com/craftcms/cms/issues/3529))
- Added `craft\base\ApplicationTrait::getSystemName()`.

## 3.1.3 - 2019-01-21

### Added
Expand Down
2 changes: 2 additions & 0 deletions docs/config/environments.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ Some settings should be defined on a per-environment basis. For example, when de

Some settings in the Control Panel can be set to environment variables (like the ones defined in your `.env` file):

- General Settings
- **System Name**
- Sites
- **Base URL**
- Asset Volumes
Expand Down
21 changes: 21 additions & 0 deletions src/base/ApplicationTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use craft\db\Query;
use craft\db\Table;
use craft\errors\DbConnectException;
use craft\errors\SiteNotFoundException;
use craft\errors\WrongEditionException;
use craft\events\EditionChangeEvent;
use craft\helpers\App;
Expand Down Expand Up @@ -613,6 +614,26 @@ public function saveInfo(Info $info): bool
return false;
}

/**
* Returns the system name.
*
* @return string
*/
public function getSystemName(): string
{
if (($name = Craft::$app->getProjectConfig()->get('system.name')) !== null) {
return Craft::parseEnv($name);
}

try {
$name = $this->getSites()->getPrimarySite()->name;
} catch (SiteNotFoundException $e) {
$name = null;
}

return $name ?: 'Craft';
}

/**
* Returns the Yii framework version.
*
Expand Down
2 changes: 1 addition & 1 deletion src/db/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ private function _getFixedSystemName(): string
->from([Table::INFO])
->column()[0];
} catch (\Throwable $e) {
return Craft::$app->getProjectConfig()->get('system.name') ?? Craft::$app->getSites()->getPrimarySite()->name;
return Craft::$app->getSystemName();
}
}
}
4 changes: 2 additions & 2 deletions src/models/Info.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,11 @@ public function getEdition(): int
* Returns the system name.
*
* @return string
* @deprecated in 3.1. Use `Craft::$app->projectConfig->get('system.name')` instead.
* @deprecated in 3.1. Use `Craft::$app->getSystemName()` instead.
*/
public function getName(): string
{
return Craft::$app->getProjectConfig()->get('system.name');
return Craft::$app->getSystemName();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/templates/_layouts/base.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% set systemName = craft.app.projectConfig.get('system.name')|t('site') -%}
{% set systemName = craft.app.getSystemName()|t('site') -%}
{% set docTitle = docTitle is defined ? docTitle : title|striptags -%}
{% set bodyClass = (bodyClass is defined ? bodyClass~' ' : '') ~ craft.app.locale.getOrientation() -%}

Expand Down
3 changes: 2 additions & 1 deletion src/templates/settings/general/_index.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@
<input type="hidden" name="action" value="system-settings/save-general-settings">
{{ redirectInput('settings') }}

{{ forms.textField({
{{ forms.autosuggestField({
first: true,
label: "System Name"|t('app'),
id: 'name',
suggestions: craft.cp.getEnvSuggestions(),
name: 'name',
value: system.name
}) }}
Expand Down
2 changes: 1 addition & 1 deletion src/web/twig/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -1022,7 +1022,7 @@ public function getGlobals(): array

// Only set these things when Craft is installed and not being updated
if ($isInstalled && !Craft::$app->getUpdates()->getIsCraftDbMigrationNeeded()) {
$globals['systemName'] = Craft::$app->getProjectConfig()->get('system.name');
$globals['systemName'] = Craft::$app->getSystemName();
/** @noinspection PhpUnhandledExceptionInspection */
$site = Craft::$app->getSites()->getCurrentSite();
$globals['currentSite'] = $site;
Expand Down

0 comments on commit 534b069

Please sign in to comment.