Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/develop' into migration-union
Browse files Browse the repository at this point in the history
  • Loading branch information
MGatner committed Aug 14, 2019
2 parents 936301e + b8a6f03 commit 4223467
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 15 deletions.
14 changes: 7 additions & 7 deletions app/Controllers/BaseController.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
<?php namespace App\Controllers;
<?php

namespace App\Controllers;

use CodeIgniter\Controller;

/**
* Class BaseController
Expand All @@ -7,14 +11,11 @@
* and performing functions that are needed by all your controllers.
* Extend this class in any new controllers:
* class Home extends BaseController
*
*
* For security be sure to declare any new methods as protected or private.
*
*
* @package CodeIgniter
*/

use CodeIgniter\Controller;

class BaseController extends Controller
{

Expand All @@ -35,7 +36,6 @@ public function initController(\CodeIgniter\HTTP\RequestInterface $request, \Cod
{
// Do Not Edit This Line
parent::initController($request, $response, $logger);

//--------------------------------------------------------------------
// Preload any models, libraries, etc, here.
//--------------------------------------------------------------------
Expand Down
16 changes: 16 additions & 0 deletions system/API/ResponseTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ trait ResponseTrait
protected $codes = [
'created' => 201,
'deleted' => 200,
'no_content' => 204,
'invalid_request' => 400,
'unsupported_response_type' => 400,
'invalid_scope' => 400,
Expand Down Expand Up @@ -190,6 +191,21 @@ public function respondDeleted($data = null, string $message = '')

//--------------------------------------------------------------------

/**
* Used after a command has been successfully executed but there is no
* meaningful reply to send back to the client.
*
* @param string $message Message.
*
* @return mixed
*/
public function respondNoContent(string $message = 'No Content')
{
return $this->respond(null, $this->codes['no_content'], $message);
}

//--------------------------------------------------------------------

/**
* Used when the client is either didn't send authorization information,
* or had bad authorization credentials. User is encouraged to try again
Expand Down
2 changes: 1 addition & 1 deletion system/Cache/Handlers/RedisHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ public function delete(string $key)
{
$key = $this->prefix . $key;

return ($this->redis->delete($key) === 1);
return ($this->redis->del($key) === 1);
}

//--------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion system/Database/BaseBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -1293,7 +1293,7 @@ public function orderBy(string $orderBy, string $direction = '', bool $escape =
*
* @return BaseBuilder
*/
public function limit(int $value = null, int $offset = 0)
public function limit(int $value = null, ?int $offset = 0)
{
if (! is_null($value))
{
Expand Down
13 changes: 13 additions & 0 deletions system/HTTP/DownloadResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,19 @@ public function setFilePath(string $filepath)
$this->file = new File($filepath, true);
}

/**
* set name for the download.
*
* @param string $filename
*
* @return $this
*/
public function setFileName(string $filename)
{
$this->filename = $filename;
return $this;
}

/**
* get content length.
*
Expand Down
4 changes: 2 additions & 2 deletions system/HTTP/Files/UploadedFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -356,9 +356,9 @@ public function getExtension(): string
*
* @return string|null
*/
public function guessExtension(): ?string
public function guessExtension(): string
{
return Mimes::guessExtensionFromType($this->getClientMimeType(), $this->getClientExtension());
return Mimes::guessExtensionFromType($this->getClientMimeType(), $this->getClientExtension()) ?? $this->getClientExtension();
}

//--------------------------------------------------------------------
Expand Down
9 changes: 9 additions & 0 deletions tests/system/API/ResponseTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,15 @@ public function testForbidden()
$this->assertEquals($this->formatter->format($expected), $this->response->getBody());
}

public function testNoContent()
{
$controller = $this->makeController();
$controller->respondNoContent('');

$this->assertEquals('No Content', $this->response->getReason());
$this->assertEquals(204, $this->response->getStatusCode());
}

public function testNotFound()
{
$controller = $this->makeController();
Expand Down
9 changes: 9 additions & 0 deletions tests/system/HTTP/DownloadResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,15 @@ public function testSetContentTypeNoCharSet()

$this->assertEquals('application/octet-stream', $response->getHeaderLine('Content-Type'));
}

public function testSetFileName()
{
$response = new DownloadResponse('unit-test.txt', true);
$response->setFileName('myFile.txt');
$response->buildHeaders();

$this->assertSame('attachment; filename="myFile.txt"; filename*=UTF-8\'\'myFile.txt', $response->getHeaderLine('Content-Disposition'));
}

public function testNoCache()
{
Expand Down
5 changes: 1 addition & 4 deletions user_guide_src/source/dbmgmt/migration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ configuration file::
];

This will look for any migrations located at both **APPPATH/Database/Migrations** and
**ROOTPATH/Database/Migrations**. This makes it simple to include migrations in your
**ROOTPATH/MyCompany/Database/Migrations**. This makes it simple to include migrations in your
re-usable, modular code suites.

*************
Expand Down Expand Up @@ -208,9 +208,6 @@ for the version. ::
> php spark migrate:version
Version:

// Sequential
> php spark migrate:version 007

// Timestamp
> php spark migrate:version 20161426211300

Expand Down
15 changes: 15 additions & 0 deletions user_guide_src/source/outgoing/api_responses.rst
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ exist for the most common use cases::
respondCreated($data);
// Item successfully deleted
respondDeleted($data);
// Command executed by no response required
respondNoContent($message);
// Client isn't authorized
failUnauthorized($description);
// Forbidden action
Expand Down Expand Up @@ -179,6 +181,19 @@ Class Reference
$user = $userModel->delete($id);
return $this->respondDeleted(['id' => $id]);

.. php:method:: respondNoContent(string $message = 'No Content')
:param string $message: A custom "reason" message to return.
:returns: The value of the Response object's send() method.

Sets the appropriate status code to use when a command was successfully executed by the server but there is no
meaningful reply to send back to the client, typically 204.

::

sleep(1);
return $this->respondNoContent();

.. php:method:: failUnauthorized(string $description = 'Unauthorized'[, string $code=null[, string $message = '']])
:param mixed $description: The error message to show the user.
Expand Down
4 changes: 4 additions & 0 deletions user_guide_src/source/outgoing/response.rst
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ do the following::
// Contents of photo.jpg will be automatically read
return $response->download('/path/to/photo.jpg', NULL);

Use the optional ``setFileName()`` method to change the filename as it is sent to the client's browser::
return $response->download('awkwardEncryptedFileName.fakeExt')->setFileName('expenses.csv');

.. note:: The response object MUST be returned for the download to be sent to the client. This allows the response
to be passed through all **after** filters before being sent to the client.

Expand Down

0 comments on commit 4223467

Please sign in to comment.