Code Monkey home page Code Monkey logo

http-factory's Introduction

๐Ÿญ HTTP-Factory

Travis Scrutinizer Packagist GitHub (pre-)release License PHP from Packagist

HTTP-Factory is a PHP package that implements PSR-17 HTTP factories interface. It acts as a simple facade to provide easy access to concrete HTTP factory packets. As its main feature it offers support for auto-discovery of the supported factories.

All PSR-17 interfaces are implemented:

  • Psr\Http\Message\ResponseFactoryInterface
  • Psr\Http\Message\ServerRequestFactoryInterface
  • Psr\Http\Message\StreamFactoryInterface
  • Psr\Http\Message\UploadedFileFactoryInterface
  • Psr\Http\Message\UriFactoryInterface

Additionally, it implements a createServerRequestFromGlobals method, which is not part of PSR-17.

Auto-discovering PSR-7 packages

The package features auto-discovery support for the following PSR-7 packages:

  1. laminas/laminas-diactoros
  2. guzzlehttp/psr7
  3. slim/slim
  4. nyholm/psr7

The auto-discovery mechanism assumes that you are using one (and only one) of the above PSR-7 packages in your project. The first detected PSR-17 package will then be used for all interface factory methods.

Installation

When starting a new project one of the following PSR-7 packages must be installed.

$ composer require laminas/laminas-diactoros
$ composer require guzzlehttp/psr7
$ composer require slim/slim
$ composer require nyholm/psr7

When using the "nyholm/psr7" package you have to require the "nyholm/psr7-server" package, too.

$ composer require nyholm/psr7-server

Then install the HTTP-Factory package.

$ composer require tebe/http-factory

You can now use HTTP-Factory in the codebase of your project like so:

use Tebe\HttpFactory\HttpFactory;

$factory = new HttpFactory();

# creates a ResponseInterface
$factory->createResponse(int $code = 200, string $reasonPhrase = '');

# creates a ServerRequestInterface
$factory->createServerRequest(string $method, $uri, array $serverParams = []);

# creates a ServerRequestInterface
$factory->createServerRequestFromGlobals();

# creates a StreamInterface
$factory->createStream(string $content = '');

# creates a StreamInterface
$factory->createStreamFromFile(string $filename, string $mode = 'r');

# creates a StreamInterface
$factory->createStreamFromResource($resource);

# creates an UriInterface
$factory->createUri(string $uri = '');

# creates an UploadedFileInterface
$factory->createUploadedFile(
    StreamInterface $stream, 
    int $size = null, 
    int $error = \UPLOAD_ERR_OK, 
    string $clientFilename = null, 
    string $clientMediaType = null
); 

Usage

Using constructor

use Tebe\HttpFactory\HttpFactory;

$factory = new HttpFactory();
$response = $factory->createResponse(200);
echo $response->getStatusCode();

Using static instance method

use Tebe\HttpFactory\HttpFactory;

$response = HttpFactory::instance()->createResponse(200);
echo $response->getStatusCode();

Using own strategies

use Tebe\HttpFactory\HttpFactory;

HttpFactory::setStrategies([
    DiactorosFactory::class,
    GuzzleFactory::class,
    SlimFactory::class
]);

$response = HttpFactory::instance()->createResponse(200);
echo $response->getStatusCode();

Using own factory

use Tebe\HttpFactory\HttpFactory;
use Tebe\HttpFactory\FactoryInterface;

class MyFactory implements FactoryInterface
{
    // implement interface methods
}

$factory = new HttpFactory();
$factory->setFactory(new MyFactory());
$response = $factory->createResponse(200);
echo $response->getStatusCode();

Tests

Run PHPUnit:

$ composer phpunit

Run PHP_CodeSniffer:

$ composer phpcs

Run PHP Code Beautifier and Fixer:

$ composer phpcbf

Run PHPUnit and PHP_CodeSniffer together:

$ composer test

Suggestions

Any suggestions? Open an issue.

http-factory's People

Contributors

tbreuss avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.