Code
Explorer
You are a developer and looking for Shopware projects?
Apply Now!
createStreamFromResource example
$symfonyRequest
->server->
all
(
)
)
;
foreach
(
$symfonyRequest
->headers->
all
(
)
as
$name
=>
$value
)
{
try
{
$request
=
$request
->
withHeader
(
$name
,
$value
)
;
}
catch
(
\InvalidArgumentException
$e
)
{
// ignore invalid header
}
}
$body
=
$this
->streamFactory->
createStreamFromResource
(
$symfonyRequest
->
getContent
(
true
)
)
;
if
(
method_exists
(
Request::
class
, 'getContentTypeFormat'
)
)
{
$format
=
$symfonyRequest
->
getContentTypeFormat
(
)
;
}
else
{
$format
=
$symfonyRequest
->
getContentType
(
)
;
}
if
(
'json' ===
$format
)
{
$parsedBody
=
json_decode
(
$symfonyRequest
->
getContent
(
)
, true, 512, \JSON_BIGINT_AS_STRING
)
;
if
(
!\
is_array
(
$parsedBody
)
)
{
if
(
!\
is_string
(
$content
)
)
{
trigger_deprecation
(
'symfony/http-client', '6.2', 'Passing a "%s" to "%s()" is deprecated, use "createStreamFrom*()" instead.',
get_debug_type
(
$content
)
, __METHOD__
)
;
}
if
(
$content
instanceof StreamInterface
)
{
return
$content
;
}
if
(
\
is_string
(
$content
?? ''
)
)
{
$stream
=
$this
->streamFactory->
createStream
(
$content
?? ''
)
;
}
elseif
(
\
is_resource
(
$content
)
)
{
$stream
=
$this
->streamFactory->
createStreamFromResource
(
$content
)
;
}
else
{
throw
new
\
InvalidArgumentException
(
sprintf
(
'"%s()" expects string, resource or StreamInterface, "%s" given.', __METHOD__,
get_debug_type
(
$content
)
)
)
;
}
if
(
$stream
->
isSeekable
(
)
)
{
$stream
->
seek
(
0
)
;
}
return
$stream
;
}
foreach
(
$response
->
getHeaders
(
false
)
as
$name
=>
$values
)
{
foreach
(
$values
as
$value
)
{
try
{
$psrResponse
=
$psrResponse
->
withAddedHeader
(
$name
,
$value
)
;
}
catch
(
\InvalidArgumentException
$e
)
{
// ignore invalid header
}
}
}
$body
=
$response
instanceof StreamableInterface ?
$response
->
toStream
(
false
)
: StreamWrapper::
createResource
(
$response
,
$this
->client
)
;
$body
=
$this
->streamFactory->
createStreamFromResource
(
$body
)
;
if
(
$body
->
isSeekable
(
)
)
{
$body
->
seek
(
0
)
;
}
return
$psrResponse
->
withBody
(
$body
)
;
}
catch
(
TransportExceptionInterface
$e
)
{
if
(
$e
instanceof \InvalidArgumentException
)
{
throw
new
Psr18RequestException
(
$e
,
$request
)
;
}
$media
=
$this
->
findMediaById
(
$mediaId
,
$context
)
;
return
$this
->
getFileSystem
(
$media
)
->
read
(
$this
->
getFilePath
(
$media
)
)
?: '';
}
public
function
loadMediaFileStream
(
string
$mediaId
, Context
$context
)
: StreamInterface
{
$media
=
$this
->
findMediaById
(
$mediaId
,
$context
)
;
$resource
=
$this
->
getFileSystem
(
$media
)
->
readStream
(
$this
->
getFilePath
(
$media
)
)
;
return
$this
->streamFactory->
createStreamFromResource
(
$resource
)
;
}
private
function
getFilePath
(
MediaEntity
$media
)
: string
{
$this
->fileNameValidator->
validateFileName
(
$media
->
getFileName
(
)
?: ''
)
;
return
$this
->urlGenerator->
getRelativeMediaUrl
(
$media
)
;
}
private
function
getFileSystem
(
MediaEntity
$media
)
: FilesystemOperator
{
$symfonyRequest
->server->
all
(
)
)
;
foreach
(
$symfonyRequest
->headers->
all
(
)
as
$name
=>
$value
)
{
try
{
$request
=
$request
->
withHeader
(
$name
,
$value
)
;
}
catch
(
\InvalidArgumentException
$e
)
{
// ignore invalid header
}
}
$body
=
$this
->streamFactory->
createStreamFromResource
(
$symfonyRequest
->
getContent
(
true
)
)
;
$request
=
$request
->
withBody
(
$body
)
->
withUploadedFiles
(
$this
->
getFiles
(
$symfonyRequest
->files->
all
(
)
)
)
->
withCookieParams
(
$symfonyRequest
->cookies->
all
(
)
)
->
withQueryParams
(
$symfonyRequest
->query->
all
(
)
)
->
withParsedBody
(
$symfonyRequest
->request->
all
(
)
)
;
foreach
(
$symfonyRequest
->attributes->
all
(
)
as
$key
=>
$value
)
{
$request
=
$request
->
withAttribute
(
$key
,
$value
)
;
}
foreach
(
$response
->
getHeaders
(
false
)
as
$name
=>
$values
)
{
foreach
(
$values
as
$value
)
{
try
{
$psrResponse
=
$psrResponse
->
withAddedHeader
(
$name
,
$value
)
;
}
catch
(
\InvalidArgumentException
$e
)
{
// ignore invalid header
}
}
}
if
(
$response
instanceof StreamableInterface
)
{
$body
=
$this
->streamFactory->
createStreamFromResource
(
$response
->
toStream
(
false
)
)
;
}
elseif
(
!
$buffer
)
{
$body
=
$this
->streamFactory->
createStreamFromResource
(
StreamWrapper::
createResource
(
$response
,
$this
->client
)
)
;
}
else
{
$body
=
$this
->streamFactory->
createStream
(
$response
->
getContent
(
false
)
)
;
}
if
(
$body
->
isSeekable
(
)
)
{
$body
->
seek
(
0
)
;
}
return
$psrResponse
->
withBody
(
$body
)
;
}