Skip to content
Permalink
Browse files

Moved non-psr7 functions to a separate package (#55)

* Moved non-psr7 functions to a separate package

* Moved more code

* CS

* minor

* Fixed rebase

* Merged all to one PSR17 factory

* Updated readme

* Fixed tests

* Do not use factories in tests

* CS
  • Loading branch information...
Nyholm committed Jul 31, 2018
1 parent 93a9b60 commit fec7c1647166f1653842528f1fc0f870d6c75c45
@@ -57,8 +57,22 @@ $response = $psr18Client->sendRequest($request);

### Create server requests

TODO
```bash
composer require nyholm/psr7-server
```

```php
$psr17Factory = new \Nyholm\Psr7\Factory\Psr17Factory();
$creator = new ServerRequestCreator(
$psr17Factory,
$psr17Factory,
$psr17Factory,
$psr17Factory
);
$serverRequest = $creator->fromGlobals();
```

### Emitting a response

@@ -19,7 +19,7 @@
<php>
<const name="REQUEST_FACTORY" value="Nyholm\Psr7\Factory\Psr17Factory"/>
<const name="RESPONSE_FACTORY" value="Nyholm\Psr7\Factory\Psr17Factory"/>
<const name="SERVER_REQUEST_FACTORY" value="Nyholm\Psr7\Factory\ServerRequestFactory"/>
<const name="SERVER_REQUEST_FACTORY" value="Nyholm\Psr7\Factory\Psr17Factory"/>
<const name="UPLOADED_FILE_FACTORY" value="Nyholm\Psr7\Factory\Psr17Factory"/>
<const name="URI_FACTORY" value="Nyholm\Psr7\Factory\Psr17Factory"/>
<const name="STREAM_FACTORY" value="Nyholm\Psr7\Factory\Psr17Factory"/>
@@ -6,13 +6,16 @@
use Nyholm\Psr7\Request;
use Nyholm\Psr7\Response;
use Nyholm\Psr7\ServerRequest;
use Nyholm\Psr7\Stream;
use Nyholm\Psr7\UploadedFile;
use Nyholm\Psr7\Uri;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestFactoryInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\StreamFactoryInterface;
use Psr\Http\Message\StreamInterface;
use Psr\Http\Message\UploadedFileFactoryInterface;
@@ -22,8 +25,9 @@
/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
* @author Martijn van der Ven <martijn@vanderven.se>
*/
final class Psr17Factory implements RequestFactoryInterface, ResponseFactoryInterface, UriFactoryInterface, StreamFactoryInterface, UploadedFileFactoryInterface
final class Psr17Factory implements RequestFactoryInterface, ResponseFactoryInterface, ServerRequestFactoryInterface, StreamFactoryInterface, UploadedFileFactoryInterface, UriFactoryInterface
{
public function createRequest(string $method, $uri): RequestInterface
{
@@ -63,4 +67,9 @@ public function createUri(string $uri = ''): UriInterface
{
return new Uri($uri);
}
public function createServerRequest(string $method, $uri, array $serverParams = []): ServerRequestInterface
{
return new ServerRequest($method, $uri, [], null, '1.1', $serverParams);
}
}

This file was deleted.

@@ -20,42 +20,4 @@ public function createUri($uri = ''): UriInterface
return new Uri($uri);
}
/**
* Create a new uri from server variable.
*
* @param array $server Typically $_SERVER or similar structure.
*
* @deprecated This function will be removed as it serves no purpose.
*/
public function createUriFromArray(array $server): UriInterface
{
$uri = new Uri('');
if (isset($server['REQUEST_SCHEME'])) {
$uri = $uri->withScheme($server['REQUEST_SCHEME']);
} elseif (isset($server['HTTPS'])) {
$uri = $uri->withScheme('on' === $server['HTTPS'] ? 'https' : 'http');
}
if (isset($server['HTTP_HOST'])) {
$uri = $uri->withHost($server['HTTP_HOST']);
} elseif (isset($server['SERVER_NAME'])) {
$uri = $uri->withHost($server['SERVER_NAME']);
}
if (isset($server['SERVER_PORT'])) {
$uri = $uri->withPort($server['SERVER_PORT']);
}
if (isset($server['REQUEST_URI'])) {
$uri = $uri->withPath(current(explode('?', $server['REQUEST_URI'])));
}
if (isset($server['QUERY_STRING'])) {
$uri = $uri->withQuery($server['QUERY_STRING']);
}
return $uri;
}
}
@@ -3,14 +3,14 @@
namespace Tests\Nyholm\Psr7\Integration;
use Http\Psr7Test\ServerRequestIntegrationTest;
use Nyholm\Psr7\Factory\ServerRequestFactory;
use Nyholm\Psr7\ServerRequest;
class ServerRequestTest extends ServerRequestIntegrationTest
{
public function createSubject()
{
$_SERVER['REQUEST_METHOD'] = 'GET';
return (new ServerRequestFactory())->createServerRequestFromArray($_SERVER);
return new ServerRequest('GET', '/', [], null, '1.1', $_SERVER);
}
}
@@ -3,12 +3,12 @@
namespace Tests\Nyholm\Psr7\Integration;
use Http\Psr7Test\UriIntegrationTest;
use Nyholm\Psr7\Factory\UriFactory;
use Nyholm\Psr7\Uri;
class UriTest extends UriIntegrationTest
{
public function createUri($uri)
{
return (new UriFactory())->createUri($uri);
return new Uri($uri);
}
}

0 comments on commit fec7c16

Please sign in to comment.
You can’t perform that action at this time.