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
29 changes: 26 additions & 3 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
* The Git branch name.
* @property-read string $environment
* The environment ID (usually the Git branch plus a hash).
* @property-read string $environmentType
* The environmentType (prod, staging, dev)
* @property-read string $documentRoot
* The absolute path to the web root of the application.
* @property-read string $smtpHost
Expand Down Expand Up @@ -76,6 +78,7 @@ class Config
protected $directVariablesRuntime = [
'branch' => 'BRANCH',
'environment' => 'ENVIRONMENT',
'environmentType' => 'ENVIRONMENT_TYPE',
'documentRoot' => 'DOCUMENT_ROOT',
'smtpHost' => 'SMTP_HOST',
];
Expand Down Expand Up @@ -437,9 +440,16 @@ public function onProduction() : bool
return false;
}

$prodBranch = $this->onDedicated() ? 'production' : 'master';

return $this->getValue('BRANCH') == $prodBranch;
// we need to first confirm that we actually have the `ENVIRONMENT_TYPE` variable,
// because not all legacy containers will have this
if($this->hasVariable('ENVIRONMENT_TYPE')) {
// correct way of checking production branch
return $this->getValue('ENVIRONMENT_TYPE') == 'production';
} else {
// legacy way of checking production type
$prodBranch = $this->onDedicated() ? 'production' : 'master';
Copy link

Choose a reason for hiding this comment

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

master ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

well yeah, that's the backwards compatibility. Ideally we won't ever get into this else statement. But if we do, the functionality needs to be the same.

return $this->getValue('BRANCH') == $prodBranch;
}
}

/**
Expand Down Expand Up @@ -498,6 +508,19 @@ public function hasRelationship(string $relationship) : bool
return isset($this->relationshipsDef[$relationship]);
}

/**
* Check if an environment variable exists, useful for backwards compatibility checks
*
* @param string $name
* The env variable to check.
* @return bool
*/
protected function hasVariable(string $name) :?bool
{
$checkName = $this->envPrefix . strtoupper($name);

return array_key_exists($checkName, $this->environmentVariables);
}
/**
* Reads an environment variable, taking the prefix into account.
*
Expand Down
18 changes: 18 additions & 0 deletions tests/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,24 @@ public function test_ondedicated_returns_false_on_standard() : void
$this->assertFalse($config->onDedicated());
}

public function test_onproduction_prod_is_true() : void
{
$env = $this->mockEnvironmentDeploy;
$env['PLATFORM_ENVIRONMENT_TYPE'] = 'production';
$config = new Config($env);

$this->assertTrue($config->onProduction());
}

public function test_onproduction_stg_is_false() : void
{
$env = $this->mockEnvironmentDeploy;
$env['PLATFORM_ENVIRONMENT_TYPE'] = 'staging';
$config = new Config($env);

$this->assertFalse($config->onProduction());
}

public function test_onproduction_on_dedicated_prod_is_true() : void
{
$env = $this->mockEnvironmentDeploy;
Expand Down