Skip to content

Commit

Permalink
Implement createServerRequestFromGlobals and writing tests, edited RE…
Browse files Browse the repository at this point in the history
…ADME
  • Loading branch information
tbreuss committed Dec 27, 2018
1 parent 0fd05ae commit 7bafc9f
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 7 deletions.
47 changes: 45 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@


HTTP-Factory is a PHP package that implements [PSR-17 HTTP factories](https://www.php-fig.org/psr/psr-17/) interface.
Additionally it acts as a simple facade which allows easy access to concrete HTTP Factory packages.
It offers auto-discovering support and acts as a simple facade to allow easy access to concrete HTTP Factory packages.

All PSR-17 interfaces are implemented:

Expand All @@ -19,6 +19,7 @@ All PSR-17 interfaces are implemented:
- 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

Expand All @@ -44,13 +45,55 @@ $ 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.
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
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"guzzlehttp/psr7": "^1.4",
"squizlabs/php_codesniffer": "^3.3",
"phpunit/phpunit": "^6.5",
"nyholm/psr7": "^1.0"
"nyholm/psr7": "^1.0",
"nyholm/psr7-server": "^0.3.0"
},
"autoload": {
"psr-4": {
Expand Down
15 changes: 13 additions & 2 deletions src/Factory/NyholmFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Tebe\HttpFactory\Factory;

use Nyholm\Psr7\Factory\Psr17Factory;
use Nyholm\Psr7Server\ServerRequestCreator;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\StreamInterface;
Expand All @@ -30,7 +31,8 @@ public function __construct()
*/
public static function isInstalled(): bool
{
return class_exists('Nyholm\\Psr7\\Factory\\Psr17Factory');
return class_exists('Nyholm\\Psr7\\Factory\\Psr17Factory')
&& class_exists('Nyholm\\Psr7Server\\ServerRequestCreator');
}

/**
Expand All @@ -54,7 +56,16 @@ public function createServerRequest(string $method, $uri, array $serverParams =
*/
public function createServerRequestFromGlobals(): ServerRequestInterface
{
// TODO: Implement createServerRequestFromGlobals() method.
$psr17Factory = new Psr17Factory();

$creator = new ServerRequestCreator(
$psr17Factory, // ServerRequestFactory
$psr17Factory, // UriFactory
$psr17Factory, // UploadedFileFactory
$psr17Factory // StreamFactory
);

return $creator->fromGlobals();
}

/**
Expand Down
7 changes: 5 additions & 2 deletions src/Factory/SlimFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Psr\Http\Message\StreamInterface;
use Psr\Http\Message\UploadedFileInterface;
use Psr\Http\Message\UriInterface;
use Slim\Http\Environment;
use Slim\Http\Headers;
use Slim\Http\Request;
use Slim\Http\Response;
Expand All @@ -25,7 +26,8 @@ class SlimFactory implements FactoryInterface
*/
public static function isInstalled(): bool
{
return class_exists('Slim\\Http\\Response')
return class_exists('Slim\\Http\\Environment')
&& class_exists('Slim\\Http\\Response')
&& class_exists('Slim\\Http\\Request')
&& class_exists('Slim\\Http\\Stream')
&& class_exists('Slim\\Http\\Uri');
Expand Down Expand Up @@ -61,7 +63,8 @@ public function createServerRequest(string $method, $uri, array $serverParams =
*/
public function createServerRequestFromGlobals(): ServerRequestInterface
{
// TODO: Implement createServerRequestFromGlobals() method.
$environment = Environment::mock();
return Request::createFromEnvironment($environment);
}

/**
Expand Down
5 changes: 5 additions & 0 deletions tests/Factory/DiactorosFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ public function testCreateServerRequest()
$this->assertInstanceOf(ServerRequest::class, $this->factory->createServerRequest('GET', '/'));
}

public function testCreateServerRequestFromGlobals()
{
$this->assertInstanceOf(ServerRequest::class, $this->factory->createServerRequestFromGlobals());
}

public function testCreateStream()
{
$this->assertInstanceOf(Stream::class, $this->factory->createStream('STREAM'));
Expand Down
5 changes: 5 additions & 0 deletions tests/Factory/GuzzleFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ public function testCreateServerRequest()
$this->assertInstanceOf(ServerRequest::class, $this->factory->createServerRequest('GET', '/'));
}

public function testCreateServerRequestFromGlobals()
{
$this->assertInstanceOf(ServerRequest::class, $this->factory->createServerRequestFromGlobals());
}

public function testCreateStream()
{
$this->assertInstanceOf(Stream::class, $this->factory->createStream('fromString'));
Expand Down
5 changes: 5 additions & 0 deletions tests/Factory/NyholmFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ public function testCreateServerRequest()
$this->assertInstanceOf(ServerRequest::class, $this->factory->createServerRequest('GET', '/'));
}

public function testCreateServerRequestFromGlobals()
{
$this->assertInstanceOf(ServerRequest::class, $this->factory->createServerRequestFromGlobals());
}

public function testCreateStream()
{
$this->assertInstanceOf(Stream::class, $this->factory->createStream('fromString'));
Expand Down
5 changes: 5 additions & 0 deletions tests/Factory/SlimFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ public function testCreateServerRequest()
$this->assertInstanceOf(Request::class, $this->factory->createServerRequest('GET', '/'));
}

public function testCreateServerRequestFromGlobals()
{
$this->assertInstanceOf(Request::class, $this->factory->createServerRequestFromGlobals());
}

public function testCreateStream()
{
$this->assertInstanceOf(Stream::class, $this->factory->createStream('fromString'));
Expand Down
5 changes: 5 additions & 0 deletions tests/HttpFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ public function testCreateServerRequest()
$this->assertInstanceOf(ServerRequestInterface::class, $this->factory->createServerRequest('GET', '/'));
}

public function testCreateServerRequestFromGlobals()
{
$this->assertInstanceOf(ServerRequestInterface::class, $this->factory->createServerRequestFromGlobals());
}

public function testCreateStream()
{
$this->assertInstanceOf(StreamInterface::class, $this->factory->createStream('fromString'));
Expand Down

0 comments on commit 7bafc9f

Please sign in to comment.