Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hasHeader in Response #13570

Merged
merged 14 commits into from
Nov 3, 2018
Merged
19 changes: 11 additions & 8 deletions CHANGELOG-4.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,27 @@
- Added `forUpdate` in the Sqlite dialect to override the method from the base dialect. [#13539](https://github.com/phalcon/cphalcon/issues/13539)
- Added `TYPE_ENUM` in the Mysql adapter. [#11368](https://github.com/phalcon/cphalcon/issues/11368)
- Added `Phalcon\Acl\Adapter\Memory::addRole` support multiple inherited
- Refactored `Phalcon\Db\Adapter\Pdo::query` to use PDO's prepare and execute. `Phalcon\Db\Adapter::fetchAll` to use PDO's fetchAll
- Added `Phalcon\Tag::renderTitle()` that renders the title enclosed in `<title>` tags. [#13547](https://github.com/phalcon/cphalcon/issues/13547)
- Added `hasHeader()` method to `Phalcon\Http\Response` to provide the ability to check if a header exists. [PR-12189](https://github.com/phalcon/cphalcon/pull/12189)

## Changed
- By configuring `prefix` and `statsKey` the `Phalcon\Cache\Backend\Redis::queryKeys` no longer returns prefixed keys, now it returns original keys without prefix. [#13456](https://github.com/phalcon/cphalcon/pull/13456)
- By configuring `prefix` and `statsKey` the `Phalcon\Cache\Backend\Redis::queryKeys` no longer returns prefixed keys, now it returns original keys without prefix. [PR-13456](https://github.com/phalcon/cphalcon/pull/13456)
- Now Phalcon requires the [PSR PHP extension](https://github.com/jbboehr/php-psr) to be installed and enabled
- The `Phalcon\Mvc\Application`, `Phalcon\Mvc\Micro` and `Phalcon\Mvc\Router` now must have a URI to process [#12380](https://github.com/phalcon/cphalcon/pull/12380)
- Response headers and cookies are no longer prematurely sent [#12378](https://github.com/phalcon/cphalcon/pull/12378)
- The `Phalcon\Mvc\Application`, `Phalcon\Mvc\Micro` and `Phalcon\Mvc\Router` now must have a URI to process [PR-12380](https://github.com/phalcon/cphalcon/pull/12380)
- Response headers and cookies are no longer prematurely sent [PR-12378](https://github.com/phalcon/cphalcon/pull/12378)
- You can no longer assign data to models whilst saving them [#12317](https://github.com/phalcon/cphalcon/issues/12317)
- The `Phalcon\Mvc\Model\Manager::load` no longer reuses already initialized models [#12317](https://github.com/phalcon/cphalcon/issues/12317)
- Changed `Phalcon\Db\Dialect\Postgresql::describeReferences` to generate correct SQL, added "on update" and "on delete" constraints
- Changed catch `Exception` to `Throwable` [#12288](https://github.com/phalcon/cphalcon/issues/12288)
- Changed `Phalcon\Mvc\Model\Query\Builder::addFrom` to remove third parameter `$with` [#13109](https://github.com/phalcon/cphalcon/pull/13109)
- `Phalcon\Forms\Form::clear` will no longer call `Phalcon\Forms\Element::clear`, instead it will clear/set default value itself, and `Phalcon\Forms\Element::clear` will now call `Phalcon\Forms\Form::clear` if it's assigned to the form, otherwise it will just clear itself. [#13500](https://github.com/phalcon/cphalcon/pull/13500)
- `Phalcon\Forms\Form::getValue` will now also try to get the value by calling `Tag::getValue` or element's `getDefault` method before returning `null`, and `Phalcon\Forms\Element::getValue` calls `Tag::getDefault` only if it's not added to the form. [#13500](https://github.com/phalcon/cphalcon/pull/13500)
- Changed `Phalcon\Mvc\Model\Query\Builder::addFrom` to remove third parameter `$with` [PR-13109](https://github.com/phalcon/cphalcon/pull/13109)
- `Phalcon\Forms\Form::clear` will no longer call `Phalcon\Forms\Element::clear`, instead it will clear/set default value itself, and `Phalcon\Forms\Element::clear` will now call `Phalcon\Forms\Form::clear` if it's assigned to the form, otherwise it will just clear itself. [PR-13500](https://github.com/phalcon/cphalcon/pull/13500)
- `Phalcon\Forms\Form::getValue` will now also try to get the value by calling `Tag::getValue` or element's `getDefault` method before returning `null`, and `Phalcon\Forms\Element::getValue` calls `Tag::getDefault` only if it's not added to the form. [PR-13500](https://github.com/phalcon/cphalcon/pull/13500)
- Changed `Phalcon\Mvc\Model` to use the `Phalcon\Messages\Message` object for its messages [#13114](https://github.com/phalcon/cphalcon/issues/13114)
- Changed `Phalcon\Validation\*` to use the `Phalcon\Messages\Message` object for its messages [#13114](https://github.com/phalcon/cphalcon/issues/13114)
- Collections now use the Validation component [#12376](https://github.com/phalcon/cphalcon/pull/12376)
- Collections now use the Validation component [PR-12376](https://github.com/phalcon/cphalcon/pull/12376)
- Refactored `Phalcon\Db\Adapter\Pdo::query` to use PDO's prepare and execute. `Phalcon\Db\Adapter::fetchAll` to use PDO's fetchAll
- Fixed `\Phalcon\Http\Response::setFileToSend` filename last much _
- Changed `Phalcon\Tag::getTitle()`. It returns only the text. It accepts `prepend`, `append` booleans to prepend or append the relevant text to the title. [#13547](https://github.com/phalcon/cphalcon/issues/13547)

## Removed
- PHP < 7.0 no longer supported
Expand Down
4 changes: 2 additions & 2 deletions phalcon/acl/adapter/memory.zep
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ class Memory extends Adapter
*/
public function addInherit(string roleName, var roleToInherits) -> boolean
{
var roleInheritName, rolesNames, deepInheritName, roleToInherit, checkRoleToInherit,
var roleInheritName, rolesNames, roleToInherit, checkRoleToInherit,
checkRoleToInherits, usedRoleToInherits, roleToInheritList, usedRoleToInherit;

let rolesNames = this->_rolesNames;
Expand Down Expand Up @@ -584,7 +584,7 @@ class Memory extends Adapter
public function isAllowed(var roleName, var resourceName, string access, array parameters = null) -> boolean
{
var eventsManager, accessList, accessKey,
haveAccess = null, roleInherits, inheritedRole, rolesNames,
haveAccess = null, rolesNames,
funcAccess = null, resourceObject = null, roleObject = null, funcList,
reflectionFunction, reflectionParameters, parameterNumber, parametersForFunction,
numberOfRequiredParameters, userParametersSizeShouldBe, reflectionClass, parameterToCheck,
Expand Down
15 changes: 15 additions & 0 deletions phalcon/http/response.zep
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,21 @@ class Response implements ResponseInterface, InjectionAwareInterface
return this->_cookies;
}

/**
* Checks if a header exists
*
*<code>
* $response->hasHeader("Content-Type");
*</code>
*/
public function hasHeader(string name) -> boolean
{
var headers;
let headers = this->getHeaders();

return headers->has(name);
}

/**
* Overwrites a header in the response
*
Expand Down
8 changes: 8 additions & 0 deletions phalcon/http/response/headers.zep
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ class Headers implements HeadersInterface
{
protected _headers = [];

/**
* Sets a header to be sent at the end of the request
*/
public function has(string name) -> boolean
{
return isset(this->_headers[name]);
}

/**
* Sets a header to be sent at the end of the request
*/
Expand Down
5 changes: 5 additions & 0 deletions phalcon/http/response/headersinterface.zep
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ namespace Phalcon\Http\Response;
interface HeadersInterface
{

/**
* Returns true if the header is set, false otherwise
*/
public function has(string name) -> boolean;

/**
* Sets a header to be sent at the end of the request
*/
Expand Down
5 changes: 5 additions & 0 deletions phalcon/http/responseinterface.zep
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ interface ResponseInterface
*/
public function getHeaders() -> <HeadersInterface>;

/**
* Checks if a header exists
*/
public function hasHeader(string name) -> boolean;

/**
* Overwrites a header in the response
*/
Expand Down
1 change: 1 addition & 0 deletions phalcon/mvc/model/metadata/strategy/introspection.zep
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
namespace Phalcon\Mvc\Model\MetaData\Strategy;

use Phalcon\DiInterface;
use Phalcon\Db\AdapterInterface;
use Phalcon\Db\Column;
use Phalcon\Mvc\ModelInterface;
use Phalcon\Mvc\Model\Exception;
Expand Down
73 changes: 50 additions & 23 deletions phalcon/tag.zep
Original file line number Diff line number Diff line change
Expand Up @@ -1154,14 +1154,21 @@ class Tag
* The title will be automatically escaped.
*
* <code>
* echo Phalcon\Tag::getTitle();
* Tag::prependTitle('Hello');
* Tag::setTitle('World');
* Tag::appendTitle('from Phalcon');
*
* echo Tag::getTitle(); // Hello World from Phalcon
* echo Tag::getTitle(false); // World from Phalcon
* echo Tag::getTitle(true, false); // Hello World
* echo Tag::getTitle(false, false); // World
* </code>
*
* <code>
* {{ get_title() }}
* </code>
*/
public static function getTitle(boolean tags = true) -> string
public static function getTitle(boolean prepend = true, boolean append = true) -> string
{
var items, output, title, documentTitle, documentAppendTitle, documentPrependTitle, documentTitleSeparator, escaper;

Expand All @@ -1171,32 +1178,36 @@ class Tag
let documentTitle = escaper->escapeHtml(self::_documentTitle);
let documentTitleSeparator = escaper->escapeHtml(self::_documentTitleSeparator);

if typeof self::_documentAppendTitle == "null" {
let self::_documentAppendTitle = [];
}

let documentAppendTitle = self::_documentAppendTitle;

if typeof self::_documentPrependTitle == "null" {
let self::_documentPrependTitle = [];
}
if prepend {
if typeof self::_documentPrependTitle == "null" {
let self::_documentPrependTitle = [];
}

let documentPrependTitle = self::_documentPrependTitle;
let documentPrependTitle = self::_documentPrependTitle;

if !empty documentPrependTitle {
var tmp = array_reverse(documentPrependTitle);
for title in tmp {
let items[] = escaper->escapeHtml(title);
if !empty documentPrependTitle {
var tmp = array_reverse(documentPrependTitle);
for title in tmp {
let items[] = escaper->escapeHtml(title);
}
}
}

if !empty documentTitle {
let items[] = documentTitle;
}

if !empty documentAppendTitle {
for title in documentAppendTitle {
let items[] = escaper->escapeHtml(title);
if append {
if typeof self::_documentAppendTitle == "null" {
let self::_documentAppendTitle = [];
}

let documentAppendTitle = self::_documentAppendTitle;

if !empty documentAppendTitle {
for title in documentAppendTitle {
let items[] = escaper->escapeHtml(title);
}
}
}

Expand All @@ -1208,13 +1219,29 @@ class Tag
let output = implode(documentTitleSeparator, items);
}

if tags {
return "<title>" . output . "</title>" . PHP_EOL;
}

return output;
}

/**
* Renders the title with title tags. The title is automaticall escaped
*
* <code>
* Tag::prependTitle('Hello');
* Tag::setTitle('World');
* Tag::appendTitle('from Phalcon');
*
* echo Tag::renderTitle(); // <title>Hello World From Phalcon</title>
* </code>
*
* <code>
* {{ render_title() }}
* </code>
*/
public static function renderTitle() -> string
{
return "<title>" . self::getTitle() . "</title>" . PHP_EOL;
}

/**
* Gets the current document title separator
*
Expand Down
20 changes: 20 additions & 0 deletions tests/unit/Http/Response/HeadersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,26 @@ function () {
);
}

/**
* Tests the has of the response headers
*
* @author Nikolaos Dimopoulos <[email protected]>
* @since 2018-11-02
*/
public function testHttpResponseHeadersHas()
{
$this->specify(
"Has Response Headers is not correct",
function () {
$responseHeaders = new Headers();
$responseHeaders->set('Content-Type', 'text/html');

expect($responseHeaders->has('Content-Type'))->true();
expect($responseHeaders->has('unknown-header'))->false();
}
);
}

/**
* Tests the set of the response status headers
*
Expand Down
22 changes: 21 additions & 1 deletion tests/unit/Http/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,27 @@ function () {
);
}

/**
* Tests the hasHeader
*
* @author Nikolaos Dimopoulos <[email protected]>
* @since 2014-10-08
*/
public function testHttpResponseHasHeader()
{
$this->specify(
"hasHeader is not returning the correct value",
function () {
$response = $this->getResponseObject();
$response->resetHeaders();
$response->setHeader('Content-Type', 'text/html');

expect($response->hasHeader('Content-Type'))->true();
expect($response->hasHeader('some-unknown-header'))->false();
}
);
}

/**
* Tests the setStatusCode
*
Expand All @@ -93,7 +114,6 @@ function () {
$response = $this->getResponseObject();
$response->resetHeaders();
$response->setStatusCode(404, "Not Found");

$expected = Headers::__set_state(
[
'_headers' => [
Expand Down
Loading