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

Commit

Permalink
feat(Response): add getRouterBinaryFileResponse() to serve files
Browse files Browse the repository at this point in the history
  • Loading branch information
clement trumpff committed May 5, 2020
1 parent 6d96ed1 commit be7bccc
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 3 deletions.
15 changes: 14 additions & 1 deletion src/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ class Response
*/
protected $content;

/**
* @var string
*/
protected $file;

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

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

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

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

Expand Down
3 changes: 2 additions & 1 deletion src/Router/RouterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ public function setPayloadBodyValue($payloadBodyJSON);
* @param string $content
* @param int $statusCode
* @param string[] $headers
* @param string|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');
}
}
9 changes: 9 additions & 0 deletions tests/units/Router/Silex.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ public function testGetResponse()
->isInstanceOf('Symfony\Component\HttpFoundation\Response');
}

public function testGetResponseWithFile()
{
$this->newTestedInstance($this->app);
$this
->given($this->testedInstance)
->object($this->testedInstance->getResponse('XXX', 200, [], __DIR__.'/../../fixture/song.xml'))
->isInstanceOf('Symfony\Component\HttpFoundation\BinaryFileResponse');
}

public function testGetPayloadBodyValue()
{
$this->newTestedInstance($this->app);
Expand Down

0 comments on commit be7bccc

Please sign in to comment.