Skip to content

Commit

Permalink
Merge pull request #12 from kodedphp/psr-18
Browse files Browse the repository at this point in the history
  • Loading branch information
kodeart authored Mar 25, 2020
2 parents fe43bea + 05eaa5c commit 3e15a8a
Show file tree
Hide file tree
Showing 38 changed files with 800 additions and 311 deletions.
19 changes: 10 additions & 9 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
*.php diff=php

/build export-ignore
/Tests export-ignore
/vendor export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
/.env export-ignore
/phpunit.xml export-ignore
/composer.lock export-ignore
/*.yml export-ignore
/build export-ignore
/Tests export-ignore
/vendor export-ignore
/diagrams export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
/.env export-ignore
/phpunit.xml.dist export-ignore
/composer.lock export-ignore
/*.yml export-ignore
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,3 @@ vendor
.idea
.tmp
composer.lock
*.yml
!.travis.yml
24 changes: 24 additions & 0 deletions .scrutinizer.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
build:
nodes:
analysis:
tests:
stop_on_failure: true
override:
- php-scrutinizer-run
environment:
php:
version: '7.2'
dependencies:
override:
- composer install --no-interaction --prefer-source

filter:
excluded_paths:
- 'Tests/'
- 'vendor/'
- 'diagrams/'
- 'build/'

tools:
php_analyzer: true
external_code_coverage: true
7 changes: 5 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
language: php

php:
- 7.1
- 7.2
- 7.3
- 7.4
- nightly

matrix:
fast_finish: true
allow_failures:
- php: nightly

install:
- composer update -o --no-interaction --prefer-stable --ignore-platform-reqs
Expand All @@ -15,7 +18,7 @@ script:
- vendor/bin/phpunit --coverage-clover build/coverage/clover.xml

after_success:
- travis_retry vendor/bin/codacycoverage clover build/coverage/clover.xml
- travis_retry vendor/bin/ocular code-coverage:upload --format=php-clover build/coverage/clover.xml

sudo: false

Expand Down
57 changes: 31 additions & 26 deletions AcceptHeaderNegotiator.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,14 @@
namespace Koded\Http;

/**
*
*
* Content negotiation module.
*
* Supported access headers:
* Supported HTTP/1.1 Accept headers:
*
* Access
* Access-Language
* Access-Charset
* Access-Encoding
* Accept
* Accept-Language
* Accept-Charset
* Accept-Encoding
*
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Content_negotiation
*/
Expand Down Expand Up @@ -59,7 +57,8 @@ public function match(string $accepts): AcceptHeader
* The consuming clients should handle this according to
* their internal logic. This is much better then throwing
* exceptions which must be handled in every place where
* match() is called. The client may issue a 406 status code.
* match() is called. For example, the client may issue a
* 406 status code and be done with it.
*/
$types[] = new class('*;q=0') extends AcceptHeader {};
}
Expand Down Expand Up @@ -101,32 +100,34 @@ public function __construct(string $header)
$type = array_shift($bits);

if (!empty($type) && !preg_match('~^(\*|[a-z0-9._]+)([/|_-])?(\*|[a-z0-9.\-_+]+)?$~i', $type, $matches)) {
throw new InvalidArgumentException(sprintf('"%s" is not a valid Access header', $header), StatusCode::NOT_ACCEPTABLE);
throw new InvalidArgumentException(sprintf('"%s" is not a valid Access header', $header),
StatusCode::NOT_ACCEPTABLE);
}

$this->separator = $matches[2] ?? '/';
[$type, $subtype] = explode($this->separator, $type, 2) + [1 => '*'];

if ('*' === $type && '*' !== $subtype) {
// @see https://tools.ietf.org/html/rfc7231#section-5.3.2
throw new InvalidArgumentException(sprintf('"%s" is not a valid Access header', $header), StatusCode::NOT_ACCEPTABLE);
throw new InvalidArgumentException(sprintf('"%s" is not a valid Access header', $header),
StatusCode::NOT_ACCEPTABLE);
}

// @see https://tools.ietf.org/html/rfc7540#section-8.1.2
$this->type = strtolower($type);

/* Uses a simple heuristic to check if subtype is part of
* some obscure media type like "vnd.api-v1+json".
*
* NOTE: It is a waste of time to negotiate on the basis
* of obscure parameters while using a meaningless media
* type like "vnd.whatever". But the web world is a big mess
* and this module can handle the Dunning-Kruger effect.
*/
$this->subtype = explode('+', $subtype)[1] ?? $subtype;
$this->catchAll = '*' === $this->type && '*' === $this->subtype;

parse_str(join('&', $bits), $this->params);
/* NOTE: It is a waste of time to negotiate on the basis
* of obscure parameters while using a meaningless media
* type like "vnd.whatever". But the IT world is a big
* mess for now and this module supports ignorant devs.
*/
$this->quality = (float)($this->params['q'] ?? 1);
unset($this->params['q']);
}
Expand All @@ -138,16 +139,10 @@ public function __toString(): string
}


public function quality(): float
{
return $this->quality;
}


public function value(): string
{
// The header is explicitly rejected
if (0.0 === $this->quality()) {
if (0.0 === $this->quality) {
return '';
}

Expand All @@ -160,6 +155,12 @@ public function value(): string
}


public function quality(): float
{
return $this->quality;
}


public function weight(): float
{
return $this->weight;
Expand All @@ -168,15 +169,16 @@ public function weight(): float
/**
* @internal
*
* This method finds the best match from the accept header,
* including all the stupidity that may be passed by the
* ignorant developers who do not follow RFC standards.
*
* @param AcceptHeader $accept The accept header part
* @param AcceptHeader[] $matches Matched types
*
* @return bool TRUE if the accept header part is a match
* against the supported (this) header part
*
* This method finds the best match for the Accept header,
* including all the nonsense that may be passed by the
* developers who do not follow RFC standards.
*
*/
public function matches(AcceptHeader $accept, array &$matches = null): bool
{
Expand All @@ -193,18 +195,21 @@ public function matches(AcceptHeader $accept, array &$matches = null): bool
$accept->type = $this->type;
$accept->subtype = $this->subtype;
$matches[] = $accept;

return true;
}

// Explicitly denied
if (0.0 === $this->quality) {
$matches[] = clone $this;

return true;
}

// Explicitly denied
if (0.0 === $accept->quality) {
$matches[] = $accept;

return true;
}

Expand Down
15 changes: 4 additions & 11 deletions CallableStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,6 @@ class CallableStream implements StreamInterface
/** @var bool If callable is Generator instance */
private $isGenerator = false;

/**
* CallableStream constructor.
*
* @param callable $callable
*
* @throws \ReflectionException
*/
public function __construct(callable $callable)
{
$this->callable = $callable;
Expand Down Expand Up @@ -101,15 +94,15 @@ public function write($string): int

public function read($length): string
{
$contents = '';
$content = '';

if (null === $this->callable) {
return $contents;
return $content;
}

try {
foreach ($this->reader($length) as $chunk) {
$contents .= $chunk;
$content .= $chunk;
$this->position += mb_strlen($chunk);
}
} catch (Exception $e) {
Expand All @@ -118,7 +111,7 @@ public function read($length): string
$this->callable = null;
}

return $contents;
return $content;
}

public function getContents(): string
Expand Down
8 changes: 6 additions & 2 deletions Client/ClientFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

class ClientFactory
{

const CURL = 0;
const PHP = 1;

Expand Down Expand Up @@ -59,6 +58,11 @@ public function head($uri, array $headers = []): HttpRequestClient
return $this->create(Request::HEAD, $uri, null, $headers)->maxRedirects(0);
}

public function psr18(): HttpRequestClient
{
return $this->create(Request::HEAD, '');
}

protected function create(string $method, $uri, $body = null, array $headers = []): HttpRequestClient
{
switch ($this->clientType) {
Expand All @@ -72,4 +76,4 @@ protected function create(string $method, $uri, $body = null, array $headers = [
throw new InvalidArgumentException("{$this->clientType} is not a valid HTTP client");
}
}
}
}
Loading

0 comments on commit 3e15a8a

Please sign in to comment.