Skip to content

Commit e64f6fb

Browse files
[2.x] Compile @servers When Array is in Multiple Lines (#270)
* add. code, tests & ignoring .idea folder * add new test validation * fixing styleci * fixing styleci (2)
1 parent 3b216f2 commit e64f6fb

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
composer.lock
33
/phpunit.xml
44
.phpunit.result.cache
5+
.idea

src/Compiler.php

+4
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,10 @@ public function compileInclude($value)
271271
*/
272272
protected function compileServers($value)
273273
{
274+
$value = preg_replace_callback('/@servers\(\[(.*?)\]\)/s', function ($matches) {
275+
return '@servers(['.preg_replace('/\s+/', '', $matches[1]).'])';
276+
}, $value);
277+
274278
$pattern = $this->createMatcher('servers');
275279

276280
return preg_replace($pattern, '$1<?php $__container->servers$2; ?>', $value);

tests/CompilerTest.php

+46
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,50 @@ public function test_compile_before_statement()
3232

3333
$this->assertSame(1, preg_match('/\$__container->before\(.*?\}\);/s', $result, $matches));
3434
}
35+
36+
public function test_it_compiles_server_statement()
37+
{
38+
$str = <<<'EOL'
39+
@servers([
40+
'foo' => 'bar'
41+
])
42+
EOL;
43+
$compiler = new Compiler();
44+
$result = $compiler->compile($str);
45+
46+
$this->assertSame($result, "<?php \$__container->servers(['foo'=>'bar']); ?>");
47+
48+
$str = <<<'EOL'
49+
@servers([
50+
'foo' => [
51+
'bar',
52+
'baz',
53+
'bah'
54+
]
55+
])
56+
EOL;
57+
$compiler = new Compiler();
58+
$result = $compiler->compile($str);
59+
60+
$this->assertSame($result, "<?php \$__container->servers(['foo'=>['bar','baz','bah']]); ?>");
61+
62+
$str = <<<'EOL'
63+
@servers([
64+
'foo' => ['bar'],
65+
'bar' => ['baz']
66+
])
67+
EOL;
68+
$compiler = new Compiler();
69+
$result = $compiler->compile($str);
70+
71+
$this->assertSame($result, "<?php \$__container->servers(['foo'=>['bar'],'bar'=>['baz']]); ?>");
72+
73+
$str = <<<'EOL'
74+
@servers(['foo' => 'bar'])
75+
EOL;
76+
$compiler = new Compiler();
77+
$result = $compiler->compile($str);
78+
79+
$this->assertSame($result, "<?php \$__container->servers(['foo'=>'bar']); ?>");
80+
}
3581
}

0 commit comments

Comments
 (0)