for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
/**
* Class PriorityQueueDispatcher
*
* @link https://github.com/libreworks/caridea-dispatch
* @created 10.03.2019
* @author smiley <[email protected]>
* @copyright 2019 smiley
* @license MIT
*/
namespace chillerlan\HTTP\Psr15;
use Psr\Http\Server\MiddlewareInterface;
use function usort;
class PriorityQueueDispatcher extends QueueDispatcher{
* @inheritDoc
public function addStack(iterable $middlewareStack):QueueDispatcher{
foreach($middlewareStack as $middleware){
if(!$middleware instanceof MiddlewareInterface){
throw new MiddlewareException('invalid middleware');
}
if(!$middleware instanceof PriorityMiddlewareInterface){
$middleware = new PriorityMiddleware($middleware);
$this->middlewareStack[] = $middleware;
$this->sortMiddleware();
return $this;
public function add(MiddlewareInterface $middleware):QueueDispatcher{
protected function sortMiddleware():void{
usort(
$this->middlewareStack,
fn(PriorityMiddlewareInterface $a, PriorityMiddlewareInterface $b) => $b->getPriority() <=> $a->getPriority()
);