PHP code example of tobento / service-middleware

1. Go to this page and download the library: Download tobento/service-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/ */

    

tobento / service-middleware example snippets


use Tobento\Service\Middleware\MiddlewareDispatcher;
use Tobento\Service\Middleware\AutowiringMiddlewareFactory;
use Tobento\Service\Middleware\FallbackHandler;
use Tobento\Service\Container\Container;
use Nyholm\Psr7\Factory\Psr17Factory;

// create middleware dispatcher.
$dispatcher = new MiddlewareDispatcher(
    new FallbackHandler((new Psr17Factory())->createResponse(404)),
    new AutowiringMiddlewareFactory(new Container()) // any PSR-11 container
);

$newDispatcher = $dispatcher->new();

use Psr\Http\Server\RequestHandlerInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;

$dispatcher->add(function(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface {
    return $handler->handle($request);
});

use Psr\Http\Server\RequestHandlerInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Server\MiddlewareInterface;

class Middleware implements MiddlewareInterface
{
    public function process(
        ServerRequestInterface $request,
        RequestHandlerInterface $handler
    ): ResponseInterface {
        $response = $handler->handle($request);
        $response->getBody()->write('Hello word');
        return $response;
    }
}

$dispatcher->add(new Middleware());

use Psr\Http\Server\RequestHandlerInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Server\MiddlewareInterface;

class Middleware implements MiddlewareInterface
{
    public function process(
        ServerRequestInterface $request,
        RequestHandlerInterface $handler
    ): ResponseInterface {
        $response = $handler->handle($request);
        $response->getBody()->write('Hello word');
        return $response;
    }
}

$dispatcher->add(Middleware::class);

use Psr\Http\Server\RequestHandlerInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Server\MiddlewareInterface;

class Middleware implements MiddlewareInterface
{
    public function __construct(
        protected string $name,
    ) {}
    
    public function process(
        ServerRequestInterface $request,
        RequestHandlerInterface $handler
    ): ResponseInterface {
        $response = $handler->handle($request);
        $response->getBody()->write('Hello '.$this->name);
        return $response;
    }
}

$dispatcher->add([Middleware::class, 'name' => 'Sam']);

use Psr\Http\Server\RequestHandlerInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Server\MiddlewareInterface;

class Middleware implements MiddlewareInterface
{
    public function __construct(
        protected string $name,
    ) {}
    
    public function process(
        ServerRequestInterface $request,
        RequestHandlerInterface $handler
    ): ResponseInterface {
        $response = $handler->handle($request);
        $response->getBody()->write('Hello '.$this->name);
        return $response;
    }
}

$dispatcher->add(
    [Middleware::class, 'name' => 'Sam'],
    function(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface {
        return $handler->handle($request);
    },
);

// add single alias.
$dispatcher->addAlias('alias', Middleware::class);

// add multiple alias.
$dispatcher->addAliases([
    'alias' => Middleware::class,
]);

// add middleware by alias.
$dispatcher->add('alias');

// if you have not resolvable parameters:
$dispatcher->add(['alias', 'name' => 'Sam']);

$dispatcher->addGroup(name: 'api', middlewares: [
    Middleware::class,
    // with build-in parameters:
    [AnotherMiddleware::class, 'name' => 'Sam'],
    // by alias:
    'aliasedMiddleware',
    // by class instance:
    new SomeMiddleware(),
]);

// add middlewares by group:
$dispatcher->add('api');

// highest number first.
$dispatcher->add(Middleware::class, priority: 100);

$dispatcher->add(AnotherMiddleware::class, priority: 200);

use Nyholm\Psr7\Factory\Psr17Factory;

$request = (new Psr17Factory())->createServerRequest('GET', 'https://example.com');

$response = $dispatcher->handle($request);