From e64f6fbf941439cc52c24519a6ee24af68b47ccf Mon Sep 17 00:00:00 2001 From: AJ <60591772+devajmeireles@users.noreply.github.com> Date: Wed, 3 Jan 2024 19:43:22 -0300 Subject: [PATCH] [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) --- .gitignore | 1 + src/Compiler.php | 4 ++++ tests/CompilerTest.php | 46 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+) diff --git a/.gitignore b/.gitignore index 660fc15e..cc31cac1 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ composer.lock /phpunit.xml .phpunit.result.cache +.idea diff --git a/src/Compiler.php b/src/Compiler.php index 76af861e..c270caa9 100644 --- a/src/Compiler.php +++ b/src/Compiler.php @@ -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, '$1servers$2; ?>', $value); diff --git a/tests/CompilerTest.php b/tests/CompilerTest.php index 3ee66b77..2f0d0a36 100644 --- a/tests/CompilerTest.php +++ b/tests/CompilerTest.php @@ -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, "servers(['foo'=>'bar']); ?>"); + + $str = <<<'EOL' +@servers([ + 'foo' => [ + 'bar', + 'baz', + 'bah' + ] +]) +EOL; + $compiler = new Compiler(); + $result = $compiler->compile($str); + + $this->assertSame($result, "servers(['foo'=>['bar','baz','bah']]); ?>"); + + $str = <<<'EOL' +@servers([ + 'foo' => ['bar'], + 'bar' => ['baz'] +]) +EOL; + $compiler = new Compiler(); + $result = $compiler->compile($str); + + $this->assertSame($result, "servers(['foo'=>['bar'],'bar'=>['baz']]); ?>"); + + $str = <<<'EOL' +@servers(['foo' => 'bar']) +EOL; + $compiler = new Compiler(); + $result = $compiler->compile($str); + + $this->assertSame($result, "servers(['foo'=>'bar']); ?>"); + } }