bulldog / http-factory
A PSR-17 compliant HTTP Factory
Requires
- guzzlehttp/psr7: ^1.4
- psr/http-factory: ^1.0
Requires (Dev)
- phpunit/phpunit: ^7.2
- zendframework/zend-diactoros: ^1.8
Provides
This package is auto-updated.
Last update: 2024-11-22 22:09:42 UTC
README
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('/');