該庫用於以簡單但功能強大的定義存儲業務邏輯。
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上培養一種新的腳本語言