Skip to content

Commit

Permalink
HttpFactory PSR-17
Browse files Browse the repository at this point in the history
  • Loading branch information
ProklUng committed May 24, 2021
1 parent a21577b commit 29c6716
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 0 deletions.
4 changes: 4 additions & 0 deletions config/base.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ services:
class: Symfony\Component\HttpFoundation\Response
factory: ['@bitrix.response.convertor', 'response']

# Http factory стандарта PSR-17
psr17.http_factory:
class: Prokl\ServiceProvider\Services\PSR\PSR17\HttpFactory

# Request приложения.
global.request:
class: Symfony\Component\HttpFoundation\Request
Expand Down
105 changes: 105 additions & 0 deletions src/Services/PSR/PSR17/HttpFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php

namespace Prokl\ServiceProvider\Services\PSR\PSR17;

use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Psr7\ServerRequest;
use GuzzleHttp\Psr7\UploadedFile;
use GuzzleHttp\Psr7\Uri;
use GuzzleHttp\Psr7\Utils;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestFactoryInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\StreamFactoryInterface;
use Psr\Http\Message\StreamInterface;
use Psr\Http\Message\UploadedFileFactoryInterface;
use Psr\Http\Message\UploadedFileInterface;
use Psr\Http\Message\UriFactoryInterface;
use Psr\Http\Message\UriInterface;

/**
* Class HttpFactory
* @package Prokl\ServiceProvider\Services\PSR\PSR17
*/
class HttpFactory implements
RequestFactoryInterface,
ResponseFactoryInterface,
ServerRequestFactoryInterface,
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, [], null, '1.1', $reasonPhrase);
}

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

/**
* @inheritDoc
*/
public function createStream(string $content = ''): StreamInterface
{
return Utils::streamFor($content);
}

/**
* @inheritDoc
*/
public function createStreamFromFile(string $filename, string $mode = 'r'): StreamInterface
{
return Utils::streamFor(fopen($filename, $mode));
}

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

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

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

0 comments on commit 29c6716

Please sign in to comment.