first commit
This commit is contained in:
36
vendor/kodus/psr7-server/CHANGELOG.md
vendored
Normal file
36
vendor/kodus/psr7-server/CHANGELOG.md
vendored
Normal file
@ -0,0 +1,36 @@
|
||||
# Changelog
|
||||
|
||||
All notable changes to this project will be documented in this file, in reverse chronological order by release.
|
||||
|
||||
## 0.3.0
|
||||
|
||||
### Added
|
||||
|
||||
- `ServerRequestCreator` is final
|
||||
|
||||
### Fixed
|
||||
|
||||
- Fallback to an empty Stream if UploadedFileFactory fails.
|
||||
|
||||
## 0.2.0
|
||||
|
||||
### Changed
|
||||
|
||||
- Make sure we use psr/http-factory
|
||||
|
||||
## 0.1.2
|
||||
|
||||
### Added
|
||||
|
||||
- `ServerRequestCreatorInterface`
|
||||
- `ServerRequestCreator::getHeadersFromServer`
|
||||
|
||||
## 0.1.1
|
||||
|
||||
### Added
|
||||
|
||||
Better testing
|
||||
|
||||
## 0.1.0
|
||||
|
||||
First release
|
21
vendor/kodus/psr7-server/LICENSE
vendored
Normal file
21
vendor/kodus/psr7-server/LICENSE
vendored
Normal file
@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2018 Tobias Nyholm
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
38
vendor/kodus/psr7-server/README.md
vendored
Normal file
38
vendor/kodus/psr7-server/README.md
vendored
Normal file
@ -0,0 +1,38 @@
|
||||
# Helper class to create PSR-7 server request
|
||||
|
||||
[](https://github.com/Nyholm/psr7-server/releases)
|
||||
[](https://travis-ci.org/Nyholm/psr7-server)
|
||||
[](https://scrutinizer-ci.com/g/Nyholm/psr7-server)
|
||||
[](https://scrutinizer-ci.com/g/Nyholm/psr7-server)
|
||||
[](https://packagist.org/packages/nyholm/psr7-server)
|
||||
[](https://packagist.org/packages/nyholm/psr7-server)
|
||||
[](LICENSE)
|
||||
|
||||
A helper class that can create ANY PSR-7 server request.
|
||||
|
||||
## Installation
|
||||
|
||||
```bash
|
||||
composer require nyholm/psr7-server
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
```php
|
||||
// Instanciate ANY PSR-17 factory implementations. Here is nyholm/psr7 as an example
|
||||
$psr17Factory = new \Nyholm\Psr7\Factory\Psr17Factory();
|
||||
|
||||
$creator = new \Nyholm\Psr7Server\ServerRequestCreator(
|
||||
$psr17Factory, // ServerRequestFactory
|
||||
$psr17Factory, // UriFactory
|
||||
$psr17Factory, // UploadedFileFactory
|
||||
$psr17Factory // StreamFactory
|
||||
);
|
||||
|
||||
$serverRequest = $creator->fromGlobals();
|
||||
```
|
||||
|
||||
## Other packages
|
||||
|
||||
* [nyholm/psr7](https://github.com/Nyholm/psr7) - A super fast PSR-7 implementation.
|
||||
* [zendframework/zend-httphandlerrunner](https://github.com/zendframework/zend-httphandlerrunner) - To send/emit PSR-7 responses
|
279
vendor/kodus/psr7-server/src/ServerRequestCreator.php
vendored
Normal file
279
vendor/kodus/psr7-server/src/ServerRequestCreator.php
vendored
Normal file
@ -0,0 +1,279 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Nyholm\Psr7Server;
|
||||
|
||||
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;
|
||||
use Psr\Http\Message\UploadedFileInterface;
|
||||
use Psr\Http\Message\UriFactoryInterface;
|
||||
use Psr\Http\Message\UriInterface;
|
||||
|
||||
/**
|
||||
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
|
||||
* @author Martijn van der Ven <martijn@vanderven.se>
|
||||
*/
|
||||
final class ServerRequestCreator implements ServerRequestCreatorInterface
|
||||
{
|
||||
private $serverRequestFactory;
|
||||
|
||||
private $uriFactory;
|
||||
|
||||
private $uploadedFileFactory;
|
||||
|
||||
private $streamFactory;
|
||||
|
||||
public function __construct(
|
||||
ServerRequestFactoryInterface $serverRequestFactory,
|
||||
UriFactoryInterface $uriFactory,
|
||||
UploadedFileFactoryInterface $uploadedFileFactory,
|
||||
StreamFactoryInterface $streamFactory
|
||||
) {
|
||||
$this->serverRequestFactory = $serverRequestFactory;
|
||||
$this->uriFactory = $uriFactory;
|
||||
$this->uploadedFileFactory = $uploadedFileFactory;
|
||||
$this->streamFactory = $streamFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function fromGlobals(): ServerRequestInterface
|
||||
{
|
||||
$server = $_SERVER;
|
||||
if (false === isset($server['REQUEST_METHOD'])) {
|
||||
$server['REQUEST_METHOD'] = 'GET';
|
||||
}
|
||||
|
||||
$headers = \function_exists('getallheaders') ? getallheaders() : static::getHeadersFromServer($_SERVER);
|
||||
|
||||
return $this->fromArrays($server, $headers, $_COOKIE, $_GET, $_POST, $_FILES, fopen('php://input', 'r') ?: null);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function fromArrays(array $server, array $headers = [], array $cookie = [], array $get = [], array $post = [], array $files = [], $body = null): ServerRequestInterface
|
||||
{
|
||||
$method = $this->getMethodFromEnv($server);
|
||||
$uri = $this->getUriFromEnvWithHTTP($server);
|
||||
$protocol = isset($server['SERVER_PROTOCOL']) ? \str_replace('HTTP/', '', $server['SERVER_PROTOCOL']) : '1.1';
|
||||
|
||||
$serverRequest = $this->serverRequestFactory->createServerRequest($method, $uri, $server);
|
||||
foreach ($headers as $name => $value) {
|
||||
$serverRequest = $serverRequest->withAddedHeader($name, $value);
|
||||
}
|
||||
|
||||
$serverRequest = $serverRequest
|
||||
->withProtocolVersion($protocol)
|
||||
->withCookieParams($cookie)
|
||||
->withQueryParams($get)
|
||||
->withParsedBody($post)
|
||||
->withUploadedFiles($this->normalizeFiles($files));
|
||||
|
||||
if (null === $body) {
|
||||
return $serverRequest;
|
||||
}
|
||||
|
||||
if (\is_resource($body)) {
|
||||
$body = $this->streamFactory->createStreamFromResource($body);
|
||||
} elseif (\is_string($body)) {
|
||||
$body = $this->streamFactory->createStream($body);
|
||||
} elseif (!$body instanceof StreamInterface) {
|
||||
throw new \InvalidArgumentException('The $body parameter to ServerRequestCreator::fromArrays must be string, resource or StreamInterface');
|
||||
}
|
||||
|
||||
return $serverRequest->withBody($body);
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation from Zend\Diactoros\marshalHeadersFromSapi().
|
||||
*/
|
||||
public static function getHeadersFromServer(array $server): array
|
||||
{
|
||||
$headers = [];
|
||||
foreach ($server as $key => $value) {
|
||||
// Apache prefixes environment variables with REDIRECT_
|
||||
// if they are added by rewrite rules
|
||||
if (0 === \strpos($key, 'REDIRECT_')) {
|
||||
$key = \substr($key, 9);
|
||||
|
||||
// We will not overwrite existing variables with the
|
||||
// prefixed versions, though
|
||||
if (\array_key_exists($key, $server)) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if ($value && 0 === \strpos($key, 'HTTP_')) {
|
||||
$name = \strtr(\strtolower(\substr($key, 5)), '_', '-');
|
||||
$headers[$name] = $value;
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($value && 0 === \strpos($key, 'CONTENT_')) {
|
||||
$name = 'content-'.\strtolower(\substr($key, 8));
|
||||
$headers[$name] = $value;
|
||||
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
return $headers;
|
||||
}
|
||||
|
||||
private function getMethodFromEnv(array $environment): string
|
||||
{
|
||||
if (false === isset($environment['REQUEST_METHOD'])) {
|
||||
throw new \InvalidArgumentException('Cannot determine HTTP method');
|
||||
}
|
||||
|
||||
return $environment['REQUEST_METHOD'];
|
||||
}
|
||||
|
||||
private function getUriFromEnvWithHTTP(array $environment): UriInterface
|
||||
{
|
||||
$uri = $this->createUriFromArray($environment);
|
||||
if (empty($uri->getScheme())) {
|
||||
$uri = $uri->withScheme('http');
|
||||
}
|
||||
|
||||
return $uri;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an UploadedFile instance array.
|
||||
*
|
||||
* @param array $files A array which respect $_FILES structure
|
||||
*
|
||||
* @return UploadedFileInterface[]
|
||||
*
|
||||
* @throws \InvalidArgumentException for unrecognized values
|
||||
*/
|
||||
private function normalizeFiles(array $files): array
|
||||
{
|
||||
$normalized = [];
|
||||
|
||||
foreach ($files as $key => $value) {
|
||||
if ($value instanceof UploadedFileInterface) {
|
||||
$normalized[$key] = $value;
|
||||
} elseif (\is_array($value) && isset($value['tmp_name'])) {
|
||||
$normalized[$key] = $this->createUploadedFileFromSpec($value);
|
||||
} elseif (\is_array($value)) {
|
||||
$normalized[$key] = $this->normalizeFiles($value);
|
||||
} else {
|
||||
throw new \InvalidArgumentException('Invalid value in files specification');
|
||||
}
|
||||
}
|
||||
|
||||
return $normalized;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create and return an UploadedFile instance from a $_FILES specification.
|
||||
*
|
||||
* If the specification represents an array of values, this method will
|
||||
* delegate to normalizeNestedFileSpec() and return that return value.
|
||||
*
|
||||
* @param array $value $_FILES struct
|
||||
*
|
||||
* @return array|UploadedFileInterface
|
||||
*/
|
||||
private function createUploadedFileFromSpec(array $value)
|
||||
{
|
||||
if (\is_array($value['tmp_name'])) {
|
||||
return $this->normalizeNestedFileSpec($value);
|
||||
}
|
||||
|
||||
try {
|
||||
$stream = $this->streamFactory->createStreamFromFile($value['tmp_name']);
|
||||
} catch (\RuntimeException $e) {
|
||||
$stream = $this->streamFactory->createStream();
|
||||
}
|
||||
|
||||
return $this->uploadedFileFactory->createUploadedFile(
|
||||
$stream,
|
||||
(int) $value['size'],
|
||||
(int) $value['error'],
|
||||
$value['name'],
|
||||
$value['type']
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalize an array of file specifications.
|
||||
*
|
||||
* Loops through all nested files and returns a normalized array of
|
||||
* UploadedFileInterface instances.
|
||||
*
|
||||
* @param array $files
|
||||
*
|
||||
* @return UploadedFileInterface[]
|
||||
*/
|
||||
private function normalizeNestedFileSpec(array $files = []): array
|
||||
{
|
||||
$normalizedFiles = [];
|
||||
|
||||
foreach (\array_keys($files['tmp_name']) as $key) {
|
||||
$spec = [
|
||||
'tmp_name' => $files['tmp_name'][$key],
|
||||
'size' => $files['size'][$key],
|
||||
'error' => $files['error'][$key],
|
||||
'name' => $files['name'][$key],
|
||||
'type' => $files['type'][$key],
|
||||
];
|
||||
$normalizedFiles[$key] = $this->createUploadedFileFromSpec($spec);
|
||||
}
|
||||
|
||||
return $normalizedFiles;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new uri from server variable.
|
||||
*
|
||||
* @param array $server typically $_SERVER or similar structure
|
||||
*/
|
||||
private function createUriFromArray(array $server): UriInterface
|
||||
{
|
||||
$uri = $this->uriFactory->createUri('');
|
||||
|
||||
if (isset($server['HTTP_X_FORWARDED_PROTO'])) {
|
||||
$uri = $uri->withScheme($server['HTTP_X_FORWARDED_PROTO']);
|
||||
} else {
|
||||
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['SERVER_PORT'])) {
|
||||
$uri = $uri->withPort($server['SERVER_PORT']);
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($server['HTTP_HOST'])) {
|
||||
if (preg_match('/^(.+)\:(\d+)$/', $server['HTTP_HOST'], $matches) === 1) {
|
||||
$uri = $uri->withHost($matches[1])->withPort($matches[2]);
|
||||
} else {
|
||||
$uri = $uri->withHost($server['HTTP_HOST']);
|
||||
}
|
||||
} elseif (isset($server['SERVER_NAME'])) {
|
||||
$uri = $uri->withHost($server['SERVER_NAME']);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
58
vendor/kodus/psr7-server/src/ServerRequestCreatorInterface.php
vendored
Normal file
58
vendor/kodus/psr7-server/src/ServerRequestCreatorInterface.php
vendored
Normal file
@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Nyholm\Psr7Server;
|
||||
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Psr\Http\Message\StreamInterface;
|
||||
|
||||
/**
|
||||
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
|
||||
* @author Martijn van der Ven <martijn@vanderven.se>
|
||||
*/
|
||||
interface ServerRequestCreatorInterface
|
||||
{
|
||||
/**
|
||||
* Create a new server request from the current environment variables.
|
||||
* Defaults to a GET request to minimise the risk of an \InvalidArgumentException.
|
||||
* Includes the current request headers as supplied by the server through `getallheaders()`.
|
||||
* If `getallheaders()` is unavailable on the current server it will fallback to its own `getHeadersFromServer()` method.
|
||||
* Defaults to php://input for the request body.
|
||||
*
|
||||
* @throws \InvalidArgumentException if no valid method or URI can be determined
|
||||
*/
|
||||
public function fromGlobals(): ServerRequestInterface;
|
||||
|
||||
/**
|
||||
* Create a new server request from a set of arrays.
|
||||
*
|
||||
* @param array $server typically $_SERVER or similar structure
|
||||
* @param array $headers typically the output of getallheaders() or similar structure
|
||||
* @param array $cookie typically $_COOKIE or similar structure
|
||||
* @param array $get typically $_GET or similar structure
|
||||
* @param array $post typically $_POST or similar structure
|
||||
* @param array $files typically $_FILES or similar structure
|
||||
* @param StreamInterface|resource|string|null $body Typically stdIn
|
||||
*
|
||||
* @throws \InvalidArgumentException if no valid method or URI can be determined
|
||||
*/
|
||||
public function fromArrays(
|
||||
array $server,
|
||||
array $headers = [],
|
||||
array $cookie = [],
|
||||
array $get = [],
|
||||
array $post = [],
|
||||
array $files = [],
|
||||
$body = null
|
||||
): ServerRequestInterface;
|
||||
|
||||
/**
|
||||
* Get parsed headers from ($_SERVER) array.
|
||||
*
|
||||
* @param array $server typically $_SERVER or similar structure
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function getHeadersFromServer(array $server): array;
|
||||
}
|
Reference in New Issue
Block a user