ห้องสมุดนี้ใช้เพื่อจัดเก็บตรรกะทางธุรกิจในคำจำกัดความที่เรียบง่ายและมีประสิทธิภาพ
ภาษาสคริปต์ขนาดเล็กสำหรับ 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 );โซลูชันนี้ใช้งานได้ (และจะดำเนินการเฉพาะถ้าเราเรียกคำสั่ง eval) แต่มันเป็นคำฟาโรห์มีแนวโน้มที่จะเกิดข้อผิดพลาดและมันอันตราย
ห้องสมุดของเราทำเช่นเดียวกัน แต่ปลอดภัยและสะอาด
$ 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 $ specialcom = [], $ areaname = [], $ serviceClass = null)
มันรีเซ็ตคำจำกัดความก่อนหน้า แต่ตัวแปรบริการและพื้นที่
ตั้งค่าวัตถุผู้โทร วัตถุผู้โทรอาจเป็นคลาสบริการที่มีวิธีการที่พวกเขาสามารถเรียกได้ภายในสคริปต์
ตั้งค่าพจนานุกรมด้วยตัวแปรที่ใช้โดยระบบ
มันส่งการแสดงออกไปยัง minilang และมันถูกย่อยสลายในส่วนของมัน สคริปต์ไม่ได้ดำเนินการ แต่แยกวิเคราะห์
มันประเมินตรรกะ มันส่งคืนจริงหรือเท็จ
มันตั้งค่าค่าหรือค่า ไม่พิจารณาว่าอยู่ที่ไหนจริงหรือไม่
บูลีน
อาร์เรย์ของสตริง หาก $ throwerror เป็นเท็จทุกข้อผิดพลาดจะถูกเก็บไว้ที่นี่
ตัวอย่าง:
$ this -> throwError = false ;
$ mini -> separate ( " when FIELDDOESNOTEXIST=1 then field2=2 " );
var_dump ( $ this -> errorLog );ไวยากรณ์ของรหัสถูกแยกออกเป็นสี่ส่วน init โดยที่ (หรือเมื่อ) ตั้งค่า (หรือจากนั้น) และอื่น ๆ
ตัวอย่าง:
$ mini -> separate ( " when field1=1 then field2=2 " );มันบอกว่าถ้า field1 = 1 จากนั้นเราตั้งค่า Field2 เป็น 2
ตัวแปรถูกกำหนดโดย varname
ตัวอย่าง: ตัวอย่าง/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 |
| สาย | "Hello World", 'Hello World' |
| สตริง | "ชื่อของฉันคือ {{var}}" |
| การทำงาน | NameFunction (arg, arg) |
ตั้งค่า var = 20 และ var2 = "hello" และ var3 = "สวัสดี {{var}}" และ var4 = fn ()
| คำที่สงวนไว้ | คำอธิบาย |
|---|---|
| โมฆะ() | ค่าว่าง |
| เท็จ() | ค่าเท็จ |
| จริง() | ค่าที่แท้จริง |
| บน() | 1 |
| param (var, 'l1.l2.l3') | แยกอาร์เรย์ (var) เป็น var ['l1'] ['l2'] ['l3'] |
| ปิด() | 0 |
| undef () | -1 (สำหรับไม่ได้กำหนด) |
| พลิก () | (ค่าพิเศษ) มันกลับค่าค่าใช้จ่ายใน <-> ปิด ใช้เป็นค่า = flip () |
| ตอนนี้() | ส่งคืนการประทับเวลาปัจจุบัน (จำนวนเต็ม) |
| ตัวจับเวลา () | ส่งคืนการประทับเวลาปัจจุบัน (จำนวนเต็ม) |
| ช่วงเวลา () | ส่งคืนช่วงเวลา (เป็นวินาที) ระหว่างการเปลี่ยนแปลงครั้งสุดท้ายและตอนนี้ ใช้ DatelastChange หรือ Method DatelastChange () ของคลาสการโทรกลับ |
| fullinterval () | ส่งคืนช่วงเวลา (เป็นวินาที) ระหว่างการเริ่มต้นของกระบวนการและตอนนี้ ใช้ฟิลด์ dateInit หรือวิธีการ dateInit () ของคลาสการโทรกลับ |
| มี ()/str_contains () | ส่งคืนจริงถ้าข้อความมีอยู่ในข้อความอื่นตัวอย่าง: str_contains (field1, 'hi') |
| str_starts_with (), startwith () | ส่งคืนจริงถ้าข้อความเริ่มต้นด้วยข้อความอื่น |
| str_ends_with (), endwith () | ส่งคืนจริงถ้าข้อความลงท้ายด้วยข้อความอื่น |
ตัวอย่าง: ตัวอย่าง/Examplereserved.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 );ตัวอย่างตัวจับเวลา: ตัวอย่าง/ExamplereservedTimer.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
โดยที่ Variable1 = 20 และ $ variable2 = Variable3 หรือฟังก์ชั่น (20) = 40
โดยที่ $ field = 20 และ field2 <> 40 หรือ field3 = 40 // ไวยากรณ์ SQL
โดยที่ $ field == 20 && field2! = 40 || Field3 =+40 // ไวยากรณ์ PHP
โดยที่ 1 // มันเป็นความจริงเสมอ
ส่วนนี้ของนิพจน์อนุญาตให้ตั้งค่าของตัวแปร เป็นไปได้ที่จะตั้งค่าตัวแปรมากกว่าหนึ่งตัวในเวลาเดียวกันโดยแยกด้วย "," หรือ "และ."
นอกจากนี้เรายังสามารถใช้นิพจน์ "set" หรือ "จากนั้น"
ตั้ง ค่านิพจน์
หรือ
จากนั้น แสดงออก
ส่วนนี้ของนิพจน์นี้จะดำเนินการเฉพาะในกรณีที่ถูกต้อง
เราสามารถตั้งค่าตัวแปรโดยใช้นิพจน์ถัดไป:
ไลบรารีนี้ไม่อนุญาตให้มีคำสั่งที่ซับซ้อนเช่น
ตั้งค่าตัวแปร 1 = 20 และ $ variable2 = Variable3 และฟังก์ชัน (20) = 40
$ mini -> separate ( " when condition=1 then field1+10 " ); // if condition is 1 then it increases the field1 by 10. ส่วนเสริมของนิพจน์นี้อนุญาตให้ตั้งค่าของตัวแปร เป็นไปได้ที่จะตั้งค่าตัวแปรมากกว่าหนึ่งตัวในเวลาเดียวกันโดยแยกด้วย "," หรือ "และ"
รหัสนี้จะถูกประเมินเฉพาะถ้า "โดยที่" ส่งคืนเท็จหากเรียกด้วยตนเอง
ตัวแปรอื่น 1 = 20 และ $ variable2 = ตัวแปร 3 และฟังก์ชัน (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 ' );คุณสามารถหลบหนีลูปโดยใช้ "break" ของผู้ให้บริการใน "ชุด" หรือ "อื่น"
$ 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 (); เป็นไปได้ที่จะสร้างคลาสที่มีตรรกะที่สร้างขึ้นในภาษา เป้าหมายคือการเพิ่มประสิทธิภาพของรหัส
ในการสร้างคลาสก่อนอื่นเราต้องเขียนตรรกะโดยใช้ Method spect2 () แทนที่จะ แยก () มันจะเก็บตรรกะภายในอาร์เรย์ของอินสแตนซ์ของคลาส คุณสามารถใช้รหัสโดยตรงหรือคุณสามารถบันทึกภายในชั้นเรียนดังนี้:
// 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 ' );มันจะบันทึกไฟล์ใหม่ที่เรียกว่า? extrambasicclass.php (คุณสามารถตรวจสอบตัวอย่างได้หรือไม่ตัวอย่าง/genclass/1.examplebasic.php)
ด้วยคลาสที่สร้างขึ้นคุณสามารถใช้คลาสใหม่นี้แทน minilang เนื่องจากคลาสนี้ได้รวบรวมไว้แล้วจึงเป็นไปอย่างรวดเร็ว อย่างไรก็ตามหากคุณต้องการเปลี่ยนตรรกะคุณจะต้องรวบรวมอีกครั้ง (คุณสามารถตรวจสอบตัวอย่าง? ตัวอย่าง/genclass/2.examplebasic.php และ? ตัวอย่าง/genclass/extraffbasicclass.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
เราเรียกการดำเนินการบางอย่าง 1,000 ครั้ง
การสร้างภาษาสคริปต์ใหม่ใน PHP