Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/bermudaphp/psr15factory
Makes middleware for psr-15 based application
https://github.com/bermudaphp/psr15factory
http middleware middleware-factory php php-middleware php8 psr-15
Last synced: 2 months ago
JSON representation
Makes middleware for psr-15 based application
- Host: GitHub
- URL: https://github.com/bermudaphp/psr15factory
- Owner: bermudaphp
- License: mit
- Created: 2020-04-20T16:05:19.000Z (almost 5 years ago)
- Default Branch: v2.1.x
- Last Pushed: 2023-04-07T10:49:44.000Z (almost 2 years ago)
- Last Synced: 2024-06-05T10:44:20.824Z (8 months ago)
- Topics: http, middleware, middleware-factory, php, php-middleware, php8, psr-15
- Language: PHP
- Homepage:
- Size: 181 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## Installation
```bash
composer require bermudaphp/psr15factory
```## Usage
```php
$factory = new MiddlewareFactory($containerInterface, $responseFactoryInterface);
```## Classname
```php
class MyMiddleware implements MiddlewareInterface
{
private ResponseFactoryInterface $factory;
public function __construct(ResponseFactoryInterface $factory)
{
$this->factory = $factory;
}
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
return $this->factory->createResponse(200, 'OK!');
}
}$middleware = $factory->make(MyMiddleware::class);
$middleware instanceof MyMiddleware::class // trueclass MyHandler implements RequestHandlerInterface
{
private ResponseFactoryInterface $factory;
public function __construct(ResponseFactoryInterface $factory)
{
$this->factory = $factory;
}
public function handle(ServerRequestInterface $request): ResponseInterface
{
return $this->factory->createResponse(200, 'OK!');
}
}$middleware = $factory->make(MyHandler::class);
$middleware instanceof MiddlewareInterface::class // true```
## Lazy Middleware
```php
$middleware = $factory->make(static function(ContainerInterface $c) use ($uri, $permanent): RedirectMiddleware
{
return new RedirectMiddleware($uri, $c->get(ResponseFactoryInterface::class), $permanent);
});$middleware instanceof MiddlewareInterface::class // true
$middleware instanceof RedirectMiddleware::class // true```
## Callable Middleware
```php
$middleware = $factory->make(static function(ServerRequestInterface $req): ResponseInterface
{
return new TextResponse('Hello World!');
});$middleware instanceof MiddlewareInterface::class // true
class MyCallback
{
public function methodName(ServerRequestInterface $req) : ResponseInterface
{
return new TextResponse('Hello World');
}
}$middleware = $factory->make('MyCallback@methodName');
$middleware instanceof MiddlewareInterface::class // true
```## Invokable Middleware
```php
class Invokable
{
public function __invoke(string $name) : ResponseInterface
{
return new TextResponse('Hello, '. $name);
}
}$factory->make('Invokable::class') instanceof MiddlewareInterface::class ; // true
```## Request Args Middleware
```php
$middleware = $factory->make(static function(string $name): ResponseInterface
{
return new TextResponse(sprintf('Hello, %s!', $name));
});$response = $middleware->process((new ServerRequest())->withAttribute('name', 'John'), $requestHandler);
$response instanceof TextResponse // true
```## Availables callback method signature
```php
function(): ResponseInterface ;
function(ContainerInterface $container): ResponseInterface ;
function(mixed ... $requestAttributeName): ResponseInterface ;
function(ServerRequestInterface $req): ResponseInterface ;
function(ServerRequestInterface $req, RequestHandlerInterface $handler): ResponseInterface ;
function(ServerRequestInterface $req, ResponseInterface $resp, callable $next): ResponseInterface ;
function(ServerRequestInterface $req, callable $next): ResponseInterface ;
```## Aggregation MiddlewareFactory
```php
$myFactory = new class implements MiddlewareFactoryInterface
{
/**
* @param mixed $any
* @return MiddlewareInterface
* @throws UnresolvableMiddlewareException
*/
public function make($any): MiddlewareInterface
{
if (is_string($any) && $any == 'redirect')
{
return new MyRedirectMiddleware ;
}
throw new UnresolvableMiddlewareException;
}
/**
* Alias for self::make
*/
public function __invoke($any) : MiddlewareInterface
{
return $this->make($any);
}
}$factory = (new AggregateMiddlewareFactory)->addFactory($factory)
->addFactory($myFactory);$middleware = $factory->make('redirect');
$middleware instanceof MiddlewareInterface // true
```