agungsugiarto/codeigniter4-psr7bridge

CodeIgniter 4 PSR HTTP message bridge

v1.0.0-beta.2 2023-01-06 05:07 UTC

This package is auto-updated.

Last update: 2024-02-06 10:43:42 UTC


README

Latest Stable Version Total Downloads Latest Unstable Version License

The PSR-7 Bridge

The PSR-7 bridge converts codeigniter4-http objects from and to objects implementing HTTP message interfaces defined by the PSR-7.

Table of Contents

Installation

$ composer require agungsugiarto/codeigniter4-psr7bridge

The bridge also needs a PSR-7 and PSR-17 implementation to convert IncomingRequest objects to PSR-7 objects. The following command installs the nyholm/psr7 library, a lightweight and fast PSR-7 implementation.

$ composer require nyholm/psr7

Usage

Converting from IncomingRequest Objects to PSR-7

The bridge provides an interface of a factory called Fluent\HttpMessageBridge\Interfaces\HttpMessageFactoryInterface that builds objects implementing PSR-7 interfaces from IncommingRequest objects.

The following code snippet explains how to convert a CodeIgniter\HTTP\IncomingRequest to a Nyholm\Psr7\ServerRequest class implementing the Psr\Http\Message\ServerRequestInterface interface:

<?php

use CodeIgniter\Config\Services;
use Fluent\HttpMessageBridge\PsrHttpFactory;
use Nyholm\Psr7\Factory\Psr17Factory;

$requestCodeIgniter = Services::request();

$psr17Factory = new Psr17Factory();
$psrHttpFactory = new PsrHttpFactory($psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory);
$psrRequest = $psrHttpFactory->createRequest($requestCodeIgniter);

And now from a CodeIgniter\HTTP\Response to a Nyholm\Psr7\Response class implementing the Psr\Http\Message\ResponseInterface interface:

<?php

use CodeIgniter\HTTP\Response;
use Fluent\HttpMessageBridge\PsrHttpFactory;
use Nyholm\Psr7\Factory\Psr17Factory;

$responseCodeIgniter = new Response(config('App'));

$psr17Factory = new Psr17Factory();
$psrHttpFactory = new PsrHttpFactory($psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory);
$psrResponse = $psrHttpFactory->createResponse($responseCodeIgniter);

License

Released under the MIT License, see LICENSE.