Skip to content

Commit 632315e

Browse files
committed
fix: ENVIRONMENT_TYPE needs to be used to check onProduction()
1 parent f7bd009 commit 632315e

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

src/Config.php

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
* The Git branch name.
3636
* @property-read string $environment
3737
* The environment ID (usually the Git branch plus a hash).
38+
* @property-read string $environmentType
39+
* The environmentType (prod, staging, dev)
3840
* @property-read string $documentRoot
3941
* The absolute path to the web root of the application.
4042
* @property-read string $smtpHost
@@ -76,6 +78,7 @@ class Config
7678
protected $directVariablesRuntime = [
7779
'branch' => 'BRANCH',
7880
'environment' => 'ENVIRONMENT',
81+
'environmentType' => 'ENVIRONMENT_TYPE',
7982
'documentRoot' => 'DOCUMENT_ROOT',
8083
'smtpHost' => 'SMTP_HOST',
8184
];
@@ -437,9 +440,20 @@ public function onProduction() : bool
437440
return false;
438441
}
439442

440-
$prodBranch = $this->onDedicated() ? 'production' : 'master';
441-
442-
return $this->getValue('BRANCH') == $prodBranch;
443+
// we need to first confirm that we actually have the `ENVIRONMENT_TYPE` variable,
444+
// because not all legacy containers will have this
445+
print_r(array_keys($this->environmentVariables));
446+
if($this->hasVariable('ENVIRONMENT_TYPE')) {
447+
// correct way of checking production branch
448+
echo 'new';
449+
450+
return $this->getValue('ENVIRONMENT_TYPE') == 'production';
451+
} else {
452+
echo "legacy";
453+
// legacy way of checking production type
454+
$prodBranch = $this->onDedicated() ? 'production' : 'master';
455+
return $this->getValue('BRANCH') == $prodBranch;
456+
}
443457
}
444458

445459
/**
@@ -498,6 +512,17 @@ public function hasRelationship(string $relationship) : bool
498512
return isset($this->relationshipsDef[$relationship]);
499513
}
500514

515+
/**
516+
* Check if an environment variable exists, useful for backwards compatibility checks
517+
*
518+
* @param string $name
519+
* The env variable to check.
520+
* @return bool
521+
*/
522+
protected function hasVariable(string $name) :?bool
523+
{
524+
return array_key_exists($name, $this->environmentVariables);
525+
}
501526
/**
502527
* Reads an environment variable, taking the prefix into account.
503528
*

tests/ConfigTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,27 @@ public function test_ondedicated_returns_false_on_standard() : void
200200
$this->assertFalse($config->onDedicated());
201201
}
202202

203+
public function test_onproduction_prod_is_true() : void
204+
{
205+
echo 'test_onproduction_prod_is_true';
206+
207+
$env = $this->mockEnvironmentDeploy;
208+
$env['PLATFORM_ENVIRONMENT_TYPE'] = 'production';
209+
$config = new Config($env);
210+
211+
$this->assertTrue($config->onProduction());
212+
}
213+
214+
public function test_onproduction_stg_is_false() : void
215+
{
216+
echo 'test_onproduction_stg_is_false';
217+
$env = $this->mockEnvironmentDeploy;
218+
$env['PLATFORM_ENVIRONMENT_TYPE'] = 'staging';
219+
$config = new Config($env);
220+
221+
$this->assertFalse($config->onProduction());
222+
}
223+
203224
public function test_onproduction_on_dedicated_prod_is_true() : void
204225
{
205226
$env = $this->mockEnvironmentDeploy;

0 commit comments

Comments
 (0)