2015-08-27 12:03:05 -07:00
< ? php
/*
* This file is part of the Symfony package .
*
* ( c ) Fabien Potencier < fabien @ symfony . com >
*
* For the full copyright and license information , please view the LICENSE
* file that was distributed with this source code .
*/
namespace Symfony\Bridge\PsrHttpMessage\Factory ;
2018-11-23 12:29:20 +00:00
use Psr\Http\Message\ResponseFactoryInterface ;
use Psr\Http\Message\ServerRequestFactoryInterface ;
use Psr\Http\Message\StreamFactoryInterface ;
use Psr\Http\Message\UploadedFileFactoryInterface ;
use Psr\Http\Message\UploadedFileInterface ;
2015-08-27 12:03:05 -07:00
use Symfony\Bridge\PsrHttpMessage\HttpMessageFactoryInterface ;
use Symfony\Component\HttpFoundation\BinaryFileResponse ;
use Symfony\Component\HttpFoundation\File\UploadedFile ;
use Symfony\Component\HttpFoundation\Request ;
use Symfony\Component\HttpFoundation\Response ;
use Symfony\Component\HttpFoundation\StreamedResponse ;
/**
2018-11-23 12:29:20 +00:00
* Builds Psr\HttpMessage instances using a PSR - 17 implementation .
2015-08-27 12:03:05 -07:00
*
2018-11-23 12:29:20 +00:00
* @ author Antonio J . García Lagar < aj @ garcialagar . es >
2015-08-27 12:03:05 -07:00
*/
2018-11-23 12:29:20 +00:00
class PsrHttpFactory implements HttpMessageFactoryInterface
2015-08-27 12:03:05 -07:00
{
2018-11-23 12:29:20 +00:00
private $serverRequestFactory ;
private $streamFactory ;
private $uploadedFileFactory ;
private $responseFactory ;
public function __construct ( ServerRequestFactoryInterface $serverRequestFactory , StreamFactoryInterface $streamFactory , UploadedFileFactoryInterface $uploadedFileFactory , ResponseFactoryInterface $responseFactory )
2015-08-27 12:03:05 -07:00
{
2018-11-23 12:29:20 +00:00
$this -> serverRequestFactory = $serverRequestFactory ;
$this -> streamFactory = $streamFactory ;
$this -> uploadedFileFactory = $uploadedFileFactory ;
$this -> responseFactory = $responseFactory ;
2015-08-27 12:03:05 -07:00
}
/**
* { @ inheritdoc }
*/
public function createRequest ( Request $symfonyRequest )
{
2018-11-23 12:29:20 +00:00
$request = $this -> serverRequestFactory -> createServerRequest (
$symfonyRequest -> getMethod (),
$symfonyRequest -> getSchemeAndHttpHost () . $symfonyRequest -> getRequestUri (),
$symfonyRequest -> server -> all ()
);
foreach ( $symfonyRequest -> headers -> all () as $name => $value ) {
$request = $request -> withHeader ( $name , $value );
}
2015-08-27 12:03:05 -07:00
2017-04-13 15:53:35 +01:00
if ( PHP_VERSION_ID < 50600 ) {
2018-11-23 12:29:20 +00:00
$body = $this -> streamFactory -> createStreamFromFile ( 'php://temp' , 'wb+' );
2015-08-27 12:03:05 -07:00
$body -> write ( $symfonyRequest -> getContent ());
2017-04-13 15:53:35 +01:00
} else {
2018-11-23 12:29:20 +00:00
$body = $this -> streamFactory -> createStreamFromResource ( $symfonyRequest -> getContent ( true ));
2015-08-27 12:03:05 -07:00
}
$request = $request
2018-11-23 12:29:20 +00:00
-> withBody ( $body )
-> withUploadedFiles ( $this -> getFiles ( $symfonyRequest -> files -> all ()))
2015-08-27 12:03:05 -07:00
-> withCookieParams ( $symfonyRequest -> cookies -> all ())
-> withQueryParams ( $symfonyRequest -> query -> all ())
-> withParsedBody ( $symfonyRequest -> request -> all ())
;
foreach ( $symfonyRequest -> attributes -> all () as $key => $value ) {
$request = $request -> withAttribute ( $key , $value );
}
return $request ;
}
/**
* Converts Symfony uploaded files array to the PSR one .
*
* @ param array $uploadedFiles
*
* @ return array
*/
private function getFiles ( array $uploadedFiles )
{
$files = array ();
foreach ( $uploadedFiles as $key => $value ) {
2017-04-13 15:53:35 +01:00
if ( null === $value ) {
2018-11-23 12:29:20 +00:00
$files [ $key ] = $this -> uploadedFileFactory -> createUploadedFile ( $this -> streamFactory -> createStream (), 0 , UPLOAD_ERR_NO_FILE );
2017-04-13 15:53:35 +01:00
continue ;
}
2015-08-27 12:03:05 -07:00
if ( $value instanceof UploadedFile ) {
$files [ $key ] = $this -> createUploadedFile ( $value );
} else {
$files [ $key ] = $this -> getFiles ( $value );
}
}
return $files ;
}
/**
* Creates a PSR - 7 UploadedFile instance from a Symfony one .
*
* @ param UploadedFile $symfonyUploadedFile
*
* @ return UploadedFileInterface
*/
private function createUploadedFile ( UploadedFile $symfonyUploadedFile )
{
2018-11-23 12:29:20 +00:00
return $this -> uploadedFileFactory -> createUploadedFile (
$this -> streamFactory -> createStreamFromFile (
$symfonyUploadedFile -> getRealPath ()
),
( int ) $symfonyUploadedFile -> getSize (),
2015-08-27 12:03:05 -07:00
$symfonyUploadedFile -> getError (),
$symfonyUploadedFile -> getClientOriginalName (),
$symfonyUploadedFile -> getClientMimeType ()
);
}
/**
* { @ inheritdoc }
*/
public function createResponse ( Response $symfonyResponse )
{
2018-11-23 12:29:20 +00:00
$response = $this -> responseFactory -> createResponse ( $symfonyResponse -> getStatusCode ());
2015-08-27 12:03:05 -07:00
if ( $symfonyResponse instanceof BinaryFileResponse ) {
2018-11-23 12:29:20 +00:00
$stream = $this -> streamFactory -> createStreamFromFile (
$symfonyResponse -> getFile () -> getPathname ()
);
2015-08-27 12:03:05 -07:00
} else {
2018-11-23 12:29:20 +00:00
$stream = $this -> streamFactory -> createStreamFromFile ( 'php://temp' , 'wb+' );
2015-08-27 12:03:05 -07:00
if ( $symfonyResponse instanceof StreamedResponse ) {
ob_start ( function ( $buffer ) use ( $stream ) {
$stream -> write ( $buffer );
2018-11-23 12:29:20 +00:00
return '' ;
2015-08-27 12:03:05 -07:00
});
$symfonyResponse -> sendContent ();
ob_end_clean ();
} else {
$stream -> write ( $symfonyResponse -> getContent ());
}
}
2018-11-23 12:29:20 +00:00
$response = $response -> withBody ( $stream );
2015-08-27 12:03:05 -07:00
2018-11-23 12:29:20 +00:00
$headers = $symfonyResponse -> headers -> all ();
2015-08-27 12:03:05 -07:00
$cookies = $symfonyResponse -> headers -> getCookies ();
if ( ! empty ( $cookies )) {
$headers [ 'Set-Cookie' ] = array ();
foreach ( $cookies as $cookie ) {
$headers [ 'Set-Cookie' ][] = $cookie -> __toString ();
}
}
2018-11-23 12:29:20 +00:00
foreach ( $headers as $name => $value ) {
$response = $response -> withHeader ( $name , $value );
}
2015-08-27 12:03:05 -07:00
$protocolVersion = $symfonyResponse -> getProtocolVersion ();
2018-11-23 12:29:20 +00:00
$response = $response -> withProtocolVersion ( $protocolVersion );
2015-08-27 12:03:05 -07:00
return $response ;
}
}