|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Jabe\Engine\Impl\Util; |
|
4
|
|
|
|
|
5
|
|
|
use Jabe\Engine\Delegate\ExpressionInterface; |
|
6
|
|
|
use Jabe\Engine\Exception\NotValidException; |
|
7
|
|
|
use Jabe\Engine\Impl\Cfg\ProcessEngineConfigurationImpl; |
|
8
|
|
|
use Jabe\Engine\Impl\Context\Context; |
|
9
|
|
|
use Jabe\Engine\Impl\El\ExpressionManager; |
|
10
|
|
|
use Jabe\Engine\Impl\Scripting\{ |
|
11
|
|
|
ExecutableScript, |
|
12
|
|
|
ScriptFactory |
|
13
|
|
|
}; |
|
14
|
|
|
use Jabe\Engine\Impl\Scripting\Engine\JuelScriptEngineFactory; |
|
15
|
|
|
use Jabe\Engine\Impl\Util\EnsureUtil; |
|
16
|
|
|
|
|
17
|
|
|
class ScriptUtil |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* Creates a new {@link ExecutableScript} from a source or resource. It excepts static and |
|
21
|
|
|
* dynamic sources and resources. Dynamic means that the source or resource is an expression |
|
22
|
|
|
* which will be evaluated during execution. |
|
23
|
|
|
* |
|
24
|
|
|
* @param language the language of the script |
|
25
|
|
|
* @param source the source code of the script or an expression which evaluates to the source code |
|
26
|
|
|
* @param resource the resource path of the script code or an expression which evaluates to the resource path |
|
27
|
|
|
* @param expressionManager the expression manager to use to generate the expressions of dynamic scripts |
|
28
|
|
|
* @return the newly created script |
|
29
|
|
|
* @throws NotValidException if language is null or empty or both of source and resource are null or empty |
|
30
|
|
|
*/ |
|
31
|
|
|
public static function getScript(string $language, string $source, ?string $resource, ?ExpressionManager $expressionManager): ExecutableScript |
|
32
|
|
|
{ |
|
33
|
|
|
$scriptFactory = self::getScriptFactory(); |
|
34
|
|
|
EnsureUtil::ensureNotEmpty(NotValidException::class, "Script language", $language); |
|
|
|
|
|
|
35
|
|
|
EnsureUtil::ensureAtLeastOneNotNull("No script source or resource was given", $source, $resource); |
|
36
|
|
|
if (!empty($resource)) { |
|
37
|
|
|
return self::getScriptFromResource($language, $resource, $expressionManager, $scriptFactory); |
|
|
|
|
|
|
38
|
|
|
} else { |
|
39
|
|
|
return self::getScriptFormSource($language, $source, $expressionManager, $scriptFactory); |
|
|
|
|
|
|
40
|
|
|
} |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Creates a new {@link ExecutableScript} from a source. It excepts static and dynamic sources. |
|
45
|
|
|
* Dynamic means that the source is an expression which will be evaluated during execution. |
|
46
|
|
|
* |
|
47
|
|
|
* @param language the language of the script |
|
48
|
|
|
* @param source the source code of the script or an expression which evaluates to the source code |
|
49
|
|
|
* @param expressionManager the expression manager to use to generate the expressions of dynamic scripts |
|
50
|
|
|
* @param scriptFactory the script factory used to create the script |
|
51
|
|
|
* @return the newly created script |
|
52
|
|
|
* @throws NotValidException if language is null or empty or source is null |
|
53
|
|
|
*/ |
|
54
|
|
|
public static function getScriptFromSource(string $language, string $source, ?ExpressionManager $expressionManager, ScriptFactory $scriptFactory): ExecutableScript |
|
55
|
|
|
{ |
|
56
|
|
|
EnsureUtil::ensureNotEmpty(NotValidException::class, "Script language", $language); |
|
|
|
|
|
|
57
|
|
|
EnsureUtil::ensureNotNull(NotValidException::class, "Script source", $source); |
|
58
|
|
|
if (self::isDynamicScriptExpression($language, $source)) { |
|
|
|
|
|
|
59
|
|
|
$sourceExpression = $expressionManager->createExpression($source); |
|
|
|
|
|
|
60
|
|
|
return self::getScriptFromSourceExpression($language, $sourceExpression, $scriptFactory); |
|
|
|
|
|
|
61
|
|
|
} else { |
|
62
|
|
|
EnsureUtil::ensureNotEmpty(NotValidException::class, "Script language", $language); |
|
63
|
|
|
EnsureUtil::ensureNotNull(NotValidException::class, "Script source", $source); |
|
64
|
|
|
return $scriptFactory->createScriptFromSource($language, $source); |
|
|
|
|
|
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Creates a new {@link ExecutableScript} from a dynamic source. Dynamic means that the source |
|
70
|
|
|
* is an expression which will be evaluated during execution. |
|
71
|
|
|
* |
|
72
|
|
|
* @param language the language of the script |
|
73
|
|
|
* @param sourceExpression the expression which evaluates to the source code |
|
74
|
|
|
* @param scriptFactory the script factory used to create the script |
|
75
|
|
|
* @return the newly created script |
|
76
|
|
|
* @throws NotValidException if language is null or empty or sourceExpression is null |
|
77
|
|
|
*/ |
|
78
|
|
|
public static function getScriptFromSourceExpression(string $language, ExpressionInterface $sourceExpression, ScriptFactory $scriptFactory): ExecutableScript |
|
79
|
|
|
{ |
|
80
|
|
|
EnsureUtil::ensureNotEmpty(NotValidException::class, "Script language", $language); |
|
|
|
|
|
|
81
|
|
|
EnsureUtil::ensureNotNull(NotValidException::class, "Script source expression", $sourceExpression); |
|
82
|
|
|
return $scriptFactory->createScriptFromSource($language, $sourceExpression); |
|
|
|
|
|
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Creates a new {@link ExecutableScript} from a resource. It excepts static and dynamic resources. |
|
87
|
|
|
* Dynamic means that the resource is an expression which will be evaluated during execution. |
|
88
|
|
|
* |
|
89
|
|
|
* @param language the language of the script |
|
90
|
|
|
* @param resource the resource path of the script code or an expression which evaluates to the resource path |
|
91
|
|
|
* @param expressionManager the expression manager to use to generate the expressions of dynamic scripts |
|
92
|
|
|
* @param scriptFactory the script factory used to create the script |
|
93
|
|
|
* @return the newly created script |
|
94
|
|
|
* @throws NotValidException if language or resource are null or empty |
|
95
|
|
|
*/ |
|
96
|
|
|
public static function getScriptFromResource(string $language, string $resource, ?ExpressionManager $expressionManager, ?ScriptFactory $scriptFactory): ExecutableScript |
|
97
|
|
|
{ |
|
98
|
|
|
EnsureUtil::ensureNotEmpty(NotValidException::class, "Script language", $language); |
|
|
|
|
|
|
99
|
|
|
EnsureUtil::ensureNotEmpty(NotValidException::class, "Script resource", $resource); |
|
100
|
|
|
if (self::isDynamicScriptExpression($language, $resource)) { |
|
|
|
|
|
|
101
|
|
|
$resourceExpression = $expressionManager->createExpression($resource); |
|
|
|
|
|
|
102
|
|
|
return self::getScriptFromResourceExpression($language, $resourceExpression, $scriptFactory); |
|
|
|
|
|
|
103
|
|
|
} else { |
|
104
|
|
|
EnsureUtil::ensureNotEmpty(NotValidException::class, "Script language", $language); |
|
105
|
|
|
EnsureUtil::ensureNotEmpty(NotValidException::class, "Script resource", $resource); |
|
106
|
|
|
return $scriptFactory->createScriptFromResource($language, $resource); |
|
|
|
|
|
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Creates a new {@link ExecutableScript} from a dynamic resource. Dynamic means that the source |
|
112
|
|
|
* is an expression which will be evaluated during execution. |
|
113
|
|
|
* |
|
114
|
|
|
* @param language the language of the script |
|
115
|
|
|
* @param resourceExpression the expression which evaluates to the resource path |
|
116
|
|
|
* @param scriptFactory the script factory used to create the script |
|
117
|
|
|
* @return the newly created script |
|
118
|
|
|
* @throws NotValidException if language is null or empty or resourceExpression is null |
|
119
|
|
|
*/ |
|
120
|
|
|
public static function getScriptFromResourceExpression(string $language, ExpressionInterface $resourceExpression, ScriptFactory $scriptFactory): ExecutableScript |
|
121
|
|
|
{ |
|
122
|
|
|
EnsureUtil::ensureNotEmpty(NotValidException::class, "Script language", $language); |
|
|
|
|
|
|
123
|
|
|
EnsureUtil::ensureNotNull(NotValidException::class, "Script resource expression", $resourceExpression); |
|
124
|
|
|
return $scriptFactory->createScriptFromResource($language, $resourceExpression); |
|
|
|
|
|
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Checks if the value is an expression for a dynamic script source or resource. |
|
129
|
|
|
* |
|
130
|
|
|
* @param language the language of the script |
|
131
|
|
|
* @param value the value to check |
|
132
|
|
|
* @return true if the value is an expression for a dynamic script source/resource, otherwise false |
|
133
|
|
|
*/ |
|
134
|
|
|
public static function isDynamicScriptExpression(?string $language, string $value): bool |
|
135
|
|
|
{ |
|
136
|
|
|
return StringUtil::isExpression($value) && $language != null && !in_array(strtolower($language), JuelScriptEngineFactory::$names); |
|
|
|
|
|
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Returns the configured script factory in the context or a new one. |
|
141
|
|
|
*/ |
|
142
|
|
|
public static function getScriptFactory(): ScriptFactory |
|
143
|
|
|
{ |
|
144
|
|
|
$processEngineConfiguration = Context::getProcessEngineConfiguration(); |
|
145
|
|
|
if ($processEngineConfiguration !== null) { |
|
146
|
|
|
return $processEngineConfiguration->getScriptFactory(); |
|
|
|
|
|
|
147
|
|
|
} else { |
|
148
|
|
|
return new ScriptFactory(); |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
|