Skip to content
This repository has been archived by the owner on Dec 20, 2021. It is now read-only.

Commit

Permalink
Merge 6ce792f into 6d96ed1
Browse files Browse the repository at this point in the history
  • Loading branch information
1992clement committed May 4, 2020
2 parents 6d96ed1 + 6ce792f commit 3e51cc4
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 3 deletions.
16 changes: 15 additions & 1 deletion src/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use League\JsonGuard;
use RREST\Validator\JsonValidator;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\Encoder\XmlEncoder;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
Expand All @@ -18,6 +19,11 @@ class Response
*/
protected $content;

/**
* @var string|File|\SplFileInfo
*/
protected $file;

/**
* @var string
*/
Expand Down Expand Up @@ -62,6 +68,14 @@ public function __construct(RouterInterface $router, $format, $statusCode)
$this->setStatusCode($statusCode);
}

/**
* @param string|File|\SplFileInfo $file
*/
public function setFile($file)
{
$this->file = $file;
}

/**
* @return string
*/
Expand Down Expand Up @@ -226,7 +240,7 @@ public function getRouterResponse($autoSerializeContent = true)
}

return $this->router->getResponse(
$content, $this->getConfiguredHeaderstatusCode(), $this->getConfiguredHeaders()
$content, $this->getConfiguredHeaderstatusCode(), $this->getConfiguredHeaders(), $this->file
);
}

Expand Down
4 changes: 3 additions & 1 deletion src/Router/RouterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace RREST\Router;

use RREST\Response;
use Symfony\Component\HttpFoundation\File\File;

interface RouterInterface
{
Expand Down Expand Up @@ -55,8 +56,9 @@ public function setPayloadBodyValue($payloadBodyJSON);
* @param string $content
* @param int $statusCode
* @param string[] $headers
* @param string|File|\SplFileInfo|null $file
*
* @return mixed
*/
public function getResponse($content = '', $statusCode = 200, $headers = array());
public function getResponse($content = '', $statusCode = 200, $headers = array(), $file = null);
}
6 changes: 5 additions & 1 deletion src/Router/Silex.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace RREST\Router;

use Symfony\Component\HttpFoundation\BinaryFileResponse as HttpFoundationBinaryFileResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response as HttpFoundationResponse;
use RREST\Response;
Expand Down Expand Up @@ -111,8 +112,11 @@ public function setPayloadBodyValue($payloadBodyJSON)
/**
* {@inheritdoc}
*/
public function getResponse($content = '', $statusCode = 200, $headers = array())
public function getResponse($content = '', $statusCode = 200, $headers = array(), $file = null)
{
if (!empty($file)) {
return new HttpFoundationBinaryFileResponse($file, $statusCode, $headers);
}
return new HttpFoundationResponse($content, $statusCode, $headers);
}
}
13 changes: 13 additions & 0 deletions tests/units/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,4 +215,17 @@ public function testGetRouterResponse()
->isEqualTo('https://api.domain.com/items/uuid')
;
}

public function testGetRouterResponseWithFile()
{
$this->newTestedInstance($this->router, 'json', 201);

$this
->given($this->testedInstance)
->and(
$this->testedInstance->setFile(__DIR__.'/../fixture/song.xml')
)
->object($this->testedInstance->getRouterResponse())
->isInstanceOf('Symfony\Component\HttpFoundation\BinaryFileResponse');
}
}

0 comments on commit 3e51cc4

Please sign in to comment.