Skip to content

Commit

Permalink
[2.x] Compile @servers When Array is in Multiple Lines (#270)
Browse files Browse the repository at this point in the history
* add. code, tests & ignoring .idea folder

* add new test validation

* fixing styleci

* fixing styleci (2)
  • Loading branch information
devajmeireles committed Jan 3, 2024
1 parent 3b216f2 commit e64f6fb
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
composer.lock
/phpunit.xml
.phpunit.result.cache
.idea
4 changes: 4 additions & 0 deletions src/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,10 @@ public function compileInclude($value)
*/
protected function compileServers($value)
{
$value = preg_replace_callback('/@servers\(\[(.*?)\]\)/s', function ($matches) {
return '@servers(['.preg_replace('/\s+/', '', $matches[1]).'])';
}, $value);

$pattern = $this->createMatcher('servers');

return preg_replace($pattern, '$1<?php $__container->servers$2; ?>', $value);
Expand Down
46 changes: 46 additions & 0 deletions tests/CompilerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,50 @@ public function test_compile_before_statement()

$this->assertSame(1, preg_match('/\$__container->before\(.*?\}\);/s', $result, $matches));
}

public function test_it_compiles_server_statement()
{
$str = <<<'EOL'
@servers([
'foo' => 'bar'
])
EOL;
$compiler = new Compiler();
$result = $compiler->compile($str);

$this->assertSame($result, "<?php \$__container->servers(['foo'=>'bar']); ?>");

$str = <<<'EOL'
@servers([
'foo' => [
'bar',
'baz',
'bah'
]
])
EOL;
$compiler = new Compiler();
$result = $compiler->compile($str);

$this->assertSame($result, "<?php \$__container->servers(['foo'=>['bar','baz','bah']]); ?>");

$str = <<<'EOL'
@servers([
'foo' => ['bar'],
'bar' => ['baz']
])
EOL;
$compiler = new Compiler();
$result = $compiler->compile($str);

$this->assertSame($result, "<?php \$__container->servers(['foo'=>['bar'],'bar'=>['baz']]); ?>");

$str = <<<'EOL'
@servers(['foo' => 'bar'])
EOL;
$compiler = new Compiler();
$result = $compiler->compile($str);

$this->assertSame($result, "<?php \$__container->servers(['foo'=>'bar']); ?>");
}
}

0 comments on commit e64f6fb

Please sign in to comment.