From 86f0cb1cf2a39b30d56825bbcf2581eb76cfeb2d Mon Sep 17 00:00:00 2001 From: Dmitriy Zayceff Date: Tue, 21 Aug 2018 17:25:23 +0300 Subject: [PATCH] Fixes. --- api-docs/README.md | 4 +-- api-docs/README.ru.md | 4 +-- package.php.yml | 4 ++- .../php/pkg/twig/classes/TwigTemplate.java | 13 ++++++++ .../php/pkg/twig/support/JPHPExtension.java | 6 ++-- tests/BasicTest.php | 30 +++++++++++++++++++ 6 files changed, 53 insertions(+), 8 deletions(-) diff --git a/api-docs/README.md b/api-docs/README.md index b41397f..6ba73fb 100644 --- a/api-docs/README.md +++ b/api-docs/README.md @@ -3,13 +3,13 @@ --- ## twig -> version 1.0.2, created by JPPM. +> version 1.0.3, created by JPPM. Twig template engine for JPHP ### Install ``` -jppm add twig@1.0.2 +jppm add twig@1.0.3 ``` ### API diff --git a/api-docs/README.ru.md b/api-docs/README.ru.md index a0bfc06..7cfc6a2 100644 --- a/api-docs/README.ru.md +++ b/api-docs/README.ru.md @@ -3,13 +3,13 @@ --- ## twig -> версия 1.0.2, создано с помощью JPPM. +> версия 1.0.3, создано с помощью JPPM. Twig template engine for JPHP ### Установка ``` -jppm add twig@1.0.2 +jppm add twig@1.0.3 ``` ### АПИ diff --git a/package.php.yml b/package.php.yml index bd03b5b..f9ac515 100644 --- a/package.php.yml +++ b/package.php.yml @@ -1,5 +1,5 @@ name: twig -version: 1.0.2 +version: 1.0.3 description: Twig template engine for JPHP plugins: @@ -44,6 +44,8 @@ doc: ru: Русский history: + 1.0.3: + - Fix bug of getting value in forin loop. 1.0.2: - Add able to return raw text from filters and functions. - Upgrade pebble lib version. diff --git a/src-jvm/main/java/php/pkg/twig/classes/TwigTemplate.java b/src-jvm/main/java/php/pkg/twig/classes/TwigTemplate.java index b7fbc7e..032640d 100644 --- a/src-jvm/main/java/php/pkg/twig/classes/TwigTemplate.java +++ b/src-jvm/main/java/php/pkg/twig/classes/TwigTemplate.java @@ -8,7 +8,9 @@ import java.io.StringWriter; import java.util.HashMap; import java.util.Map; +import java.util.Map.Entry; import php.pkg.twig.TwigExtension; +import php.runtime.Memory; import php.runtime.annotation.Reflection.Getter; import php.runtime.annotation.Reflection.Namespace; import php.runtime.annotation.Reflection.Signature; @@ -44,9 +46,18 @@ public String render(Environment env) throws IOException { return render(env, new HashMap<>()); } + protected void prepareContext(Environment env, Map contexts) { + for (Entry entry : contexts.entrySet()) { + if (entry.getValue() instanceof Memory) { + contexts.put(entry.getKey(), Memory.unwrap(env, (Memory) entry.getValue(), true)); + } + } + } + @Signature public String render(Environment env, Map contexts) throws IOException { StringWriter writer = new StringWriter(); + prepareContext(env, contexts); getWrappedObject().evaluate(writer, contexts, WrapLocale.getDefault(env)); return writer.toString(); } @@ -59,6 +70,8 @@ public String renderBlock(Environment env, String block) throws IOException { @Signature public String renderBlock(Environment env, String block, Map contexts) throws IOException { StringWriter writer = new StringWriter(); + prepareContext(env, contexts); + getWrappedObject().evaluateBlock(block, writer, contexts, WrapLocale.getDefault(env)); return writer.toString(); } diff --git a/src-jvm/main/java/php/pkg/twig/support/JPHPExtension.java b/src-jvm/main/java/php/pkg/twig/support/JPHPExtension.java index 5e72d39..19e65b8 100644 --- a/src-jvm/main/java/php/pkg/twig/support/JPHPExtension.java +++ b/src-jvm/main/java/php/pkg/twig/support/JPHPExtension.java @@ -44,9 +44,9 @@ private ResolvedAttribute resolve(IObject instance, String attributeName, Object } } - return new ResolvedAttribute(Memory.unwrap(env, memory)); + return new ResolvedAttribute(Memory.unwrap(env, memory.toImmutable(), true)); } else { - return new ResolvedAttribute(Memory.unwrap(env, instance.callMethodAny(env, attributeName, argumentValues))); + return new ResolvedAttribute(Memory.unwrap(env, instance.callMethodAny(env, attributeName, argumentValues).toImmutable(), true)); } } catch (Throwable e) { if (e instanceof BaseError) { @@ -73,7 +73,7 @@ private ResolvedAttribute resolve(Memory instance, String attributeName, Object[ return null; } - return new ResolvedAttribute(Memory.unwrap(env, instance.valueOfIndex(attributeName))); + return new ResolvedAttribute(Memory.unwrap(env, instance.valueOfIndex(attributeName).toImmutable(), true)); } } diff --git a/tests/BasicTest.php b/tests/BasicTest.php index 949de78..46a5972 100644 --- a/tests/BasicTest.php +++ b/tests/BasicTest.php @@ -110,4 +110,34 @@ public function testSafeString() Assert::isEqual('', $this->twig->renderString('{{ "" | safeString }}')); } + + public function testEquals() + { + Assert::isEqual('success', + $this->twig->renderString( + '{{ x == "1.2.3" ? "success" : "fail" }}', + ['x' => '1.2.3'] + ) + ); + } + + public function testArrayGetEquals() + { + Assert::isEqual('success', + $this->twig->renderString( + '{{ x[0] == y[0] ? "success" : "fail" }}', + ['x' => ['1.2.3'], 'y' => ['1.2.3']] + ) + ); + } + + public function testForInEquals() + { + Assert::isEqual('success', + $this->twig->renderString( + '{% for el in arr %}{{ el == "1.2.3" ? "success" : "fail" }}{% endfor %}', + ['arr' => ['1.2.3']] + ) + ); + } } \ No newline at end of file