decorator
v6.4.3
PHP 的 Python 風格decorator
PHP 8.1 或更高版本
composer require sagittaracc/php-python- decorator
運行一個方法需要多長時間。查看Timer
decorator
use Sagittaracc PhpPython decorator decorator ;
class Calc
{
use decorator ;
#[Timer]
function sum ( $ a , $ b )
{
sleep ( 1 );
return $ a + $ b ;
}
}
這就是你可以這樣稱呼它
$ calc = new Calc ();
echo call_ decorator _func_array ([ $ calc , ' sum ' ], [ 1 , 2 ]); // Total execution: 1.00034234 ms; Result: 3
或內聯
$ timerOnSum = ( new Timer )-> wrapper ( fn ( $ a , $ b ) => $ calc -> sum ( $ a , $ b ));
echo $ timerOnSum ( 1 , 2 ); // Total execution: 1.00034234 ms; Result: 3
use Sagittaracc PhpPython decorator decorator ;
use Sagittaracc PhpPython decorator modules generics aliases T ;
use Sagittaracc PhpPython decorator modules validation core validators ArrayOf ;
#[T]
class Box
{
use decorator ;
#[ArrayOf(T::class)]
public $ items ;
public function addItem (#[T] $ item )
{
$ this -> items [] = $ item ;
}
}
$ box = new Box ();
$ box (Pen::class); // new Box<Pen>();
call_ decorator _func_array ([ $ box , ' addItem ' ], [ new Pencil ]); // throws a GenericError
use Sagittaracc PhpPython decorator decorator ;
use Sagittaracc PhpPython decorator tests examples Progress ;
use Sagittaracc PhpPython decorator tests validators Length ;
use Sagittaracc PhpPython decorator tests validators SerializeOf ;
use Sagittaracc PhpPython decorator tests validators In ;
use Sagittaracc PhpPython decorator tests validators LessThan ;
use Sagittaracc PhpPython decorator tests validators UInt8 ;
class Progress
{
use decorator ;
#[UInt8]
public $ max ;
#[UInt8]
#[LessThan( ' max ' )]
public $ pos ;
#[In( ' progress ' , ' finish ' , ' aborted ' )]
public $ status ;
#[Length( 32 )]
public string $ caption ;
}
$ progress = new Progress ();
set_ decorator _prop ( $ progress , ' max ' , 255 ); // max uint8 - 255
set_ decorator _prop ( $ progress , ' pos ' , 100 ); // should be less than max
set_ decorator _prop ( $ progress , ' status ' , ' progress ' ); // status is one of possible cases (progress, finish or aborted)
set_ decorator _prop ( $ progress , ' caption ' , ' in progress ... ' ); // just a string (max length is 32)
use Sagittaracc PhpPython decorator decorator ;
use Sagittaracc PhpPython decorator modules rpc core Rpc ;
#[Rpc]
class Controller
{
use decorator ;
public function sum ( $ a , $ b )
{
return $ a + $ b ;
}
}
在index.php
中
$ requestBody = file_get_contents ( ' php://input ' );
$ controller = new Controller ();
$ controller ( $ requestBody );
在terminal
$ curl -d " { " id " :1, " method " : " sum " , " params " :[1,4]} " http://localhost:4000
{"json-rpc":"2.0","id":1,"result":5}
use Sagittaracc PhpPython decorator decorator ;
use Sagittaracc PhpPython decorator modules console core Console ;
class Controller
{
use decorator ;
#[Console( ' hello ' )]
function greetingPerson ( $ name )
{
return " Hello, $ name " ;
}
}
在命令列中,它會呼叫以下內容:
php index.php -c hello --name Yuriy
然後在index.php
中你應該讀取命令和參數,然後像這樣呼叫它:
( new Console ( ' hello ' ))-> setParameters ([ ' name ' => ' Yuriy ' ])-> getMethod (Controller::class)-> run ();