PHP code example of iry / php-event

1. Go to this page and download the library: Download iry/php-event library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

iry / php-event example snippets



// MyNamespace\event\Setting为示例名称请修改自己的Class名称
iry\e\App::setCfg('\\MyNamespace\\event\\Setting');//参数为 Class带命名空间的全名称

//iry\e\App::setCfg(\MyNamespace\event\Setting::class);//如果php版本>= 5.5 也可以这样


namespace \MyNamespace\event;
class Setting implements \iry\e\interfaces\Setting {
   public function name(){return 'my_name'}
   public function getPoolDriver(){}
   public function getSubscribers(){} //return array [class1,class2,....]
   public function getEventRules(){return 'className';}
   public function getTempPath(){return sys_get_temp_dir();}

}

//启动守护进程
//argv: 'ls' / 'help' / 'show' / 'daemon' / '', default:''
iry\e\Service::start($argv);

use iry\e\Fire; //引用类

Fire::event('事件名',['参数1','更多参数...'],'延时广播 秒','依赖事件ID');
Fire::event('complete',[]);//常用方法
Fire::event('complete',[],10);//延时用法,10秒后广播事件消息
Fire::event('complete',[],0,5000);//消息ID为5000的广播确认完之后才会广播当前事件

use iry\e\Fire; //引用类

$fire = new Fire();
$fire ->start('beforeRequest',['参数1','...']);
    //... 你的业务代码
$fire ->then('afterRequest',['参数1','...']);
    //... 你的业务代码
$fire ->then('complete',['参数1','...']);
//执行顺序 beforeRequest > afterRequest > complete

$fire->getLastEventId();//获取最后一次事件的ID