Skip to content

Commit

Permalink
Get rid of dependency from deprecated php-http/message-factory
Browse files Browse the repository at this point in the history
  • Loading branch information
murat11 committed Apr 26, 2023
1 parent 7f77c0e commit e02a76d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
"require": {
"php": ">=7.2",
"psr/http-message": "^1.1 || ^2.0",
"php-http/message-factory": "^1.0",
"psr/http-factory": "^1.0"
},
"require-dev": {
Expand Down
29 changes: 27 additions & 2 deletions src/Factory/HttplugFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@

namespace Nyholm\Psr7\Factory;

use Http\Message\{MessageFactory, StreamFactory, UriFactory};
use Nyholm\Psr7\{Request, Response, Stream, Uri};
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamFactoryInterface;
use Psr\Http\Message\StreamInterface;
use Psr\Http\Message\UriFactoryInterface;
use Psr\Http\Message\UriInterface;

/**
Expand All @@ -17,7 +20,7 @@
*
* @final This class should never be extended. See https:/Nyholm/psr7/blob/master/doc/final.md
*/
class HttplugFactory implements MessageFactory, StreamFactory, UriFactory
class HttplugFactory implements RequestFactoryInterface, ResponseFactoryInterface, StreamFactoryInterface, UriFactoryInterface
{
public function createRequest($method, $uri, array $headers = [], $body = null, $protocolVersion = '1.1'): RequestInterface
{
Expand All @@ -42,4 +45,26 @@ public function createUri($uri = ''): UriInterface

return new Uri($uri);
}

public function createStreamFromFile(string $filename, string $mode = 'r'): StreamInterface
{
if ('' === $filename) {
throw new \RuntimeException('Path cannot be empty');
}

if (false === $resource = @\fopen($filename, $mode)) {
if ('' === $mode || false === \in_array($mode[0], ['r', 'w', 'a', 'x', 'c'], true)) {
throw new \InvalidArgumentException(\sprintf('The mode "%s" is invalid.', $mode));
}

throw new \RuntimeException(\sprintf('The file "%s" cannot be opened: %s', $filename, \error_get_last()['message'] ?? ''));
}

return Stream::create($resource);
}

public function createStreamFromResource($resource): StreamInterface
{
return Stream::create($resource);
}
}

0 comments on commit e02a76d

Please sign in to comment.