https://github.com/tbreuss/http-factory
:factory: PSR-17 HTTP-Factory with auto-discovery support
https://github.com/tbreuss/http-factory
auto-discovery discovery factory hacktoberfest http-factory middleware psr-17 psr17
Last synced: about 1 month ago
JSON representation
:factory: PSR-17 HTTP-Factory with auto-discovery support
- Host: GitHub
- URL: https://github.com/tbreuss/http-factory
- Owner: tbreuss
- License: mit
- Created: 2018-08-18T12:24:31.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2023-01-10T18:17:34.000Z (over 2 years ago)
- Last Synced: 2025-04-26T01:26:29.492Z (about 1 month ago)
- Topics: auto-discovery, discovery, factory, hacktoberfest, http-factory, middleware, psr-17, psr17
- Language: PHP
- Homepage:
- Size: 38.1 KB
- Stars: 3
- Watchers: 3
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# :factory: HTTP-Factory
[](https://travis-ci.org/tbreuss/http-factory)
[](https://scrutinizer-ci.com/g/tbreuss/http-factory/)
[](https://packagist.org/packages/tebe/http-factory)
[](https://github.com/tbreuss/http-factory/releases)
[](https://github.com/tbreuss/http-factory/blob/master/LICENSE)
[](https://packagist.org/packages/tebe/http-factory)HTTP-Factory is a PHP package that implements [PSR-17 HTTP factories](https://www.php-fig.org/psr/psr-17/) 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\UriFactoryInterfaceAdditionally, 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/psr7The 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.
~~~bash
$ 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.
~~~bash
$ composer require nyholm/psr7-server
~~~Then install the HTTP-Factory package.
~~~bash
$ composer require tebe/http-factory
~~~You can now use HTTP-Factory in the codebase of your project like so:
~~~php
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
~~~php
use Tebe\HttpFactory\HttpFactory;$factory = new HttpFactory();
$response = $factory->createResponse(200);
echo $response->getStatusCode();
~~~### Using static instance method
~~~php
use Tebe\HttpFactory\HttpFactory;$response = HttpFactory::instance()->createResponse(200);
echo $response->getStatusCode();
~~~### Using own strategies
~~~php
use Tebe\HttpFactory\HttpFactory;HttpFactory::setStrategies([
DiactorosFactory::class,
GuzzleFactory::class,
SlimFactory::class
]);$response = HttpFactory::instance()->createResponse(200);
echo $response->getStatusCode();
~~~### Using own factory
~~~php
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:
~~~bash
$ composer phpunit
~~~Run PHP_CodeSniffer:
~~~bash
$ composer phpcs
~~~Run PHP Code Beautifier and Fixer:
~~~bash
$ composer phpcbf
~~~Run PHPUnit and PHP_CodeSniffer together:
~~~bash
$ composer test
~~~## Suggestions
Any suggestions? Open an issue.