Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[2.x] Compile @servers When Array is in Multiple Lines #270

Merged
Merged
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
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']); ?>");
}
}