72 lines
1.3 KiB
PHP
72 lines
1.3 KiB
PHP
<?php
|
|
|
|
|
|
namespace HttpServer\Events;
|
|
|
|
|
|
class Pipeline
|
|
{
|
|
|
|
private \Closure $if;
|
|
private \Closure $else;
|
|
private \Closure $catch;
|
|
|
|
private bool $condition;
|
|
|
|
|
|
/**
|
|
* @param bool $condition
|
|
* @param \Closure $handler
|
|
* @return $this
|
|
*/
|
|
public function if(bool $condition, \Closure $handler): static
|
|
{
|
|
$this->condition = $condition;
|
|
$this->if = $handler;
|
|
return $this;
|
|
}
|
|
|
|
|
|
/**
|
|
* @param \Closure $handler
|
|
* @return $this
|
|
*/
|
|
public function else(\Closure $handler): static
|
|
{
|
|
$this->else = $handler;
|
|
return $this;
|
|
}
|
|
|
|
|
|
/**
|
|
* @param \Closure $handler
|
|
* @return $this
|
|
*/
|
|
public function catch(\Closure $handler): static
|
|
{
|
|
$this->catch = $handler;
|
|
return $this;
|
|
}
|
|
|
|
|
|
/**
|
|
* @param $argv
|
|
* @return mixed
|
|
*/
|
|
public function exec(...$argv)
|
|
{
|
|
try {
|
|
if ($this->condition !== true) {
|
|
call_user_func($this->else, ...$argv);
|
|
} else {
|
|
call_user_func($this->if, ...$argv);
|
|
}
|
|
return $argv;
|
|
} catch (\Throwable $exception) {
|
|
call_user_func($this->catch, $exception, ...$argv);
|
|
return $argv;
|
|
}
|
|
}
|
|
|
|
}
|