该库用于以简单但功能强大的定义存储业务逻辑。
PHP的迷你脚本语言。它执行三个简单的任务。
例如 :
when var1>5 and var2>20 then var3=20 // when and then
init var5=20 when var1>5 and var2>20 then var3=var5 // init, when and then
init var5=20 when var1>5 and var2>20 then var3=var5 else var3=var20 // init, when, then and else
when var1>$abc then var3=var5 // $abc is a PHP variable.
有时,我们需要根据“某些值等于,然后设置或执行其他代码”来执行任意代码。
在PHP中,我们可以执行下一个代码。
if ( $ condition ) {
$ variable = 1 ;
}但是,此代码在运行时执行。如果我们需要在某个特定点执行此代码怎么办?
我们可以执行下一个代码:
$ script = ' if($condition) {
$variable=1;
} ' ;
// and later..
eval ( $ script );该解决方案有效(并且仅在我们调用命令评估时执行)。但这是冗长的,容易出错,而且很危险。
我们的图书馆做同样但安全和干净。
$ mini -> separate ( " when condition then variable=1 " );使用作曲家安装它:
作曲家需要EFTEC/Minilang
创建一个新项目
use eftec minilang MiniLang ;
include " ../lib/MiniLang.php " ; // or the right path to MiniLang.php
$ mini = new MiniLang ();
$ mini -> separate ( " when field1=1 then field2=2 " ); // we set the logic of the language but we are not executed it yet.
$ mini -> separate ( " when field1=2 then field2=4 " ); // we set more logic.
$ result =[ ' field1 ' => 1 , ' field2 ' => 0 ]; // used for variables.
$ callback = new stdClass (); // used for callbacks if any
$ mini -> evalAllLogic ( $ callback , $ result );
var_dump ( $ result );另一个例子:
use eftec minilang MiniLang ;
include " ../lib/MiniLang.php " ;
$ result =[ ' var1 ' => ' hello ' ];
$ global1 = " hello " ;
$ mini = new MiniLang ( null , $ result );
$ mini -> throwError = false ; // if error then we store the errors in $mini->errorLog;
$ mini -> separate ( ' when var1="hello" then var2="world" ' ); // if var1 is equals "hello" then var2 is set "world"
$ mini -> separate ( ' when $global1="hello" then $global2="world" ' ); // we can also use php variables (global)
$ mini -> evalAllLogic ( false ); // false means it continues to evaluate more expressions if any
// (true means that it only evaluates the first expression where "when" is valid)
var_dump ( $ result ); // array(2) { ["var1"]=> string(5) "hello" ["var2"]=> string(5) "world" }
var_dump ( $ global2 ); // string(5) "world" __construct(&$ caller,&$ dict,array $ speartycom = [],$ areaname = [],$ serviceClass = null)
它重置了先前的定义,但变量,服务和区域。
设置呼叫者对象。呼叫者对象可能是一个可以在脚本中调用的方法的服务类。
设置一个使用系统使用的变量的字典。
它向Minilang发送表达式,并在其各个部分分解。该脚本不是执行的,而是被解析。
它评估逻辑。它返回对还是错。
它设置一个或值。它不考虑在哪里是正确的。
布尔。
字符串数组。如果$ thowerror为false,则在此处存储每个错误。
例子:
$ this -> throwError = false ;
$ mini -> separate ( " when FIELDDOESNOTEXIST=1 then field2=2 " );
var_dump ( $ this -> errorLog );代码的语法分为四个部分。初始化,位置(或何时),设置(或随后)等。
例子:
$ mini -> separate ( " when field1=1 then field2=2 " );它说如果field1 = 1,则我们将field2设置为2。
变量由varname定义
示例:示例/示例Variable.php
$ mini = new MiniLang ();
$ mini -> separate ( " when field1>0 then field2=3 " ); // we prepare the language
$ variables =[ ' field1 ' => 1 ]; // we define regular variables
$ callback = new stdClass ();
$ mini -> evalAllLogic ( $ callback , $ variables ); // we set the variables and run the languageand run the language
var_dump ( $ variables ); // field1=1, field2=3变量可以托管PHP对象,并且可以调用并访问其内部的字段。
varname.field
代码示例/示例variable2.php的示例
class MyModel {
var $ id = 1 ;
var $ value = "" ;
public function __construct ( $ id = 0 , $ value = "" )
{
$ this -> id = $ id ;
$ this -> value = $ value ;
}
}
class ClassCaller {
public function Processcaller ( $ arg ) {
echo " Caller: setting the variable { $ arg -> id } <br> " ;
}
}
class ClassService {
public function ProcessService ( $ arg ) {
echo " Service: setting the variable { $ arg -> id } <br> " ;
}
}
$ mini = new MiniLang ([],[], new ClassService ());
$ mini -> separate ( " when field1.id>0 then
field2.value=3
and field3.processcaller
and processcaller(field3)
and processservice(field3) " ); // we prepare the language
$ variables =[ ' field1 ' => new MyModel ( 1 , " hi " )
, ' field2 ' => new MyModel ( 2 , '' )
, ' field3 ' => new MyModel ( 3 , '' )]; // we define regular variables
$ callback = new ClassCaller ();
$ mini -> evalAllLogic ( $ callback , $ variables , false ); // we set the variables and run the languageand run the language
var_dump ( $ variables );变量可以容纳关联/索引数组,并且可以读取和访问其内部的元素。
例子:
$ mini = new MiniLang ( null ,
[
' vararray ' =>[ ' associindex ' => ' hi ' , 0 => ' a ' , 1 => ' b ' , 2 => ' c ' , 3 => ' d ' , 4 => ' last ' , ' a ' =>[ ' b ' =>[ ' c ' => ' nested ' ]]]
]
);vararray.associndex // vararray['associindex'] ('hi')
vararray. 4 // vararray[4] 'last'
vararray. 123 // it will throw an error (out of index)
vararray. param ( ' a.b.c ' )) // vararray['a']['b']['c'] ('nested')
param (vararray, ' a.b.c ' )) // vararray['a']['b']['c'] ('nested')
vararray._first // first element ('hi')
vararray._last // last element ('last')
vararray._count // returns the number of elements. (6)代码示例/examplevariable_arr.php的示例
class ClassCaller {
public function Processcaller ( $ arg ) {
echo " Caller: setting the variable { $ arg [ ' id ' ]} <br> " ;
}
}
class ClassService {
public function ProcessService ( $ arg ) {
echo " Service: setting the variable { $ arg [ ' id ' ]} <br> " ;
}
}
$ mini = new MiniLang ([],[], new ClassService ());
$ mini -> separate ( " when field1.id>0 then
field2.value=3
and field3.processcaller
and processcaller(field3)
and processservice(field3) " );
$ variables =[ ' field1 ' =>[ ' id ' => 1 , ' value ' => 3 ]
, ' field2 ' =>[ ' id ' => 2 , ' value ' => '' ]
, ' field3 ' =>[ ' id ' => 3 , ' value ' => '' ]
];
$ callback = new ClassCaller ();
$ mini -> evalAllLogic ( $ callback , $ variables , false );
var_dump ( $ variables );全局变量采用PHP的值($ global),因此不需要在语言中定义或设置它
注意:出于安全目的。 PHP定义为“ $ _namevar”定义的全局变量无法读取或修改。因此,如果要保护全局变量,则可以将其重命名为前缀。
示例:如果您尝试读取变量$ _server,则它将返回可以或未定义的变量$服务器的值。
全局变量由
$globalname
$ globalname .associndex // $globalname['associindex']
$ globalname . 4 // $globalname[4]
$ globalname . param ( ' a.b.c ' ) // $globalname['a']['b']['c']
param ( $ globalname , ' a.b.c ' ) // $globalname['a']['b']['c']例子:
$globalname=30
示例代码:示例/示例Global.php
$ field1 = 1 ; // our global variable
$ mini = new MiniLang ();
$ mini -> separate ( ' when $field1>0 then $field1=3 ' ); // we prepare the language
$ variables =[]; // local variables
$ callback = new stdClass ();
$ mini -> evalAllLogic ( $ callback , $ variables ); // we set the variables and run the languageand run the language
var_dump ( $ field1 ); // returns 3 | 类型 | 例子 |
|---|---|
| 数字 | 20 |
| 细绳 | “你好世界”,'你好世界' |
| 字符串 | “我的名字是{{var}}” |
| 功能 | 名称功能(arg,arg) |
set var = 20 and var2 =“ hello”和var3 =“ hello {{var}}”和var4 = fn()
| 保留的单词 | 解释 |
|---|---|
| 无效的() | 空值 |
| 错误的() | 假值 |
| 真的() | 真值 |
| 在() | 1 |
| param(var,'l1.l2.l3') | 将数组(var)分离为var ['l1'] ['l2'] ['l3'] |
| 离开() | 0 |
| undef() | -1(对于未定义) |
| 翻动() | (特殊价值)。它会反转<-> off的价值 用作值= flip() |
| 现在() | 返回当前时间戳(整数) |
| 计时器() | 返回当前时间戳(整数) |
| 间隔() | 返回最后一个更改之间的间隔(以秒为单位)。它使用回调类的字段datelastchange或方法datelastchange() |
| fullInterval() | 在流程的开始和现在之间返回(以秒为单位)的间隔(以秒为单位)。它使用回调类的字段日期或方法dateinit() |
| contains()/str_contains() | 如果文本包含在另一个文本中,则返回true。示例:str_contains(field1,'hi') |
| str_starts_with(),startwith() | 如果文本以另一个文字开头,则返回true |
| str_ends_with(),endWith() | 如果文本以其他文本结尾,则返回true。 |
示例:示例/eskepleresvered.php
$ mini = new MiniLang ();
$ mini -> separate ( " when true=true then field1=timer() " ); // we prepare the language
$ variables =[ ' field1 ' => 1 ]; // we define regular variables
$ callback = new stdClass ();
$ mini -> evalAllLogic ( $ callback , $ variables ); // we set the variables and run the language
var_dump ( $ variables );示例计时器:示例/eskepleresveredTimer.php
class ClassWithTimer {
var $ dateLastChange ;
public function dateInit () {
return time ();
}
public function __construct ()
{
$ this -> dateLastChange = time ();
}
}
$ mini = new MiniLang ();
$ mini -> separate ( " when true=true then field1=interval() and field2=fullinterval() " ); // we prepare the language
$ variables =[ ' field1 ' => 0 , ' field2 ' => 0 ]; // we define regular variables
$ callback = new ClassWithTimer ();
$ mini -> evalAllLogic ( $ callback , $ variables ); // we set the variables and run the language
var_dump ( $ variables );表达式的这一部分允许设置一个值。此表达通常是可选的,并且可以省略。
它与设置相似,但是在哪里和无论何处是否有效之前执行。
init counter = 20 ,其中变量1 = 20集变量+计数器
$ mini -> separate ( " init tmp=50 when condition=1 then field1+10 " ); // set tmp to 50. If condition is 1 then it increases the field1 by 10. 表达的这一部分为语句增加了条件。
我们也可以使用“何时”。
在哪里表达
或者
表达时
通过“和”或“或”分离,可以同时比较不仅仅是条件。
其中v1 = 10和v2 = 20或v3 <50
其中变量1 = 20和$ variable2 = variable3或函数(20)= 40
其中$ field = 20和field2 <> 40或field3 = 40 // sql语法
其中$ field == 20 && field2!= 40 || field3 =+40 // php语法
其中1 //总是如此
表达式的这一部分允许设置变量的值。可以通过“”,“和”和“。”同时设置多个变量。
我们还可以使用表达式“集”或“然后”
设置表达式
或者
然后表达
表达的这一部分仅在何处有效时执行
我们可以使用下一个表达式设置变量:
该库不允许像
设置变量1 = 20和$ variable2 =变量3和函数(20)= 40
$ mini -> separate ( " when condition=1 then field1+10 " ); // if condition is 1 then it increases the field1 by 10. 表达式的可选部分允许设置变量的值。可以通过“”,“和”和“分开,可以同时设置多个变量。
仅当“ where”返回false时,该代码才会被评估。
else variable1 = 20和$ variable2 = variable3和函数(20)= 40
可以使用空间“循环”创建一个循环
要开始循环,您必须写
$ this -> separate ( ' loop variableloop=variable_with_values ' );
// where variable_with_values is must contains an array of values
// variableloop._key will contain the key of the loop
// variableloop._value will contain the value of the loop 为了结束循环,您必须使用
$ this -> separate ( ' loop end ' );您可以使用“集合”或“ else”中的操作员“断路”逃脱循环。
$ this -> separate ( ' when condition set break else break ' );注意:仅在评估所有逻辑时才能评估循环。它不适用于evallogic()和evallogic2()
注意:您不能在循环中添加条件,但是您可以跳过分配空数组的循环
例子:
$ this -> separate ( ' loop $row=variable ' );
$ this -> separate ( ' loop $row2=variable ' );
$ this -> separate ( ' where op=2 then cc=2 ' );
$ this -> separate ( ' where op=3 then break ' ); // ends of the first loop
$ this -> separate ( ' loop end ' );
$ this -> separate ( ' loop end ' )
$ obj -> evalAllLogic (); 可以用语言创建的逻辑创建类。目标是提高代码的性能。
要生成类,首先,我们需要使用方法独立2()而不是单独()编写逻辑。它将将逻辑存储在类实例的数组中。您可以直接使用代码,也可以如下保存在类中:
// create an instance of MiniLang
$ mini = new MiniLang ( null );
// the logic goes here
$ mini -> separate2 ( ' when var1="hello" and comp.f=false() then var2="world" ' ); // if var1 is equals "hello" then var2 is set "world"
// and the generation of the class
$ r = $ mini -> generateClass ( ' ExampleBasicClass ' , ' nsexample ' , ' ExampleBasicClass.php ' );它会保存一个名为的新文件吗? exampleBasicClass.php(您可以检查示例?example/genclass/1.examplebasic.php)
通过生成的类,您可以使用此新类而不是Minilang 。由于该课程已经编译,因此它正在快速燃烧。但是,如果您需要更改逻辑,则需要再次编译它。 (您可以检查示例?示例/genclass/2.examplebasic.php和example/genclass/examplebasicclass.php)
$ result =[ ' var1 ' => ' hello ' ];
$ obj = new ExampleBasicClass ( null , $ result );
$ obj -> evalAllLogic ( true );班级看起来像:
<?php
namespace ns example ;
use eftec minilang MiniLang ;
class ExampleBasicClass extends MiniLang {
public $ numCode = 2 ; // num of lines of code
public $ usingClass = true ; // if true then we are using a class (this class)
public function whereRun ( $ lineCode = 0 ) {
// ...
} // end function WhereRun
public function setRun ( $ lineCode = 0 ) {
// ...
} // end function SetRun
public function elseRun ( $ lineCode = 0 ) {
// ...
} // end function ElseRun
public function initRun ( $ lineCode = 0 ) {
// ...
} // end function InitRun
} // end class每个方法都评估表达的一部分。
示例/示例benchmark.php
我们致电一些操作1000次。
在PHP上培养一种新的脚本语言