Skip to content

Commit

Permalink
feat: support OGNL syntax in expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
vicvolk committed Oct 19, 2022
1 parent f4b994a commit 73beb05
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .phpunit.result.cache
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"version":1,"defects":{"Tests\\JuelEngineTest::testEngine":4,"Tests\\JuelEngineTest::testEngineManager":5,"Tests\\JuelEngineTest::testEngineFactory":4,"Tests\\JuelEngineTest::testJuelEngineManager":4,"Tests\\ExpressionLanguageTest::testNumericMethods":4,"Tests\\ExpressionLanguageTest::testMethodInvocation":4,"Tests\\JuelEngineTest::testJuelEngineManagerWithFunction":4,"Tests\\LuaEngineTest::testLuaScript":5},"times":{"Tests\\JuelEngineTest::testEngine":0.003,"Tests\\JuelEngineTest::testEngineFactory":0.006,"Tests\\JuelEngineTest::testEngineManager":0.003,"Tests\\JuelEngineTest::testJuelEngineManager":0.001,"Tests\\ExpressionLanguageTest::testNumericVariables":0.006,"Tests\\ExpressionLanguageTest::testNumericMethods":0,"Tests\\ExpressionLanguageTest::testBooleanMethods":0,"Tests\\ExpressionLanguageTest::testMethodInvocation":0.001,"Tests\\ExpressionLanguageTest::testExpressionString":0,"Tests\\ExpressionLanguageTest::testIsDeferred":0,"Tests\\ExpressionLanguageTest::testGetExpectedType":0,"Tests\\ExpressionLanguageTest::testGetType":0,"Tests\\JuelEngineTest::testJuelEngineManagerWithVariable":0.001,"Tests\\JuelEngineTest::testJuelEngineManagerWithFunction":0.001,"Tests\\LuaEngineTest::testLuaScript":0}}
{"version":1,"defects":{"Tests\\JuelEngineTest::testEngine":4,"Tests\\JuelEngineTest::testEngineManager":5,"Tests\\JuelEngineTest::testEngineFactory":4,"Tests\\JuelEngineTest::testJuelEngineManager":4,"Tests\\ExpressionLanguageTest::testNumericMethods":4,"Tests\\ExpressionLanguageTest::testMethodInvocation":4,"Tests\\JuelEngineTest::testJuelEngineManagerWithFunction":3,"Tests\\LuaEngineTest::testLuaScript":5,"Tests\\JuelEngineTest::testOgnlSyntaxInJuelExpression":4},"times":{"Tests\\JuelEngineTest::testEngine":0.003,"Tests\\JuelEngineTest::testEngineFactory":0.021,"Tests\\JuelEngineTest::testEngineManager":0.003,"Tests\\JuelEngineTest::testJuelEngineManager":0.001,"Tests\\ExpressionLanguageTest::testNumericVariables":0.006,"Tests\\ExpressionLanguageTest::testNumericMethods":0,"Tests\\ExpressionLanguageTest::testBooleanMethods":0,"Tests\\ExpressionLanguageTest::testMethodInvocation":0.001,"Tests\\ExpressionLanguageTest::testExpressionString":0,"Tests\\ExpressionLanguageTest::testIsDeferred":0,"Tests\\ExpressionLanguageTest::testGetExpectedType":0,"Tests\\ExpressionLanguageTest::testGetType":0,"Tests\\JuelEngineTest::testJuelEngineManagerWithVariable":0.002,"Tests\\JuelEngineTest::testJuelEngineManagerWithFunction":0.002,"Tests\\LuaEngineTest::testLuaScript":0,"Tests\\JuelEngineTest::testOgnlSyntaxInJuelExpression":0.005}}
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
},
"require": {
"php": "^7.4 || ^8.0",
"bingo-soft/el": "^1.0",
"bingo-soft/juel": "^1.0"
"bingo-soft/el": "^1.1",
"bingo-soft/juel": "^1.2",
"bingo-soft/util": "^1.3"
},
"require-dev": {
"doctrine/coding-standard": "*",
Expand Down
2 changes: 1 addition & 1 deletion src/AbstractScriptEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public function put(string $key, $value): void
{
$nn = $this->getBindings(ScriptContextInterface::ENGINE_SCOPE);
if ($nn !== null) {
$nn->put($key, $value);
$nn->put($key, $value);
}
}

Expand Down
13 changes: 12 additions & 1 deletion src/Engine/Juel/JuelScriptEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
SimpleBindings,
SimpleScriptContext
};
use Util\Reflection\MetaObject;

class JuelScriptEngine extends AbstractScriptEngine
{
Expand Down Expand Up @@ -71,10 +72,20 @@ public function createBindings(): BindingsInterface
return new SimpleBindings();
}

public function put(string $key, $value): void
{
parent::put($key, $value);
if ($value instanceof MetaObject) {
$context = $this->createElContext($this->context);
$context->getELResolver()->setValue($context, null, $key, $value);
}
}

public function evaluateExpression(ValueExpression $expr, ScriptContextInterface $ctx)
{
try {
return $expr->getValue($this->createElContext($ctx));
$context = $this->createElContext($ctx);
return $expr->getValue($context);
} catch (ELException $elexp) {
throw new ScriptException($elexp);
}
Expand Down
15 changes: 15 additions & 0 deletions tests/JuelEngineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
ExpressionFactoryImpl,
SimpleContext
};
use Util\Reflection\MetaObject;

class JuelEngineTest extends TestCase
{
Expand Down Expand Up @@ -63,4 +64,18 @@ public function bar(): int
$this->assertEquals(3.23, $engine->eval('${simple.propFloat + 2}'));
$this->assertEquals(34, $engine->eval('${simple.bar() + simple.foo()}'));
}

public function testOgnlSyntaxInJuelExpression(): void
{
$rich1 = new RichType();
$meta1 = new MetaObject($rich1);
$meta1->setValue("richType.richType.richField", 10);


$manager = new ScriptEngineManager();
$engine = $manager->getEngineByName("juel");
$engine->put("parameterObject", $meta1);

$this->assertEquals(21, $engine->eval('${richType.richType.richField + 11}'));
}
}
52 changes: 52 additions & 0 deletions tests/RichType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace Tests;

class RichType
{
private int $richField;
private $richType;
private $richProperty;
private $richMap = [];
private $richList = ["bar"];

public function getRichType(): RichType
{
return $this->richType;
}

public function setRichType(RichType $richType): void
{
$this->richType = $richType;
}

public function getRichProperty(): string
{
return $this->richProperty;
}

public function setRichProperty(string $richProperty): void
{
$this->richProperty = $richProperty;
}

public function getRichList(): array
{
return $this->richList;
}

public function setRichList(array $richList): void
{
$this->richList = $richList;
}

public function getRichMap(): array
{
return $this->richMap;
}

public function setRichMap(array $richMap): void
{
$this->richMap = $richMap;
}
}

0 comments on commit 73beb05

Please sign in to comment.