xtompie / middleware
Middleware component
0.1
2021-12-15 19:37 UTC
Requires
- php: >=8.0
Requires (Dev)
- phpunit/phpunit: ^9.5
This package is auto-updated.
Last update: 2025-04-16 02:46:47 UTC
README
Middleware component
Requiments
PHP >= 8.0
Installation
Using composer
composer require xtompie/middleware
Docs
One middleware is a callble fn(mixed $input, $next): mixed
Private service middleware
Service without middleware:
<?php use Xtompie\Middleware\Middleware; class TopArticlesService { public function __construct( protected DAO $dao, ) {} public function __invoke(int $limit, int $offset): ArticleCollection { return $this->dao->query([ "select" => "*", "from" => "article", "order" => "top DESC" "offset" => $offset, "limit" => $limit ]) ->mapInto(Article::class) ->pipeInto(ArticleCollection::class); } } }
Service with middleware
<?php use Xtompie\Middleware\Middleware; class TopArticlesService { public function __construct( protected DAO $dao, protected InMemoryCacheMiddleware $cache, protected LoggerMiddleware $logger, ) {} public function __invoke(int $limit, int $offset): ArticleCollection { return Middleware::dispatch( [ $this->cache, $this->logger, fn ($args) => return $this->invoke(...$args) ], func_get_args() ) } protected function invoke(int $limit, int $offset): ArticleCollection { return $this->dao->query([ "select" => "*", "from" => "article", "order" => "top DESC" "offset" => $offset, "limit" => $limit ]) ->mapInto(Article::class) ->pipeInto(ArticleCollection::class); } } }
or with cached middleware chain
<?php use Xtompie\Middleware\Middleware; class TopArticlesService { public function __construct( protected DAO $dao, protected InMemoryCacheMiddleware $cache, protected LoggerMiddleware $logger, protected Middleware $middleware, ) { $this->middleware = $middleware->withMiddlewares([ $this->cache, $this->logger, fn ($args) => return $this->invoke(...$args) ]) } public function __invoke(int $limit, int $offset): ArticleCollection { return ($this->middleware)(func_get_args()); } protected function invoke(int $limit, int $offset): ArticleCollection { return $this->dao->query([ "select" => "*", "from" => "article", "order" => "top DESC" "offset" => $offset, "limit" => $limit ]) ->mapInto(Article::class) ->pipeInto(ArticleCollection::class); } } }