Skip to content

Commit

Permalink
🛀 move PSR-17 factories to one ugly HTTPFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
codemasher committed Mar 2, 2024
1 parent 3741e94 commit 6ba4a17
Show file tree
Hide file tree
Showing 14 changed files with 127 additions and 237 deletions.
12 changes: 6 additions & 6 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@
</groups>
<php>
<const name="TEST_IS_CI" value="true"/>
<const name="REQUEST_FACTORY" value="chillerlan\HTTP\Psr17\RequestFactory"/>
<const name="RESPONSE_FACTORY" value="chillerlan\HTTP\Psr17\ResponseFactory"/>
<const name="SERVER_REQUEST_FACTORY" value="chillerlan\HTTP\Psr17\ServerRequestFactory"/>
<const name="STREAM_FACTORY" value="chillerlan\HTTP\Psr17\StreamFactory"/>
<const name="UPLOADED_FILE_FACTORY" value="chillerlan\HTTP\Psr17\UploadedFileFactory"/>
<const name="URI_FACTORY" value="chillerlan\HTTP\Psr17\UriFactory"/>
<const name="REQUEST_FACTORY" value="chillerlan\HTTP\Common\HTTPFactory"/>
<const name="RESPONSE_FACTORY" value="chillerlan\HTTP\Common\HTTPFactory"/>
<const name="SERVER_REQUEST_FACTORY" value="chillerlan\HTTP\Common\HTTPFactory"/>
<const name="STREAM_FACTORY" value="chillerlan\HTTP\Common\HTTPFactory"/>
<const name="UPLOADED_FILE_FACTORY" value="chillerlan\HTTP\Common\HTTPFactory"/>
<const name="URI_FACTORY" value="chillerlan\HTTP\Common\HTTPFactory"/>
</php>
</phpunit>
3 changes: 1 addition & 2 deletions src/Common/CurlMultiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

namespace chillerlan\HTTP\Common;

use chillerlan\HTTP\Psr17\ResponseFactory;
use chillerlan\HTTP\Psr18\ClientException;
use chillerlan\HTTP\HTTPOptions;
use chillerlan\Settings\SettingsContainerInterface;
Expand Down Expand Up @@ -49,7 +48,7 @@ class CurlMultiClient implements LoggerAwareInterface{
public function __construct(
protected MultiResponseHandlerInterface $multiResponseHandler,
protected HTTPOptions|SettingsContainerInterface $options = new HTTPOptions,
protected ResponseFactoryInterface $responseFactory = new ResponseFactory,
protected ResponseFactoryInterface $responseFactory = new HTTPFactory,
protected LoggerInterface $logger = new NullLogger,
){
$this->curl_multi = curl_multi_init();
Expand Down
105 changes: 105 additions & 0 deletions src/Common/HTTPFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php
/**
* Class HTTPFactory
*
* @created 02.03.2024
* @author smiley <[email protected]>
* @copyright 2024 smiley
* @license MIT
*/

namespace chillerlan\HTTP\Common;

use chillerlan\HTTP\Psr7\{Request, Response, ServerRequest, Stream, UploadedFile, Uri};
use chillerlan\HTTP\Utils\StreamUtil;
use Fig\Http\Message\{RequestMethodInterface, StatusCodeInterface};
use Psr\Http\Message\{
RequestFactoryInterface, RequestInterface, ResponseFactoryInterface, ResponseInterface, ServerRequestFactoryInterface,
ServerRequestInterface, StreamFactoryInterface, StreamInterface, UploadedFileFactoryInterface, UploadedFileInterface,
UriFactoryInterface, UriInterface
};
use RuntimeException;
use function is_file,is_readable;
use const UPLOAD_ERR_OK;

/**
* Implements the PSR-17 HTTP factories
*/
class HTTPFactory implements
RequestFactoryInterface,
ResponseFactoryInterface,
RequestMethodInterface,
ServerRequestFactoryInterface,
StatusCodeInterface,
StreamFactoryInterface,
UploadedFileFactoryInterface,
UriFactoryInterface {

/**
* @inheritDoc
*/
public function createRequest(string $method, $uri):RequestInterface{
return new Request($method, $uri);
}

/**
* @inheritDoc
*/
public function createResponse(int $code = 200, string $reasonPhrase = ''):ResponseInterface{
return new Response($code, $reasonPhrase);
}

/**
* @inheritDoc
*/
public function createStream(string $content = ''):StreamInterface{
return FactoryUtils::createStream(content: $content, rewind: false);
}

/**
* @inheritDoc
*/
public function createStreamFromFile(string $filename, string $mode = 'r'):StreamInterface{

if(empty($filename) || !is_file($filename) || !is_readable($filename)){
throw new RuntimeException('invalid file');
}

return new Stream(StreamUtil::tryFopen($filename, $mode));
}

/**
* @inheritDoc
*/
public function createStreamFromResource($resource):StreamInterface{
return new Stream($resource);
}

/**
* @inheritDoc
*/
public function createUri(string $uri = ''):UriInterface{
return new Uri($uri);
}

/**
* @inheritDoc
*/
public function createServerRequest(string $method, $uri, array $serverParams = []):ServerRequestInterface{
return new ServerRequest($method, $uri, $serverParams);
}

/**
* @inheritDoc
*/
public function createUploadedFile(
StreamInterface $stream,
int|null $size = null,
int $error = UPLOAD_ERR_OK,
string|null $clientFilename = null,
string|null $clientMediaType = null,
):UploadedFileInterface{
return new UploadedFile($stream, ($size ?? (int)$stream->getSize()), $error, $clientFilename, $clientMediaType);
}

}
3 changes: 1 addition & 2 deletions src/Common/MultipartStreamBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

namespace chillerlan\HTTP\Common;

use chillerlan\HTTP\Psr17\StreamFactory;
use chillerlan\HTTP\Psr7\Message;
use chillerlan\HTTP\Utils\{HeaderUtil, MessageUtil, StreamUtil};
use Psr\Http\Message\{MessageInterface, StreamFactoryInterface, StreamInterface};
Expand All @@ -35,7 +34,7 @@ class MultipartStreamBuilder{
* MultipartStreamBuilder constructor
*/
public function __construct(
protected StreamFactoryInterface $streamFactory = new StreamFactory,
protected StreamFactoryInterface $streamFactory = new HTTPFactory,
){
$this->reset();
}
Expand Down
4 changes: 2 additions & 2 deletions src/Psr15/QueueDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

namespace chillerlan\HTTP\Psr15;

use chillerlan\HTTP\Psr17\ResponseFactory;
use chillerlan\HTTP\Common\HTTPFactory;
use Psr\Http\Message\{ResponseInterface, ServerRequestInterface};
use Psr\Http\Server\{MiddlewareInterface, RequestHandlerInterface};

Expand All @@ -32,7 +32,7 @@ public function __construct(
iterable|null $middlewareStack = null,
RequestHandlerInterface|null $fallbackHandler = null,
){
$fallbackHandler ??= new EmptyResponseHandler(new ResponseFactory, 500);
$fallbackHandler ??= new EmptyResponseHandler(new HTTPFactory, 500);

$this
->addStack(($middlewareStack ?? []))
Expand Down
31 changes: 0 additions & 31 deletions src/Psr17/RequestFactory.php

This file was deleted.

31 changes: 0 additions & 31 deletions src/Psr17/ResponseFactory.php

This file was deleted.

31 changes: 0 additions & 31 deletions src/Psr17/ServerRequestFactory.php

This file was deleted.

53 changes: 0 additions & 53 deletions src/Psr17/StreamFactory.php

This file was deleted.

37 changes: 0 additions & 37 deletions src/Psr17/UploadedFileFactory.php

This file was deleted.

30 changes: 0 additions & 30 deletions src/Psr17/UriFactory.php

This file was deleted.

Loading

0 comments on commit 6ba4a17

Please sign in to comment.