Skip to content

Commit

Permalink
Merge pull request #1239 from ytetsuro/change-testable-download
Browse files Browse the repository at this point in the history
【Unsolicited PR】I changed the download method to testable.
  • Loading branch information
lonnieezell committed Oct 14, 2018
2 parents a5b9917 + e801b70 commit 53a0579
Show file tree
Hide file tree
Showing 9 changed files with 798 additions and 76 deletions.
8 changes: 7 additions & 1 deletion system/CodeIgniter.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
* @since Version 3.0.0
* @filesource
*/
use CodeIgniter\HTTP\DownloadResponse;
use CodeIgniter\HTTP\RedirectResponse;
use CodeIgniter\HTTP\Request;
use CodeIgniter\HTTP\ResponseInterface;
Expand Down Expand Up @@ -93,7 +94,7 @@ class CodeIgniter

/**
* Current response.
* @var HTTP\Response
* @var HTTP\ResponseInterface
*/
protected $response;

Expand Down Expand Up @@ -326,6 +327,7 @@ protected function handleRequest(RouteCollectionInterface $routes = null, $cache
// so it can be used with the output.
$this->gatherOutput($cacheConfig, $returned);

$filters->setResponse($this->response);
// Run "after" filters
$response = $filters->run($uri, 'after');

Expand Down Expand Up @@ -890,6 +892,10 @@ protected function gatherOutput($cacheConfig = null, $returned = null)
ob_end_clean();
}

if ($returned instanceof DownloadResponse) {
$this->response = $returned;
return;
}
// If the controller returned a response object,
// we need to grab the body from it so it can
// be added to anything else that might have been
Expand Down
35 changes: 35 additions & 0 deletions system/Exceptions/DownloadException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php namespace CodeIgniter\Exceptions;

/**
* Class DownloadException
*
* @package CodeIgniter\Exceptions
*/
class DownloadException extends \RuntimeException implements ExceptionInterface
{

public static function forCannotSetFilePath(string $path)
{
return new static(lang('HTTP.cannotSetFilePath', [$path]));
}

public static function forCannotSetBinary()
{
return new static(lang('HTTP.cannotSetBinary'));
}

public static function forNotFoundDownloadSource()
{
return new static(lang('HTTP.notFoundDownloadSource'));
}

public static function forCannotSetCache()
{
return new static(lang('HTTP.cannotSetCache'));
}

public static function forCannotSetStatusCode(int $code, string $reason)
{
return new static(lang('HTTP.cannotSetStatusCode', [$code, $reason]));
}
}
5 changes: 5 additions & 0 deletions system/Filters/Filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ public function __construct($config, RequestInterface $request, ResponseInterfac
{
$this->config = $config;
$this->request = & $request;
$this->setResponse($response);
}

public function setResponse(ResponseInterface $response)
{
$this->response = & $response;
}

Expand Down

0 comments on commit 53a0579

Please sign in to comment.