Giter VIP home page Giter VIP logo

http-factory's Introduction

PSR-17 HTTP Factory library

Build Status

This library aims to provide a simple implementation of PSR-17 (HTTP Factories). It current supports Guzzle/Psr7 and Zend Diactoros. Installation and usage examples are below, but if you have any questions or issues, please create an issue.

Installation

composer require bulldog/http-factory

Usage

You can use the FactoryBuilder class to create the HTTP Factory of your choice, or you can instantiate them directly. Currently your choices are guzzle and zend. Each factory implements HttpFactoryInterface. Since this library uses PSR-17 all of the individual factories returned will implement their corresponding PSR-17 interface. For more detailed usage, please see the tests folder.

FactoryBuilder

<?php

require 'vendor/autoload.php';

use Bulldog\HttpFactory\FactoryBuilder;
$zendFactory = FactoryBuilder::get('zend');

RequestFactory

Since we can use guzzle or zend, I'll show both examples here, but only one in the rest of the examples. But you should know you can use them interchangeably.

Zend

<?php

require 'vendor/autoload.php';

use Bulldog\HttpFactory\FactoryBuilder;
$factory = FactoryBuilder::get('zend');
$requestFactory = $factory->requestFactory();
$r = $requestFactory->createRequest('GET', 'http://localhost');

Guzzle

You'll notice the only difference between the example above and the one below is we specify guzzle as the parameter to the get method.

<?php

require 'vendor/autoload.php';

use Bulldog\HttpFactory\FactoryBuilder;
$factory = FactoryBuilder::get('guzzle');
$requestFactory = $factory->requestFactory();
$r = $requestFactory->createRequest('GET', 'http://localhost');

Without FactoryBuilder

You don't have to use FactoryBuilder to get an HTTP Factory. See below.

<?php

require 'vendor/autoload.php';

use Bulldog\HttpFactory\GuzzleHttpFactory;
$factory = new GuzzleHttpFactory();
$requestFactory = $factory->requestFactory();
$r = $requestFactory->createRequest('GET', 'http://localhost');

ResponseFactory

In the rest of the examples, I'll only show one example using zend with the FactoryBuilder.

<?php

require 'vendor/autoload.php';

use Bulldog\HttpFactory\FactoryBuilder;
$factory = FactoryBuilder::get('zend');
$responseFactory = $factory->responseFactory();
$r = $responseFactory->createResponse(200, 'OK');

ServerRequestFactory

<?php

require 'vendor/autoload.php';

use Bulldog\HttpFactory\FactoryBuilder;
$factory = FactoryBuilder::get('zend');
$reqFactory = $factory->serverRequestFactory();
$params = ['name' => 'value'];
$r = $reqFactory->createServerRequest('GET', '/', $params);

StreamFactory

<?php

require 'vendor/autoload.php';

use Bulldog\HttpFactory\FactoryBuilder;
$httpFactory = FactoryBuilder::get('zend');
$factory = $httpFactory->streamFactory();
$params = ['name' => 'value'];
$r = $factory->createStream('php://memory');

UploadedFileFactory

<?php

require 'vendor/autoload.php';

use Bulldog\HttpFactory\FactoryBuilder;
$httpFactory = FactoryBuilder::get('zend');

$streamFactory = $httpFactory->streamFactory();
$resource = $streamFactory->createStream('php://temp');

$factory = $httpFactory->uploadedFileFactory();
$params = ['name' => 'value'];
$r = $factory->createUploadedFile(
    $resource,
    1,
    \UPLOAD_ERR_OK,
    'filename.txt',
    'txt'
);

UriFactory

<?php

require 'vendor/autoload.php';

use Bulldog\HttpFactory\FactoryBuilder;
$httpFactory = FactoryBuilder::get('zend');
$factory = $httpFactory->uriFactory();
$r = $factory->createUri('/');

Links

http-factory's People

Contributors

levidurfee avatar stof avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

http-factory's Issues

More Tests

This repo could use a few more tests. Anyone is free to add them :)

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.