Files
kiri-core/HttpServer/Events/Pipeline.php
T

72 lines
1.3 KiB
PHP
Raw Normal View History

2021-04-09 02:45:33 +08:00
<?php
namespace HttpServer\Events;
class Pipeline
{
2021-04-09 02:49:18 +08:00
private \Closure $_if;
private \Closure $_else;
private \Closure $_catch;
2021-04-09 02:45:33 +08:00
private bool $condition;
/**
* @param bool $condition
* @param \Closure $handler
* @return $this
*/
public function if(bool $condition, \Closure $handler): static
{
$this->condition = $condition;
2021-04-09 02:49:18 +08:00
$this->_if = $handler;
2021-04-09 02:45:33 +08:00
return $this;
}
/**
* @param \Closure $handler
* @return $this
*/
public function else(\Closure $handler): static
{
2021-04-09 02:49:18 +08:00
$this->_else = $handler;
2021-04-09 02:45:33 +08:00
return $this;
}
/**
* @param \Closure $handler
* @return $this
*/
public function catch(\Closure $handler): static
{
2021-04-09 02:49:18 +08:00
$this->_catch = $handler;
2021-04-09 02:45:33 +08:00
return $this;
}
/**
* @param $argv
* @return mixed
*/
public function exec(...$argv)
{
try {
2021-04-09 02:47:17 +08:00
if ($this->condition !== true) {
2021-04-09 02:49:18 +08:00
call_user_func($this->_else, ...$argv);
2021-04-09 02:45:33 +08:00
} else {
2021-04-09 02:49:18 +08:00
call_user_func($this->_if, ...$argv);
2021-04-09 02:45:33 +08:00
}
return $argv;
} catch (\Throwable $exception) {
2021-04-09 02:49:18 +08:00
call_user_func($this->_catch, $exception);
2021-04-09 02:45:33 +08:00
return $argv;
}
}
}