-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #48 from peterjaap/add-env-var-support
Added support and readme for environment variables
- Loading branch information
Showing
4 changed files
with
131 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
/** | ||
* Copyright © semaio GmbH. All rights reserved. | ||
* See LICENSE.md bundled with this module for license details. | ||
*/ | ||
|
||
namespace Semaio\ConfigImportExport\Model\Resolver; | ||
|
||
class EnvironmentVariableResolver | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $value; | ||
|
||
/** | ||
* Resolve the config value if it's an environment | ||
* variable reference. | ||
* | ||
* @throws \UnexpectedValueException | ||
* | ||
* @param string $value | ||
* @return string|false the original string, or the resolved | ||
* string if it's a reference or false on error | ||
*/ | ||
public function resolveValue($value) | ||
{ | ||
$this->value = $value; | ||
return preg_replace_callback( | ||
'/\%env\(([^:\%\%]+?)\)\%/', | ||
function ($matches) { | ||
$resolvedValue = getenv($matches[1]); | ||
if ($resolvedValue === false) { | ||
throw new \UnexpectedValueException(sprintf('Environment variable %s does not exist', $matches[1])); | ||
} | ||
return $resolvedValue; | ||
}, | ||
$value | ||
); | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
Test/Unit/Model/Resolver/EnvironmentVariableResolverTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
/** | ||
* Copyright © semaio GmbH. All rights reserved. | ||
* See LICENSE.md bundled with this module for license details. | ||
*/ | ||
|
||
namespace Semaio\ConfigImportExport\Test\Unit\Model\Validator; | ||
|
||
use Magento\Store\Api\Data\WebsiteInterface; | ||
use Magento\Store\Model\StoreManagerInterface; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use PHPUnit\Framework\TestCase; | ||
use Semaio\ConfigImportExport\Model\Resolver\EnvironmentVariableResolver; | ||
|
||
class EnvironmentVariableResolverTest extends TestCase | ||
{ | ||
/** | ||
* @var EnvironmentVariableResolver | ||
*/ | ||
private $environmentVariableResolver; | ||
|
||
/** | ||
* Set up test class | ||
*/ | ||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
putenv('HOSTNAME=testvalue1'); | ||
putenv('SUBDOMAIN=testvalue2'); | ||
putenv('CONCAT_THIS=testvalue3'); | ||
putenv('WITH_THIS=testvalue4'); | ||
|
||
$this->environmentVariableResolver = new EnvironmentVariableResolver(); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function validate(): void | ||
{ | ||
$this->assertEquals($this->environmentVariableResolver->resolveValue('test_without_env_var'), 'test_without_env_var'); | ||
|
||
$this->assertEquals($this->environmentVariableResolver->resolveValue('%env(HOSTNAME)%'), 'testvalue1'); | ||
$this->assertEquals($this->environmentVariableResolver->resolveValue('https://%env(SUBDOMAIN)%.example.com'), 'https://testvalue2.example.com'); | ||
$this->assertEquals($this->environmentVariableResolver->resolveValue('%env(CONCAT_THIS)%%env(WITH_THIS)%'), 'testvalue3testvalue4'); | ||
|
||
$this->expectException(\UnexpectedValueException::class); | ||
$this->environmentVariableResolver->resolveValue('%env(DOESNOTEXIST)%'); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters