このライブラリは、ビジネスロジックをシンプルで強力な定義で保存するために使用されます。
PHPのミニスクリプト言語。 3つの簡単なタスクを実行します。
例えば :
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 " );Composerを使用してインストールします。
作曲家には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 $ specialcom = []、$ areaname = []、$ serviceclass = null)
以前の定義は、変数、サービス、領域をリセットします。
発信者オブジェクトを設定します。発信者オブジェクトそれは、スクリプト内で呼び出される方法を備えたサービスクラスである可能性があります。
システムで使用されている変数で辞書を設定します。
それはミニランに式を送り、その部分に分解されます。スクリプトは実行されませんが、解析されます。
ロジックを評価します。それはtrueまたはfalseを返します。
値または値を設定します。どこにあるかどうかは考慮していません。
ブール。
文字列の配列。 $ throwerrorの場合、すべてのエラーがここに保存されます。
例:
$ this -> throwError = false ;
$ mini -> separate ( " when FIELDDOESNOTEXIST=1 then field2=2 " );
var_dump ( $ this -> errorLog );コードの構文は4つの部分に分けられます。 init、where(or)、set(or)など。
例:
$ mini -> separate ( " when field1=1 then field2=2 " );field1 = 1の場合、field2を2に設定します。
変数はvarnameで定義されます
例:Examples/ExampleVariable.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
コードの例の例/exampleVariable2.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
例コード:例/exampleglobal.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 |
| 弦 | 「HelloWorld」、「HelloWorld」 |
| stringp | 「私の名前は{{var}}です」 |
| 関数 | namefunction(arg、arg) |
var = 20およびvar2 = "hello"およびvar3 = "hello {{var}}"およびvar4 = fn()を設定します
| 予約済みの単語 | 説明 |
|---|---|
| null() | ヌル値 |
| 間違い() | 偽の値 |
| 真実() | 真の値 |
| の上() | 1 |
| param(var、 'l1.l2.l3') | 配列(var)をvar ['l1'] ['l2'] ['l3']に分離します |
| オフ() | 0 |
| undef() | -1(未定義の場合) |
| flip() | (特別価値)。 <->オフに値を反転させます value = flip()として使用 |
| 今() | 現在のタイムスタンプ(整数)を返します |
| タイマー() | 現在のタイムスタンプ(整数)を返します |
| 間隔() | 最後の変更と今の間に間隔(秒単位)を返します。コールバッククラスのフィールドDateLastChangeまたはメソッドDatelaStchange()を使用します |
| fullinterval() | プロセスの開始から現在間隔(秒単位)を返します。コールバッククラスのフィールドdateInitまたはmethoddateinit()を使用します |
| contains()/str_contains() | テキストが別のテキストに含まれている場合にtrueを返します。例:str_contains(field1、 'hi') |
| str_starts_with()、startwith() | テキストが別のテキストで始まる場合、trueを返します |
| str_ends_with()、endwith() | テキストが別のテキストで終了する場合、trueを返します。 |
例:Examples/examPlererved.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 );サンプルタイマー:Examples/exampleResedtimer.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ここで、variable1 = 20 Set variable+counter
$ 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. 表現のこの部分は、声明に条件を追加します。
「いつ」を使用することもできます。
ここで表現
または
表現時
"and" or "or"で分離することにより、同時に条件以上のものを比較することができます。
ここで、V1 = 10およびV2 = 20またはV3 <50
ここで、variable1 = 20および$ variable2 = variable3またはfunction(20)= 40
ここで、$ field = 20およびfield2 <> 40またはfield3 = 40 // sql構文
ここで、$ field == 20 && field2!= 40 || Field3 =+40 // PHP Syntax
ここで、1 //それは常に真実です
式のこの部分により、変数の値を設定できます。 「、」、「または」で分離することにより、複数の変数を同時に設定することができます。
「セット」または「それから」という式を使用することもできます。
式を設定します
または
次に、式
式のこの部分は、有効な場所でのみ実行されます
次の式を使用して変数を設定できます。
このライブラリは、次のような複雑な指示を許可していません
set variable1 = 20および$ variable2 = variable3およびfunction(20)= 40を設定します
$ mini -> separate ( " when condition=1 then field1+10 " ); // if condition is 1 then it increases the field1 by 10. 式のこのオプションの部分により、変数の値を設定できます。 「、「、」、「、」を分離することにより、複数の変数を同時に設定することができます。
このコードは、「場合」が他のものが手動で呼ばれている場合にfalseを返す場合にのみ評価されます。
else variable1 = 20および$ variable2 = variable3およびfunction(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 (); 言語で作成されたロジックを使用してクラスを作成することが可能です。目標は、コードのパフォーマンスを向上させることです。
クラスを生成するには、まず、個別()の代わりにメソッドdepart2()を使用してロジックを記述する必要があります。クラスのインスタンスの配列内にロジックを保存します。コードを直接使用することも、次のようにクラス内で保存することもできます。
// 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(例を確認できますか?例/genclass/1.examplebasic.php)
クラスを生成すると、 Minilangの代わりにこの新しいクラスを使用できます。このクラスはすでに編集されているため、速く燃えています。ただし、ロジックを変更する必要がある場合は、再度コンパイルする必要があります。 (例を確認できますか?example/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ここで、各メソッドは式の一部を評価します。
例/examplebenchmark.php
いくつかの操作を1000回呼び出します。
PHPで新しいスクリプト言語を中編成します