Skip to content

Commit 1c3a27e

Browse files
authored
Merge pull request #11 from platformsh/local-vars
Improve error handling for missing property variables.
2 parents 7c521cc + d56677b commit 1c3a27e

2 files changed

Lines changed: 67 additions & 32 deletions

File tree

src/Config.php

Lines changed: 38 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -485,39 +485,26 @@ protected function decode($variable)
485485
*/
486486
public function __get($property)
487487
{
488-
$isBuildVar = in_array($property, array_keys($this->directVariables));
489-
$isRuntimeVar = in_array($property, array_keys($this->directVariablesRuntime));
490488
// For now, all unprefixed variables are also runtime variables. If that ever changes this
491489
// logic will change with it.
490+
$isBuildVar = in_array($property, array_keys($this->directVariables));
491+
$isRuntimeVar = in_array($property, array_keys($this->directVariablesRuntime));
492492
$isUnprefixedVar = in_array($property, array_keys($this->unPrefixedVariablesRuntime));
493493

494-
if ($this->inBuild() && $isRuntimeVar) {
495-
throw new BuildTimeVariableAccessException(sprintf('The %s variable is not available during build time.', $property));
494+
if (!($isBuildVar || $isUnprefixedVar || $isRuntimeVar)) {
495+
throw new \InvalidArgumentException(sprintf('No such variable defined: %s', $property));
496496
}
497497

498-
if ($isBuildVar) {
499-
$value = $this->getValue($this->directVariables[$property]);
500-
if (is_null($value)) {
501-
throw new NotValidPlatformException(sprintf('The %s variable is not defined. Are you sure you\'re running on Platform.sh?', $property));
502-
}
503-
return $value;
504-
}
505-
if ($isUnprefixedVar) {
506-
$value = $this->environmentVariables[$this->unPrefixedVariablesRuntime[$property]] ?? null;
507-
if (is_null($value)) {
508-
throw new NotValidPlatformException(sprintf('The %s variable is not defined. Are you sure you\'re running on Platform.sh?', $property));
509-
}
510-
return $value;
511-
}
512-
if ($isRuntimeVar) {
513-
$value = $this->getValue($this->directVariablesRuntime[$property]);
514-
if (is_null($value)) {
515-
throw new NotValidPlatformException(sprintf('The %s variable is not defined. Are you sure you\'re running on Platform.sh?', $property));
498+
$value = $this->getPropertyValue($property);
499+
500+
if (is_null($value)) {
501+
if ($this->inBuild() && !$isBuildVar) {
502+
throw new BuildTimeVariableAccessException(sprintf('The %s variable is not available during build time.', $property));
516503
}
517-
return $value;
504+
throw new NotValidPlatformException(sprintf('The %s variable is not defined. Are you sure you\'re running on Platform.sh?', $property));
518505
}
519506

520-
throw new \InvalidArgumentException(sprintf('No such variable defined: %s', $property));
507+
return $value;
521508
}
522509

523510
/**
@@ -531,21 +518,40 @@ public function __get($property)
531518
*/
532519
public function __isset($property)
533520
{
534-
$isBuildVar = in_array($property, array_keys($this->directVariables));
535-
$isRuntimeVar = in_array($property, array_keys($this->directVariablesRuntime));
521+
$value = $this->getPropertyValue($property);
522+
523+
return !is_null($value);
524+
}
525+
526+
/**
527+
* Returns the value of a dynamic property, whatever it's configuration.
528+
*
529+
* @param string $property
530+
* The property value to get.
531+
* @return string|null
532+
*/
533+
protected function getPropertyValue(string $property) : ?string
534+
{
536535
// For now, all unprefixed variables are also runtime variables. If that ever changes this
537536
// logic will change with it.
537+
$isBuildVar = in_array($property, array_keys($this->directVariables));
538+
$isRuntimeVar = in_array($property, array_keys($this->directVariablesRuntime));
538539
$isUnprefixedVar = in_array($property, array_keys($this->unPrefixedVariablesRuntime));
539540

540-
if ($this->inBuild()) {
541-
return $isBuildVar && !is_null($this->$property);
541+
if ($isBuildVar) {
542+
$value = $this->getValue($this->directVariables[$property]);
542543
}
543-
544-
if ($isBuildVar || $isRuntimeVar || $isUnprefixedVar) {
545-
return !is_null($this->$property);
544+
else if ($isUnprefixedVar) {
545+
$value = $this->environmentVariables[$this->unPrefixedVariablesRuntime[$property]] ?? null;
546+
}
547+
else if ($isRuntimeVar) {
548+
$value = $this->getValue($this->directVariablesRuntime[$property]);
549+
}
550+
else {
551+
$value = null;
546552
}
547553

548-
return false;
554+
return $value;
549555
}
550556

551557
/**

tests/ConfigTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,35 @@ public function test_build_and_deploy_properties_in_deploy_exists() : void
324324
$this->assertTrue(isset($config->socket));
325325
}
326326

327+
public function test_build_and_deploy_properties_mocked_in_local_exists() : void
328+
{
329+
$env = $this->mockEnvironmentDeploy;
330+
unset($env['PLATFORM_APPLICATION'], $env['PLATFORM_ENVIRONMENT'], $env['PLATFORM_BRANCH']);
331+
$config = new Config($env);
332+
333+
$this->assertEquals('/app', $config->appDir);
334+
$this->assertEquals('app', $config->applicationName);
335+
$this->assertEquals('test-project', $config->project);
336+
$this->assertEquals('abc123', $config->treeId);
337+
$this->assertEquals('def789', $config->projectEntropy);
338+
339+
$this->assertEquals('/app/web', $config->documentRoot);
340+
$this->assertEquals('1.2.3.4', $config->smtpHost);
341+
$this->assertEquals('8080', $config->port);
342+
$this->assertEquals('unix://tmp/blah.sock', $config->socket);
343+
344+
$this->assertTrue(isset($config->appDir));
345+
$this->assertTrue(isset($config->applicationName));
346+
$this->assertTrue(isset($config->project));
347+
$this->assertTrue(isset($config->treeId));
348+
$this->assertTrue(isset($config->projectEntropy));
349+
350+
$this->assertTrue(isset($config->documentRoot));
351+
$this->assertTrue(isset($config->smtpHost));
352+
$this->assertTrue(isset($config->port));
353+
$this->assertTrue(isset($config->socket));
354+
}
355+
327356
public function test_deploy_property_in_build_throws() : void
328357
{
329358
$this->expectException(BuildTimeVariableAccessException::class);

0 commit comments

Comments
 (0)