Skip to content
Snippets Groups Projects
Verified Commit 54dbf057 authored by Thomas Flori's avatar Thomas Flori
Browse files

add basic functionality

parent 977e137f
No related branches found
No related tags found
No related merge requests found
composer.phar
/composer.lock
/vendor/
# Commit your application's lock file https://getcomposer.org/doc/01-basic-usage.md#commit-your-composer-lock-file-to-version-control
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file
# composer.lock
{
"name": "tflori/tal",
"description": "A guzzle/psr-7 wrapper to add additional functionality like sending responses and managing cookies",
"type": "library",
"license": "MIT",
"require": {
"guzzlehttp/psr7": "^1.4.2"
},
"require-dev": {
"phpunit/phpunit": "^7.2.7",
"squizlabs/php_codesniffer": "^3.3.1"
},
"autoload": {
"psr-4": {
"Tal\\": "src"
},
"files": ["src/functions.php"]
}
}
<?php
namespace Tal;
use GuzzleHttp\Psr7\ServerRequest as BaseServerRequest;
class ServerRequest extends BaseServerRequest
{
public static function fromGlobals()
{
$method = isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : 'GET';
$headers = function_exists('getallheaders') ? getallheaders() : [];
$uri = self::getUriFromGlobals();
$body = new LazyOpenStream('php://input', 'r+');
$protocol = isset($_SERVER['SERVER_PROTOCOL']) ? str_replace('HTTP/', '', $_SERVER['SERVER_PROTOCOL']) : '1.1';
$serverRequest = new static($method, $uri, $headers, $body, $protocol, $_SERVER);
return $serverRequest
->withCookieParams($_COOKIE)
->withQueryParams($_GET)
->withParsedBody($_POST)
->withUploadedFiles(self::normalizeFiles($_FILES));
}
public function getCookie(string $name, $default = null)
{
return $this->getCookieParams()[$name] ?? $default;
}
public function hasCookie(string $name)
{
return isset($this->getCookieParams()[$name]);
}
}
<?php
use Psr\Http\Message\ResponseInterface;
if (!function_exists('sendResponse')) {
function sendResponse(ResponseInterface $response)
{
$http_line = sprintf('HTTP/%s %s %s',
$response->getProtocolVersion(),
$response->getStatusCode(),
$response->getReasonPhrase()
);
header($http_line, true, $response->getStatusCode());
foreach ($response->getHeaders() as $name => $values) {
foreach ($values as $value) {
header("$name: $value", false);
}
}
$stream = $response->getBody();
if ($stream->isSeekable()) {
$stream->rewind();
}
while (!$stream->eof()) {
echo $stream->read(1024 * 8);
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment