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 4, 2020
1 parent 6d96ed1 commit 5eeb0f4
Show file tree
Hide file tree
Showing 4 changed files with 24 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);
}
}
1 change: 1 addition & 0 deletions tests/units/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
require_once __DIR__.'/boostrap.php';

use atoum;
use RREST\Response\FileConfiguration;
use RREST\Router\Silex;
use Silex\Application;

Expand Down

0 comments on commit 5eeb0f4

Please sign in to comment.