1. Go to this page and download the library: Download ant-framework/middleware 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/ */
ant-framework / middleware example snippets
use Ant\Middleware\Pipeline;
use Ant\Middleware\Arguments;
yield;
echo 321;
},
function($str1){
echo 456;
yield new Arguments($str1,'world'); //改变在中间件中传递的变量
echo 654;
}
];
echo (new Pipeline) //实例中间件
->send('hello') //要通过中间件变量
->through($nodes) //使用的中间件,必须为可回调类型(callable类型)
->then(function($str1,$str2){ //挂起所有中间件之后回调的函数
return "($str1 $str2)";
});
// output "123456(hello world)654321"
/////////////////// PHP7 ///////////////////
use Ant\Middleware\Pipeline;
use Ant\Middleware\Arguments;
eturn改变传递参数
}
];
echo (new Pipeline)
->send('hello')
->through($nodes)
->then(function($hello){
return $hello;
});
// output "hello world"
use Ant\Middleware\Pipeline;
use Ant\Middleware\Arguments;
;
echo 321;
},
function(){
echo 456;
yield false;
echo 654;
},
];
(new Pipeline)->send()->through($nodes)->then(function(){
echo 'hello world';
});
//output "123456"
use Ant\Middleware\Pipeline;
use Ant\Middleware\Arguments;
}catch(Exception $e){
echo "Catch an exception in method 1 , which is thrown by the ".$e->getMessage();
}
},
function(){
try{
yield;
}catch(RuntimeException $e){
// 此处无法处理方法3抛出的异常,所以跳过了
echo "Catch an exception in method 2 , which is thrown by the ".$e->getMessage();
}
},
function(){
throw new Exception('method 3');
},
];
(new Pipeline)->send()->through($nodes)->then(function(){
echo 'hello world';
});
//output "Catch an exception in method 1 , which is thrown by the method 3"