PHP code example of xtompie / middleware

1. Go to this page and download the library: Download xtompie/middleware library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

xtompie / middleware example snippets




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);
        }
    }
}



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);
        }
    }
}




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);
        }
    }
}