diff --git a/.gitignore b/.gitignore index a67d42b32f8693498f7996ecce27df94a04728f6..c8153b578263d52537c55d921652bc7d7019e3c9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,2 @@ -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 diff --git a/composer.json b/composer.json new file mode 100644 index 0000000000000000000000000000000000000000..19d4d9fd2b3773518d16f74d2d85bf01c06c9bb9 --- /dev/null +++ b/composer.json @@ -0,0 +1,19 @@ +{ + "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"] + } +} diff --git a/src/ServerRequest.php b/src/ServerRequest.php new file mode 100644 index 0000000000000000000000000000000000000000..ab639490e734a2538b44b9f0f0e8db6207a432ee --- /dev/null +++ b/src/ServerRequest.php @@ -0,0 +1,35 @@ +<?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]); + } +} diff --git a/src/functions.php b/src/functions.php new file mode 100644 index 0000000000000000000000000000000000000000..5db6a33af3ac75a6031adba2bc29ef599cc3e06b --- /dev/null +++ b/src/functions.php @@ -0,0 +1,27 @@ +<?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); + } + } +}