diff --git a/Model/Resolver/EncryptResolver.php b/Model/Resolver/EncryptResolver.php index 4477427..3d64e28 100644 --- a/Model/Resolver/EncryptResolver.php +++ b/Model/Resolver/EncryptResolver.php @@ -8,7 +8,6 @@ use Magento\Framework\Encryption\EncryptorInterface; use Semaio\ConfigImportExport\Exception\UnresolveableValueException; -use function strlen; class EncryptResolver extends AbstractResolver { @@ -44,7 +43,7 @@ public function resolve($value, $configPath = null) } $valueToEncrypt = preg_replace_callback( - '/\%encrypt\(([^)]+)\)\%/', + '/\%encrypt\((.+)\)\%/', function ($matches) { return $matches[1]; }, @@ -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; } } diff --git a/Model/Resolver/EnvironmentVariableResolver.php b/Model/Resolver/EnvironmentVariableResolver.php index bad89e4..14b141b 100644 --- a/Model/Resolver/EnvironmentVariableResolver.php +++ b/Model/Resolver/EnvironmentVariableResolver.php @@ -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; } @@ -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; } } diff --git a/Model/Resolver/ThemePathResolver.php b/Model/Resolver/ThemePathResolver.php index 50f8f1b..52a02a4 100644 --- a/Model/Resolver/ThemePathResolver.php +++ b/Model/Resolver/ThemePathResolver.php @@ -44,7 +44,7 @@ public function resolve($value, $configPath = null) } $themeCode = preg_replace_callback( - '/\%theme\(([^)]+)\)\%/', + '/\%theme\((.+)\)\%/', function ($matches) { return $matches[1]; }, @@ -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; } } diff --git a/Test/Unit/Model/Resolver/EncryptResolverTest.php b/Test/Unit/Model/Resolver/EncryptResolverTest.php index 2b75ed0..2a94df0 100644 --- a/Test/Unit/Model/Resolver/EncryptResolverTest.php +++ b/Test/Unit/Model/Resolver/EncryptResolverTest.php @@ -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); diff --git a/Test/Unit/Model/Resolver/EnvironmentVariableResolverTest.php b/Test/Unit/Model/Resolver/EnvironmentVariableResolverTest.php index 6ca5858..a44cfc4 100644 --- a/Test/Unit/Model/Resolver/EnvironmentVariableResolverTest.php +++ b/Test/Unit/Model/Resolver/EnvironmentVariableResolverTest.php @@ -116,6 +116,10 @@ public function resolveDataProvider(): \Generator '%env(FEATURE_12345)%', 'testvalue5', ]; + yield [ + '%encrypt(%env(HOSTNAME)%)%', + '%encrypt(testvalue1)%', + ]; yield [ null, '', @@ -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);