2021-04-09 02:45:33 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace HttpServer\Events;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Pipeline
|
|
|
|
|
{
|
|
|
|
|
|
2021-04-09 03:03:41 +08:00
|
|
|
private ?\Closure $_if = null;
|
|
|
|
|
private ?\Closure $_else = null;
|
|
|
|
|
private ?\Closure $_catch = null;
|
|
|
|
|
private ?\Closure $_after = null;
|
|
|
|
|
private ?\Closure $_before = null;
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-09 03:00:30 +08:00
|
|
|
/**
|
|
|
|
|
* @param \Closure $handler
|
|
|
|
|
* @return $this
|
|
|
|
|
*/
|
|
|
|
|
public function after(\Closure $handler): static
|
|
|
|
|
{
|
|
|
|
|
$this->_after = $handler;
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param \Closure $handler
|
|
|
|
|
* @return $this
|
|
|
|
|
*/
|
|
|
|
|
public function before(\Closure $handler): static
|
|
|
|
|
{
|
|
|
|
|
$this->_before = $handler;
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-09 02:45:33 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $argv
|
|
|
|
|
* @return mixed
|
|
|
|
|
*/
|
|
|
|
|
public function exec(...$argv)
|
|
|
|
|
{
|
|
|
|
|
try {
|
2021-04-09 03:03:41 +08:00
|
|
|
if ($this->_before instanceof \Closure) {
|
|
|
|
|
call_user_func($this->_before, ...$argv);
|
|
|
|
|
}
|
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;
|
2021-04-09 03:00:30 +08:00
|
|
|
} finally {
|
2021-04-09 03:03:41 +08:00
|
|
|
if ($this->_after instanceof \Closure) {
|
|
|
|
|
call_user_func($this->_after, ...$argv);
|
|
|
|
|
}
|
2021-04-09 02:45:33 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|