Skip to content

Commit

Permalink
add php 8.4 support
Browse files Browse the repository at this point in the history
  • Loading branch information
chriskapp committed Feb 2, 2025
1 parent 23da2fa commit 3a1104b
Show file tree
Hide file tree
Showing 17 changed files with 132 additions and 79 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ jobs:
php-versions:
- 8.2
- 8.3
- 8.4
steps:
- name: Checkout
uses: actions/checkout@v4
Expand Down
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
"require": {
"php": ">=8.0",
"psx/uri": "^3.0",
"psr/log": "^1.0 || ^2.0 || ^3.0",
"psr/http-message": "^1.0",
"psr/log": "^1.0|^2.0|^3.0",
"psr/http-message": "^1.0|^2.0",
"guzzlehttp/guzzle": "^7.0"
},
"require-dev": {
"phpunit/phpunit": "^9.0",
"vimeo/psalm": "^5.0"
"phpunit/phpunit": "^10.0",
"vimeo/psalm": "^5.0|^6.0"
},
"autoload": {
"psr-4": {
Expand Down
4 changes: 4 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@
<directory name="vendor" />
</ignoreFiles>
</projectFiles>
<issueHandlers>
<UnusedClass errorLevel="suppress" />
<PossiblyUnusedMethod errorLevel="suppress" />
</issueHandlers>
</psalm>
34 changes: 34 additions & 0 deletions src/Exception/StreamException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
/*
* PSX is an open source PHP framework to develop RESTful APIs.
* For the current version and information visit <https://phpsx.org>
*
* Copyright 2010-2023 Christoph Kappestein <christoph.kappestein@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace PSX\Http\Exception;

use RuntimeException;

/**
* StreamException
*
* @author Christoph Kappestein <christoph.kappestein@gmail.com>
* @license http://www.apache.org/licenses/LICENSE-2.0
* @link https://phpsx.org
*/
class StreamException extends RuntimeException
{
}
2 changes: 1 addition & 1 deletion src/Filter/Backstage.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function handle(RequestInterface $request, ResponseInterface $response, F

if (stripos($accept, 'text/html') !== false && is_file($this->file)) {
$response->setHeader('Content-Type', 'text/html');
$response->getBody()->write(file_get_contents($this->file));
$response->getBody()->write((string) file_get_contents($this->file));
} else {
$filterChain->handle($request, $response);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Filter/BasicAuthentication.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function __construct(Closure $isValidCallback)
throw new BadRequestException('Invalid username or password');
});

$this->onMissing(function (ResponseInterface $response) {
$this->onMissing(function () {
$params = array(
'realm' => 'psx',
);
Expand Down
6 changes: 5 additions & 1 deletion src/Stream/BufferedStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

namespace PSX\Http\Stream;

use PSX\Http\Exception\StreamException;
use PSX\Http\StreamInterface;

/**
Expand All @@ -42,14 +43,17 @@ public function __construct(StreamInterface $stream)
$this->source = $stream;
}

protected function call()
protected function call(): void
{
if ($this->filled) {
return;
}

$source = $this->source->detach();
$buffer = fopen('php://temp', 'r+');
if ($buffer === false) {
throw new StreamException('Unable to open stream');
}

if (is_resource($source)) {
stream_copy_to_stream($source, $buffer, -1, 0);
Expand Down
2 changes: 1 addition & 1 deletion src/Stream/LazyStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function __construct(string $uri, string $mode = 'rb')
$this->mode = $mode;
}

protected function call()
protected function call(): void
{
if ($this->opened) {
return;
Expand Down
65 changes: 40 additions & 25 deletions src/Stream/Stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
namespace PSX\Http\Stream;

use InvalidArgumentException;
use PSX\Http\Exception\StreamException;
use PSX\Http\StreamInterface;

/**
Expand All @@ -46,7 +47,7 @@ public function __construct($resource)
$this->setResource($resource);
}

public function close()
public function close(): void
{
if ($this->resource) {
fclose($this->resource);
Expand All @@ -65,27 +66,35 @@ public function detach()
return $handle;
}

public function getSize()
public function getSize(): ?int
{
if ($this->resource) {
$stat = fstat($this->resource);
if ($stat === false) {
throw new StreamException('Unable to get stats from stream');
}

return $stat['size'] ?? null;
}

return null;
}

public function tell()
public function tell(): int
{
if ($this->resource) {
return ftell($this->resource);
$return = ftell($this->resource);
if ($return === false) {
throw new StreamException('Unable to tell the position from stream');
}

return $return;
}

return 0;
}

public function eof()
public function eof(): bool
{
if ($this->resource) {
return feof($this->resource);
Expand All @@ -94,58 +103,64 @@ public function eof()
return true;
}

public function rewind()
public function rewind(): void
{
if ($this->resource) {
return rewind($this->resource);
rewind($this->resource);
}

return true;
}

public function isSeekable()
public function isSeekable(): bool
{
return $this->seekable;
}

public function seek($offset, $whence = SEEK_SET)
public function seek(int $offset, int $whence = SEEK_SET): void
{
if ($this->resource && $this->seekable) {
return fseek($this->resource, $offset, $whence);
fseek($this->resource, $offset, $whence);
}

return false;
}

public function isWritable()
public function isWritable(): bool
{
return $this->writable;
}

public function write($string)
public function write(string $string): int
{
if ($this->resource && $this->writable) {
return fwrite($this->resource, $string);
$result = fwrite($this->resource, $string);
if ($result === false) {
throw new StreamException('Unable to write stream');
}

return $result;
}

return 0;
}

public function isReadable()
public function isReadable(): bool
{
return $this->readable;
}

public function read($length)
public function read(int $length): string
{
if ($this->resource && $this->readable && $length > 0) {
return fread($this->resource, $length);
$content = fread($this->resource, $length);
if ($content === false) {
throw new StreamException('Unable to read stream');
}

return $content;
}

return '';
}

public function getContents()
public function getContents(): string
{
if ($this->resource && $this->readable) {
return (string) stream_get_contents($this->resource);
Expand All @@ -154,22 +169,22 @@ public function getContents()
return '';
}

public function getMetadata($key = null)
public function getMetadata(?string $key = null)
{
if ($this->resource) {
$meta = stream_get_meta_data($this->resource);

if ($key === null) {
return $meta;
} else {
return isset($meta[$key]) ? $meta[$key] : null;
return $meta[$key] ?? null;
}
}

return $key === null ? array() : null;
}

public function __toString()
public function __toString(): string
{
if ($this->resource && $this->readable) {
return (string) stream_get_contents($this->resource, -1, 0);
Expand All @@ -178,7 +193,7 @@ public function __toString()
return '';
}

protected function setResource($resource)
protected function setResource($resource): void
{
$meta = stream_get_meta_data($resource);
$mode = $meta['mode'] . ' ';
Expand Down
Loading

0 comments on commit 3a1104b

Please sign in to comment.