Skip to content

Instantly share code, notes, and snippets.

@vakata
Last active December 3, 2021 13:18
Show Gist options
  • Select an option

  • Save vakata/f91be1cd3cea764837bae8c5dc2b489b to your computer and use it in GitHub Desktop.

Select an option

Save vakata/f91be1cd3cea764837bae8c5dc2b489b to your computer and use it in GitHub Desktop.
middleware callable dispatchers
<?php
// class for a stack of callables
class Dispatcher
{
protected array $stack;
public function __construct(array &$stack)
{
$this->stack = $stack;
}
public function __invoke(RequestInterface $req)
{
$handler = array_shift($this->stack);
return call_user_func($handler, $req, new self($this->stack));
}
}
// single line function for a stack of callables
$run = function ($req) use (&$stack, &$run) { return call_user_func(array_shift($stack), $req, $run); };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment