PHP code example of gilsegura / psr-messages-bundle
1. Go to this page and download the library: Download gilsegura/psr-messages-bundle 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/ */
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Messages\Bundle\Attribute\Pipeline;
use Psr\Messages\Bundle\Controller\AbstractController;
use Psr\Messages\Bundle\Pipeline\Config\HeadersConfiguration;
use Psr\Messages\Bundle\Pipeline\Config\MediaTypeParser;
use Psr\Messages\Bundle\Pipeline\Config\ParsedBodyConfiguration;
use Psr\Messages\Bundle\Pipeline\Config\Validation\BodyValidationConfiguration;
use Psr\Messages\Bundle\Pipeline\Config\Validation\RequestValidationConfiguration;
use Psr\Messages\Bundle\Pipeline\Config\ValidationConfiguration;
#[Pipeline(
new HeadersConfiguration(RequestHeaders::class),
new ValidationConfiguration(
new RequestValidationConfiguration(new BodyValidationConfiguration('create-user')),
),
new ParsedBodyConfiguration(MediaTypeParser::JSON_API, CreateUserRequest::class),
)]
final readonly class PostUserController extends AbstractController
{
#[\Override]
public function __invoke(ServerRequestInterface $request): ResponseInterface
{
$body = $request->getParsedBody(); // a CreateUserRequest, already parsed
// ...
}
}
use Psr\Messages\Bundle\Exception\ExceptionStatusMapper;
use Psr\Server\ResponseFactory\Status;
final readonly class ValidationStatusMapper implements ExceptionStatusMapper
{
#[\Override]
public function statusFor(\Throwable $throwable): ?Status
{
return $throwable instanceof ValidationExceptionInterface
? Status::UNPROCESSABLE_CONTENT
: null;
}
}
use Psr\Http\Server\MiddlewareInterface;
use Psr\Messages\Bundle\Pipeline\MiddlewareConfiguration;
use Psr\Messages\Bundle\Pipeline\MiddlewareFactory;
final readonly class RateLimitConfiguration implements MiddlewareConfiguration
{
public function __construct(public int $perMinute = 60) {}
#[\Override]
public static function factory(): string
{
return RateLimitMiddlewareFactory::class;
}
}
/** @implements MiddlewareFactory<RateLimitConfiguration> */
final readonly class RateLimitMiddlewareFactory implements MiddlewareFactory
{
#[\Override]
public function __invoke(MiddlewareConfiguration $configuration): MiddlewareInterface
{
return new RateLimitMiddleware($configuration->perMinute);
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.