Skip to content

Commit

Permalink
Update. new version.
Browse files Browse the repository at this point in the history
  • Loading branch information
dim-s committed Aug 21, 2018
1 parent 7da9df6 commit 9222c3a
Show file tree
Hide file tree
Showing 12 changed files with 208 additions and 51 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ language: java
sudo: required

jdk:
- oraclejdk10
- oraclejdk8

before_install:
- wget -O - https://github.com/jphp-compiler/jphp/releases/download/jppm-0.2.7/jppm-setup-0.2.7.sh | bash
- wget -O - https://github.com/jphp-compiler/jphp/releases/download/jppm-0.3.8/jppm-setup-0.3.8.sh | bash

script:
- jppm install
Expand Down
5 changes: 3 additions & 2 deletions api-docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
---

## twig
> version 1.0.0, created by JPPM.
> version 1.0.2, created by JPPM.
Twig template engine for JPHP

### Install
```
jppm add twig@1.0.0
jppm add twig@1.0.2
```

### API
Expand All @@ -22,4 +22,5 @@ jppm add twig@1.0.0
- [`TwigEngine`](https://github.com/jphp-group/twig/blob/master/api-docs/classes/twig/TwigEngine.md)- _Class TwigEngine_
- [`TwigException`](https://github.com/jphp-group/twig/blob/master/api-docs/classes/twig/TwigException.md)- _Class TwigException_
- [`TwigExtension`](https://github.com/jphp-group/twig/blob/master/api-docs/classes/twig/TwigExtension.md)- _Class TwigExtension_
- [`TwigSafeString`](https://github.com/jphp-group/twig/blob/master/api-docs/classes/twig/TwigSafeString.md)- _Value for raw text in templates._
- [`TwigTemplate`](https://github.com/jphp-group/twig/blob/master/api-docs/classes/twig/TwigTemplate.md)
5 changes: 3 additions & 2 deletions api-docs/README.ru.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
---

## twig
> версия 1.0.0, создано с помощью JPPM.
> версия 1.0.2, создано с помощью JPPM.
Twig template engine for JPHP

### Установка
```
jppm add twig@1.0.0
jppm add twig@1.0.2
```

### АПИ
Expand All @@ -22,4 +22,5 @@ jppm add twig@1.0.0
- [`TwigEngine`](https://github.com/jphp-group/twig/blob/master/api-docs/classes/twig/TwigEngine.ru.md)- _Class TwigEngine_
- [`TwigException`](https://github.com/jphp-group/twig/blob/master/api-docs/classes/twig/TwigException.ru.md)- _Class TwigException_
- [`TwigExtension`](https://github.com/jphp-group/twig/blob/master/api-docs/classes/twig/TwigExtension.ru.md)- _Class TwigExtension_
- [`TwigSafeString`](https://github.com/jphp-group/twig/blob/master/api-docs/classes/twig/TwigSafeString.ru.md)- _Value for raw text in templates._
- [`TwigTemplate`](https://github.com/jphp-group/twig/blob/master/api-docs/classes/twig/TwigTemplate.ru.md)
36 changes: 36 additions & 0 deletions api-docs/classes/twig/TwigSafeString.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# TwigSafeString

- **class** `TwigSafeString` (`twig\TwigSafeString`)
- **source** `twig/TwigSafeString.php`

**Description**

Value for raw text in templates.

---

#### Methods

- `->`[`__construct()`](#method-__construct) - _TwigSafeString constructor._
- `->`[`__toString()`](#method-__tostring) - _Returns a text of safe string_

---
# Methods

<a name="method-__construct"></a>

### __construct()
```php
__construct(string $text): void
```
TwigSafeString constructor.

---

<a name="method-__tostring"></a>

### __toString()
```php
__toString(): string
```
Returns a text of safe string
36 changes: 36 additions & 0 deletions api-docs/classes/twig/TwigSafeString.ru.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# TwigSafeString

- **класс** `TwigSafeString` (`twig\TwigSafeString`)
- **исходники** `twig/TwigSafeString.php`

**Описание**

Value for raw text in templates.

---

#### Методы

- `->`[`__construct()`](#method-__construct) - _TwigSafeString constructor._
- `->`[`__toString()`](#method-__tostring) - _Returns a text of safe string_

---
# Методы

<a name="method-__construct"></a>

### __construct()
```php
__construct(string $text): void
```
TwigSafeString constructor.

---

<a name="method-__tostring"></a>

### __toString()
```php
__toString(): string
```
Returns a text of safe string
9 changes: 7 additions & 2 deletions package.php.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: twig
version: 1.0.1
version: 1.0.2
description: Twig template engine for JPHP

plugins:
Expand Down Expand Up @@ -35,13 +35,18 @@ config:

gradle:
deps:
- io.pebbletemplates:pebble:3.0.0-SNAPSHOT
- io.pebbletemplates:pebble:3.0.2

doc:
url-prefix: https://github.com/jphp-group/twig/blob/master/api-docs/
langs:
en: English
ru: Русский

history:
1.0.2:
- Add able to return raw text from filters and functions.
- Upgrade pebble lib version.

tester:
#jvm-args: ['-agentlib:jdwp=transport=dt_socket,server=n,address=localhost:5005,suspend=y,onuncaught=n']
25 changes: 25 additions & 0 deletions sdk/twig/TwigSafeString.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
namespace twig;

/**
* Value for raw text in templates.
* @package twig
*/
class TwigSafeString
{
/**
* TwigSafeString constructor.
* @param string $text
*/
public function __construct(string $text)
{
}

/**
* Returns a text of safe string
* @return string
*/
public function __toString(): string
{
}
}
3 changes: 3 additions & 0 deletions src-jvm/main/java/php/pkg/twig/TwigExtension.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
import com.mitchellbosecke.pebble.error.ParserException;
import com.mitchellbosecke.pebble.error.PebbleException;
import com.mitchellbosecke.pebble.error.RootAttributeNotFoundException;
import com.mitchellbosecke.pebble.extension.escaper.SafeString;
import com.mitchellbosecke.pebble.template.PebbleTemplate;
import php.pkg.twig.classes.TwigEngine;
import php.pkg.twig.classes.TwigException;
import php.pkg.twig.classes.TwigSafeString;
import php.pkg.twig.classes.TwigTemplate;
import php.pkg.twig.classes.TwigTemplateExtension;
import php.runtime.env.CompileScope;
Expand All @@ -31,5 +33,6 @@ public void onRegister(CompileScope scope) {
registerWrapperClass(scope, PebbleTemplate.class, TwigTemplate.class);
registerClass(scope, TwigEngine.class);
registerClass(scope, TwigTemplateExtension.class);
registerWrapperClass(scope, SafeString.class, TwigSafeString.class);
}
}
30 changes: 30 additions & 0 deletions src-jvm/main/java/php/pkg/twig/classes/TwigSafeString.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package php.pkg.twig.classes;

import com.mitchellbosecke.pebble.extension.escaper.SafeString;
import php.pkg.twig.TwigExtension;
import php.runtime.annotation.Reflection.Namespace;
import php.runtime.annotation.Reflection.Signature;
import php.runtime.env.Environment;
import php.runtime.lang.BaseWrapper;
import php.runtime.reflection.ClassEntity;

@Namespace(TwigExtension.NS)
public class TwigSafeString extends BaseWrapper<SafeString> {
public TwigSafeString(Environment env, SafeString wrappedObject) {
super(env, wrappedObject);
}

public TwigSafeString(Environment env, ClassEntity clazz) {
super(env, clazz);
}

@Signature
public void __construct(String str) {
__wrappedObject = new SafeString(str);
}

@Signature
public String __toString() {
return getWrappedObject().toString();
}
}
22 changes: 20 additions & 2 deletions src-jvm/main/java/php/pkg/twig/classes/TwigTemplateExtension.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.mitchellbosecke.pebble.extension.Filter;
import com.mitchellbosecke.pebble.extension.Function;
import com.mitchellbosecke.pebble.extension.Test;
import com.mitchellbosecke.pebble.extension.escaper.SafeString;
import com.mitchellbosecke.pebble.template.EvaluationContext;
import com.mitchellbosecke.pebble.template.PebbleTemplate;

Expand Down Expand Up @@ -87,7 +88,13 @@ public void addFilter(Environment env, String name, Invoker filter, @Arg(type =
@Override
public Object apply(Object o, Map<String, Object> map,
PebbleTemplate pebbleTemplate, EvaluationContext evaluationContext, int i) throws PebbleException {
return filter.callAny(o, convertArgs(env, map), new TwigTemplate(env, pebbleTemplate), i);
Memory memory = filter.callAny(o, convertArgs(env, map), new TwigTemplate(env, pebbleTemplate), i);

if (memory.instanceOf(TwigSafeString.class)) {
return memory.toObject(TwigSafeString.class).getWrappedObject();
}

return memory;
}

@Override
Expand All @@ -109,7 +116,13 @@ public void addFunction(Environment env, String name, Invoker function, @Arg(typ
functionMap.put(name, new Function() {
@Override
public Object execute(Map<String, Object> map, PebbleTemplate pebbleTemplate, EvaluationContext evaluationContext, int i) {
return function.callAny(convertArgs(env, map), new TwigTemplate(env, pebbleTemplate), i);
Memory memory = function.callAny(convertArgs(env, map), new TwigTemplate(env, pebbleTemplate), i);

if (memory.instanceOf(TwigSafeString.class)) {
return memory.toObject(TwigSafeString.class).getWrappedObject();
}

return memory;
}

@Override
Expand Down Expand Up @@ -143,6 +156,11 @@ public List<String> getArgumentNames() {

@Signature
public void addGlobalVar(String name, Memory value) {
if (value.instanceOf(TwigSafeString.class)) {
globalVarMap.put(name, value.toObject(TwigSafeString.class).getWrappedObject());
return;
}

globalVarMap.put(name, value);
}
}
72 changes: 31 additions & 41 deletions src-jvm/main/java/php/pkg/twig/support/JPHPExtension.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,44 +30,39 @@ public List<AttributeResolver> getAttributeResolver() {
}

private ResolvedAttribute resolve(IObject instance, String attributeName, Object[] argumentValues, String filename, int lineNumber) {
return new ResolvedAttribute() {
@Override
public Object evaluate() {
try {
if (argumentValues == null) {
Memory memory = env.getObjectProperty(instance, attributeName.toString());

if (memory.isNull() && !attributeName.startsWith("get")) {
String methodName = "get" + attributeName.toLowerCase();

MethodEntity method = instance.getReflection().findMethod(methodName);

if (method != null && !method.isAbstractable() && !method.isStatic()) {
return method.invokeDynamic(instance, env, env.trace());
}
}

return Memory.unwrap(env, memory);
} else {
return Memory.unwrap(env, instance.callMethodAny(env, attributeName, argumentValues));
}
} catch (Throwable e) {
if (e instanceof BaseError) {
PebbleException exception = new PebbleException(
e,
((BaseError)e).getMessage(env).toString(),
lineNumber,
filename
);

throw exception;
}
try {
if (argumentValues == null) {
Memory memory = env.getObjectProperty(instance, attributeName.toString());

env.forwardThrow(e);
return null;
if (memory.isNull() && !attributeName.startsWith("get")) {
String methodName = "get" + attributeName.toLowerCase();

MethodEntity method = instance.getReflection().findMethod(methodName);

if (method != null && !method.isAbstractable() && !method.isStatic()) {
return new ResolvedAttribute(method.invokeDynamic(instance, env, env.trace()));
}
}

return new ResolvedAttribute(Memory.unwrap(env, memory));
} else {
return new ResolvedAttribute(Memory.unwrap(env, instance.callMethodAny(env, attributeName, argumentValues)));
}
} catch (Throwable e) {
if (e instanceof BaseError) {
PebbleException exception = new PebbleException(
e,
((BaseError)e).getMessage(env).toString(),
lineNumber,
filename
);

throw exception;
}
};

env.forwardThrow(e);
return null;
}
}

private ResolvedAttribute resolve(Memory instance, String attributeName, Object[] argumentValues, String filename, int lineNumber) {
Expand All @@ -78,12 +73,7 @@ private ResolvedAttribute resolve(Memory instance, String attributeName, Object[
return null;
}

return new ResolvedAttribute() {
@Override
public Object evaluate() {
return Memory.unwrap(env, instance.valueOfIndex(attributeName));
}
};
return new ResolvedAttribute(Memory.unwrap(env, instance.valueOfIndex(attributeName)));
}
}

Expand Down
Loading

0 comments on commit 9222c3a

Please sign in to comment.