Skip to content
Open
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
5 changes: 2 additions & 3 deletions Model/Resolver/EncryptResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

use Magento\Framework\Encryption\EncryptorInterface;
use Semaio\ConfigImportExport\Exception\UnresolveableValueException;
use function strlen;

class EncryptResolver extends AbstractResolver
{
Expand Down Expand Up @@ -44,7 +43,7 @@ public function resolve($value, $configPath = null)
}

$valueToEncrypt = preg_replace_callback(
'/\%encrypt\(([^)]+)\)\%/',
'/\%encrypt\((.+)\)\%/',
function ($matches) {
return $matches[1];
},
Expand All @@ -59,6 +58,6 @@ function ($matches) {
*/
public function supports($value, $configPath = null): bool
{
return 0 === strncmp((string)$value, '%encrypt', strlen('%encrypt'));
return strpos((string) $value, '%encrypt(') !== false;
}
}
38 changes: 17 additions & 21 deletions Model/Resolver/EnvironmentVariableResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,28 +27,24 @@ public function resolve($value, $configPath = null)
return null;
}

try {
$value = (string) $value;

$value = preg_replace_callback(
'/\%env\((?!PHP_|HTTP_|SERVER_|SCRIPT_|QUERY_|DOCUMENT_)([A-Z0-9\_]{3,})\)\%/',
function ($matches) {
$resolvedValue = getenv($matches[1]);
if ($resolvedValue === false) {
throw new \UnexpectedValueException(sprintf('Environment variable %s does not exist', $matches[1]));
}
$value = (string) $value;

$value = preg_replace_callback(
'/\%env\((?!PHP_|HTTP_|SERVER_|SCRIPT_|QUERY_|DOCUMENT_)([A-Z0-9\_]{3,})\)\%/',
function ($matches) use ($configPath) {
$resolvedValue = getenv($matches[1]);
if ($resolvedValue !== false) {
return $resolvedValue;
},
$value
);
} catch (\UnexpectedValueException $exception) {
if ($this->getInput()->getOption('prompt-missing-env-vars') && $this->getInput()->isInteractive()) {
$value = $this->getQuestionHelper()->ask($this->getInput(), $this->getOutput(), new Question($configPath . ': '));
} else {
throw new UnresolveableValueException($exception->getMessage());
}
}
}

if ($this->getInput()->getOption('prompt-missing-env-vars') && $this->getInput()->isInteractive()) {
return $this->getQuestionHelper()->ask($this->getInput(), $this->getOutput(), new Question($configPath . ': '));
}

throw new UnresolveableValueException(sprintf('Environment variable %s does not exist', $matches[1]));
},
$value
);

return $value;
}
Expand All @@ -58,6 +54,6 @@ function ($matches) {
*/
public function supports($value, $configPath = null): bool
{
return 0 === strncmp((string) $value, '%env', \strlen('%env'));
return strpos((string) $value, '%env(') !== false;
}
}
4 changes: 2 additions & 2 deletions Model/Resolver/ThemePathResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function resolve($value, $configPath = null)
}

$themeCode = preg_replace_callback(
'/\%theme\(([^)]+)\)\%/',
'/\%theme\((.+)\)\%/',
function ($matches) {
return $matches[1];
},
Expand All @@ -64,6 +64,6 @@ function ($matches) {
*/
public function supports($value, $configPath = null): bool
{
return 0 === strncmp((string) $value, '%theme', \strlen('%theme'));
return strpos((string) $value, '%theme(') !== false;
}
}
20 changes: 20 additions & 0 deletions Test/Unit/Model/Resolver/EncryptResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,26 @@ public function resolveDataProvider(): Generator
];
}

/**
* @test
*
* @dataProvider supportsDataProvider
*/
public function supports($value, $expectedResult): void
{
$resolver = new EncryptResolver($this->encryptor);
$this->assertEquals($expectedResult, $resolver->supports($value));
}

public function supportsDataProvider(): Generator
{
yield ['%encrypt(value)%', true];
yield ['%encrypt(%env(KEY)%)%', true];
yield ['plain_value', false];
yield ['%env(KEY)%', false];
yield [null, false];
}

public function testItWillRaiseErrorIfEncryptValueIsEmpty(): void
{
$this->expectException(UnresolveableValueException::class);
Expand Down
25 changes: 25 additions & 0 deletions Test/Unit/Model/Resolver/EnvironmentVariableResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ public function resolveDataProvider(): \Generator
'%env(FEATURE_12345)%',
'testvalue5',
];
yield [
'%encrypt(%env(HOSTNAME)%)%',
'%encrypt(testvalue1)%',
];
yield [
null,
'',
Expand All @@ -130,6 +134,27 @@ public function resolveDataProvider(): \Generator
];
}

/**
* @test
*
* @dataProvider supportsDataProvider
*/
public function supports($value, $expectedResult): void
{
$resolver = new EnvironmentVariableResolver();
$this->assertEquals($expectedResult, $resolver->supports($value));
}

public function supportsDataProvider(): \Generator
{
yield ['%env(HOSTNAME)%', true];
yield ['%encrypt(%env(HOSTNAME)%)%', true];
yield ['https://%env(SUBDOMAIN)%.example.com', true];
yield ['plain_value', false];
yield ['%encrypt(value)%', false];
yield [null, false];
}

public function testItWillPromptForManualValueEntry(): void
{
$this->input->expects($this->once())->method('getOption')->with('prompt-missing-env-vars')->willReturn(true);
Expand Down