Skip to content

Commit 9222c3a

Browse files
committed
Update. new version.
1 parent 7da9df6 commit 9222c3a

File tree

12 files changed

+208
-51
lines changed

12 files changed

+208
-51
lines changed

.travis.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ language: java
22
sudo: required
33

44
jdk:
5+
- oraclejdk10
56
- oraclejdk8
67

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

1011
script:
1112
- jppm install

api-docs/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
---
44

55
## twig
6-
> version 1.0.0, created by JPPM.
6+
> version 1.0.2, created by JPPM.
77
88
Twig template engine for JPHP
99

1010
### Install
1111
```
12-
12+
1313
```
1414

1515
### API
@@ -22,4 +22,5 @@ jppm add [email protected]
2222
- [`TwigEngine`](https://github.com/jphp-group/twig/blob/master/api-docs/classes/twig/TwigEngine.md)- _Class TwigEngine_
2323
- [`TwigException`](https://github.com/jphp-group/twig/blob/master/api-docs/classes/twig/TwigException.md)- _Class TwigException_
2424
- [`TwigExtension`](https://github.com/jphp-group/twig/blob/master/api-docs/classes/twig/TwigExtension.md)- _Class TwigExtension_
25+
- [`TwigSafeString`](https://github.com/jphp-group/twig/blob/master/api-docs/classes/twig/TwigSafeString.md)- _Value for raw text in templates._
2526
- [`TwigTemplate`](https://github.com/jphp-group/twig/blob/master/api-docs/classes/twig/TwigTemplate.md)

api-docs/README.ru.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
---
44

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

1010
### Установка
1111
```
12-
12+
1313
```
1414

1515
### АПИ
@@ -22,4 +22,5 @@ jppm add [email protected]
2222
- [`TwigEngine`](https://github.com/jphp-group/twig/blob/master/api-docs/classes/twig/TwigEngine.ru.md)- _Class TwigEngine_
2323
- [`TwigException`](https://github.com/jphp-group/twig/blob/master/api-docs/classes/twig/TwigException.ru.md)- _Class TwigException_
2424
- [`TwigExtension`](https://github.com/jphp-group/twig/blob/master/api-docs/classes/twig/TwigExtension.ru.md)- _Class TwigExtension_
25+
- [`TwigSafeString`](https://github.com/jphp-group/twig/blob/master/api-docs/classes/twig/TwigSafeString.ru.md)- _Value for raw text in templates._
2526
- [`TwigTemplate`](https://github.com/jphp-group/twig/blob/master/api-docs/classes/twig/TwigTemplate.ru.md)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# TwigSafeString
2+
3+
- **class** `TwigSafeString` (`twig\TwigSafeString`)
4+
- **source** `twig/TwigSafeString.php`
5+
6+
**Description**
7+
8+
Value for raw text in templates.
9+
10+
---
11+
12+
#### Methods
13+
14+
- `->`[`__construct()`](#method-__construct) - _TwigSafeString constructor._
15+
- `->`[`__toString()`](#method-__tostring) - _Returns a text of safe string_
16+
17+
---
18+
# Methods
19+
20+
<a name="method-__construct"></a>
21+
22+
### __construct()
23+
```php
24+
__construct(string $text): void
25+
```
26+
TwigSafeString constructor.
27+
28+
---
29+
30+
<a name="method-__tostring"></a>
31+
32+
### __toString()
33+
```php
34+
__toString(): string
35+
```
36+
Returns a text of safe string
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# TwigSafeString
2+
3+
- **класс** `TwigSafeString` (`twig\TwigSafeString`)
4+
- **исходники** `twig/TwigSafeString.php`
5+
6+
**Описание**
7+
8+
Value for raw text in templates.
9+
10+
---
11+
12+
#### Методы
13+
14+
- `->`[`__construct()`](#method-__construct) - _TwigSafeString constructor._
15+
- `->`[`__toString()`](#method-__tostring) - _Returns a text of safe string_
16+
17+
---
18+
# Методы
19+
20+
<a name="method-__construct"></a>
21+
22+
### __construct()
23+
```php
24+
__construct(string $text): void
25+
```
26+
TwigSafeString constructor.
27+
28+
---
29+
30+
<a name="method-__tostring"></a>
31+
32+
### __toString()
33+
```php
34+
__toString(): string
35+
```
36+
Returns a text of safe string

package.php.yml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: twig
2-
version: 1.0.1
2+
version: 1.0.2
33
description: Twig template engine for JPHP
44

55
plugins:
@@ -35,13 +35,18 @@ config:
3535

3636
gradle:
3737
deps:
38-
- io.pebbletemplates:pebble:3.0.0-SNAPSHOT
38+
- io.pebbletemplates:pebble:3.0.2
3939

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

46+
history:
47+
1.0.2:
48+
- Add able to return raw text from filters and functions.
49+
- Upgrade pebble lib version.
50+
4651
tester:
4752
#jvm-args: ['-agentlib:jdwp=transport=dt_socket,server=n,address=localhost:5005,suspend=y,onuncaught=n']

sdk/twig/TwigSafeString.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
namespace twig;
3+
4+
/**
5+
* Value for raw text in templates.
6+
* @package twig
7+
*/
8+
class TwigSafeString
9+
{
10+
/**
11+
* TwigSafeString constructor.
12+
* @param string $text
13+
*/
14+
public function __construct(string $text)
15+
{
16+
}
17+
18+
/**
19+
* Returns a text of safe string
20+
* @return string
21+
*/
22+
public function __toString(): string
23+
{
24+
}
25+
}

src-jvm/main/java/php/pkg/twig/TwigExtension.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
import com.mitchellbosecke.pebble.error.ParserException;
66
import com.mitchellbosecke.pebble.error.PebbleException;
77
import com.mitchellbosecke.pebble.error.RootAttributeNotFoundException;
8+
import com.mitchellbosecke.pebble.extension.escaper.SafeString;
89
import com.mitchellbosecke.pebble.template.PebbleTemplate;
910
import php.pkg.twig.classes.TwigEngine;
1011
import php.pkg.twig.classes.TwigException;
12+
import php.pkg.twig.classes.TwigSafeString;
1113
import php.pkg.twig.classes.TwigTemplate;
1214
import php.pkg.twig.classes.TwigTemplateExtension;
1315
import php.runtime.env.CompileScope;
@@ -31,5 +33,6 @@ public void onRegister(CompileScope scope) {
3133
registerWrapperClass(scope, PebbleTemplate.class, TwigTemplate.class);
3234
registerClass(scope, TwigEngine.class);
3335
registerClass(scope, TwigTemplateExtension.class);
36+
registerWrapperClass(scope, SafeString.class, TwigSafeString.class);
3437
}
3538
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package php.pkg.twig.classes;
2+
3+
import com.mitchellbosecke.pebble.extension.escaper.SafeString;
4+
import php.pkg.twig.TwigExtension;
5+
import php.runtime.annotation.Reflection.Namespace;
6+
import php.runtime.annotation.Reflection.Signature;
7+
import php.runtime.env.Environment;
8+
import php.runtime.lang.BaseWrapper;
9+
import php.runtime.reflection.ClassEntity;
10+
11+
@Namespace(TwigExtension.NS)
12+
public class TwigSafeString extends BaseWrapper<SafeString> {
13+
public TwigSafeString(Environment env, SafeString wrappedObject) {
14+
super(env, wrappedObject);
15+
}
16+
17+
public TwigSafeString(Environment env, ClassEntity clazz) {
18+
super(env, clazz);
19+
}
20+
21+
@Signature
22+
public void __construct(String str) {
23+
__wrappedObject = new SafeString(str);
24+
}
25+
26+
@Signature
27+
public String __toString() {
28+
return getWrappedObject().toString();
29+
}
30+
}

src-jvm/main/java/php/pkg/twig/classes/TwigTemplateExtension.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.mitchellbosecke.pebble.extension.Filter;
66
import com.mitchellbosecke.pebble.extension.Function;
77
import com.mitchellbosecke.pebble.extension.Test;
8+
import com.mitchellbosecke.pebble.extension.escaper.SafeString;
89
import com.mitchellbosecke.pebble.template.EvaluationContext;
910
import com.mitchellbosecke.pebble.template.PebbleTemplate;
1011

@@ -87,7 +88,13 @@ public void addFilter(Environment env, String name, Invoker filter, @Arg(type =
8788
@Override
8889
public Object apply(Object o, Map<String, Object> map,
8990
PebbleTemplate pebbleTemplate, EvaluationContext evaluationContext, int i) throws PebbleException {
90-
return filter.callAny(o, convertArgs(env, map), new TwigTemplate(env, pebbleTemplate), i);
91+
Memory memory = filter.callAny(o, convertArgs(env, map), new TwigTemplate(env, pebbleTemplate), i);
92+
93+
if (memory.instanceOf(TwigSafeString.class)) {
94+
return memory.toObject(TwigSafeString.class).getWrappedObject();
95+
}
96+
97+
return memory;
9198
}
9299

93100
@Override
@@ -109,7 +116,13 @@ public void addFunction(Environment env, String name, Invoker function, @Arg(typ
109116
functionMap.put(name, new Function() {
110117
@Override
111118
public Object execute(Map<String, Object> map, PebbleTemplate pebbleTemplate, EvaluationContext evaluationContext, int i) {
112-
return function.callAny(convertArgs(env, map), new TwigTemplate(env, pebbleTemplate), i);
119+
Memory memory = function.callAny(convertArgs(env, map), new TwigTemplate(env, pebbleTemplate), i);
120+
121+
if (memory.instanceOf(TwigSafeString.class)) {
122+
return memory.toObject(TwigSafeString.class).getWrappedObject();
123+
}
124+
125+
return memory;
113126
}
114127

115128
@Override
@@ -143,6 +156,11 @@ public List<String> getArgumentNames() {
143156

144157
@Signature
145158
public void addGlobalVar(String name, Memory value) {
159+
if (value.instanceOf(TwigSafeString.class)) {
160+
globalVarMap.put(name, value.toObject(TwigSafeString.class).getWrappedObject());
161+
return;
162+
}
163+
146164
globalVarMap.put(name, value);
147165
}
148166
}

0 commit comments

Comments
 (0)