loophp/psr-http-message-bridge-bundle

Register symfony/psr-http-message-bridge classes in your Symfony application.

1.0.0 2021-07-21 20:28 UTC

README

Latest Stable Version GitHub stars Total Downloads GitHub Workflow Status Scrutinizer code quality Type Coverage Code Coverage License Donate! Donate!

PSR HTTP Message Bridge Bundle

This bundle will register in your Symfony application's container a couple of services.

Those services are from the symfony/psr-http-message-brige bridge package.

It provides:

  • An argument value resolver to be able to get the following objects through:

    • Psr\Http\Message\MessageInterface
    • Psr\Http\Message\RequestInterface
    • Psr\Http\Message\ServerRequestInterface

    Those are directly derived from the current Symfony Request object.

    It means that you can directly get a PSR7 Request in Symfony controllers through method parameters.

  • An event listener that let users return PSR7 Responses in Symfony controllers. Basically, this event will check if the return value of a Response is an instance of Psr\Http\Message\ResponseInterface and only if it's the case, will convert it into a Symfony response.

  • Factories:

    • A HttpFoundactionFactory service to convert PSR requests into Symfony requests.
    • A PsrHttpFactory service to convert Symfony requests into PSR requests.

Requirements

  • PHP >= 7.3
  • Symfony >= 4

Installation

composer require loophp/psr-http-message-bridge-bundle

Once the bundle installed in your application, it's ready to use, there is no configuration to set up.

Usage

<?php

declare(strict_types=1);

namespace App\Controller;

use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

final class HelloWorldController {

    private ResponseFactoryInterface $responseFactory;
    private StreamFactoryInterface $streamFactory;

    public function __construct(ResponseFactoryInterface $responseFactory, StreamFactoryInterface $streamFactory)
    {
        $this->responseFactory = $responseFactory;
        $this->streamFactory = $streamFactory;
    }

    /**
     * @Route("/hello-world", name="hello_world")
     */
    public function __invoke(RequestInterface $psrRequest): ResponseInterface {
        // You can do something with $psrRequest if needed.

        // Build the PSR response.
        $response = $this->responseFactory->createResponse();

        // Return the new PSR response.
        return $response->withBody($this->streamFactory->createStream('Hello world!'));
    }
}

Code quality, tests and benchmarks

Every time changes are introduced into the library, Github run the tests and the benchmarks.

The library has tests written with PHPSpec. Feel free to check them out in the spec directory. Run composer phpspec to trigger the tests.

Before each commit some inspections are executed with GrumPHP, run ./vendor/bin/grumphp run to check manually.

PHPInfection is used to ensure that your code is properly tested, run composer infection to test your code.

Contributing

Feel free to contribute by sending Github pull requests. I'm quite responsive :-)

Changelog

See CHANGELOG.md for a changelog based on git commits.

For more detailed changelogs, please check the release changelogs.