Skip to content

Commit

Permalink
bug #493 Improve regex for container configurator (maxhelias)
Browse files Browse the repository at this point in the history
This PR was merged into the 1.2-dev branch.

Discussion
----------

Improve regex for container configurator

Escapes the regex of the container configurator because for the case of the variables "env()" this one are broken.
Example with the mercure or doctrine/mongodb-odm-bundle recipe if you remove the dependence.

Commits
-------

a3a22b3 Improve regex for container configurator
  • Loading branch information
fabpot committed May 6, 2019
2 parents 5bc24c9 + a3a22b3 commit 7728934
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Configurator/ContainerConfigurator.php
Expand Up @@ -32,7 +32,7 @@ public function unconfigure(Recipe $recipe, $parameters, Lock $lock)
$lines = [];
foreach (file($target) as $line) {
foreach (array_keys($parameters) as $key) {
if (preg_match("/^\s+$key\:/", $line)) {
if (preg_match(sprintf('/^\s+%s\:/', preg_quote($key, '/')), $line)) {
continue 2;
}
}
Expand Down Expand Up @@ -62,7 +62,7 @@ private function addParameters(array $parameters)
continue;
}
foreach ($parameters as $key => $value) {
if (preg_match("/^\s+$key\:/", $line)) {
if (preg_match(sprintf('/^\s+%s\:/', preg_quote($key, '/')), $line)) {
unset($parameters[$key]);
}
}
Expand Down
43 changes: 43 additions & 0 deletions tests/Configurator/ContainerConfiguratorTest.php
Expand Up @@ -192,4 +192,47 @@ public function testConfigureWithComplexContent()
EOF
, file_get_contents($config));
}

public function testConfigureWithEnvVariable()
{
$recipe = $this->getMockBuilder(Recipe::class)->disableOriginalConstructor()->getMock();
$lock = $this->getMockBuilder(Lock::class)->disableOriginalConstructor()->getMock();
$config = FLEX_TEST_DIR.'/config/services.yaml';
file_put_contents(
$config,
<<<EOF
# comment
parameters:
env(APP_ENV): ''
services:
EOF
);
$configurator = new ContainerConfigurator(
$this->getMockBuilder(Composer::class)->getMock(),
$this->getMockBuilder(IOInterface::class)->getMock(),
new Options(['config-dir' => 'config', 'root-dir' => FLEX_TEST_DIR])
);
$configurator->configure($recipe, ['env(APP_ENV)' => ''], $lock);
$this->assertEquals(<<<EOF
# comment
parameters:
env(APP_ENV): ''
services:
EOF
, file_get_contents($config));

$configurator->unconfigure($recipe, ['env(APP_ENV)' => ''], $lock);
$this->assertEquals(<<<EOF
# comment
parameters:
services:
EOF
, file_get_contents($config));
}
}

0 comments on commit 7728934

Please sign in to comment.