API Make SSViewer#process return HTMLText not string

This means that you dont have to worry about casting it
as HTMLText again when using the result in a template or other context

However in some situations code might be assuming it can
check with is_string, in which case you now need to use instanceof HTMLText
This commit is contained in:
Hamish Friedlander 2013-03-12 12:17:20 +13:00 committed by Sam Minnee
parent 9bd6dd9ade
commit 743a186c32
7 changed files with 24 additions and 14 deletions

View File

@ -157,9 +157,9 @@ class Controller extends RequestHandler implements TemplateGlobalProvider {
. "returning it without modification.");
}
$this->response = $body;
} else {
if(is_object($body)) {
if($body instanceof Object && $body->hasMethod('getViewer')) {
if(isset($_REQUEST['debug_request'])) {
Debug::message("Request handler $body->class object to $this->class controller;"
. "rendering with template returned by $body->class::getViewer()");

View File

@ -159,7 +159,7 @@ class SS_HTTPResponse {
* @return SS_HTTPRequest $this
*/
public function setBody($body) {
$this->body = $body;
$this->body = $body ? (string)$body : $body; // Don't type-cast false-ish values, eg null is null not ''
}
/**

View File

@ -79,7 +79,8 @@ class PjaxResponseNegotiator {
// Execute the fragment callbacks and build the response.
foreach($fragments as $fragment) {
if(isset($callbacks[$fragment])) {
$responseParts[$fragment] = call_user_func($callbacks[$fragment]);
$res = call_user_func($callbacks[$fragment]);
$responseParts[$fragment] = $res ? (string)$res : $res;
} else {
throw new SS_HTTPResponse_Exception("X-Pjax = '$fragment' not supported for this URL.", 400);
}

View File

@ -510,6 +510,17 @@ class SapphireTest extends PHPUnit_Framework_TestCase {
$controller->response->removeHeader('Location');
}
}
public static function assertContains($needle, $haystack, $message = '', $ignoreCase = FALSE, $checkForObjectIdentity = TRUE) {
if ($haystack instanceof DBField) $haystack = (string)$haystack;
parent::assertContains($needle, $haystack, $message, $ignoreCase, $checkForObjectIdentity);
}
public static function assertNotContains($needle, $haystack, $message = '', $ignoreCase = FALSE, $checkForObjectIdentity = TRUE) {
if ($haystack instanceof DBField) $haystack = (string)$haystack;
parent::assertNotContains($needle, $haystack, $message, $ignoreCase, $checkForObjectIdentity);
}
/**
* Clear the log of emails sent
*/

View File

@ -295,5 +295,8 @@ abstract class DBField extends ViewableData {
</ul>
DBG;
}
public function __toString() {
return $this->forTemplate();
}
}

View File

@ -866,7 +866,7 @@ class SSViewer {
* @param ViewableData $item
* @param SS_Cache $cache Optional cache backend.
*
* @return String Parsed template output.
* @return HTMLText Parsed template output.
*/
public function process($item, $arguments = null) {
SSViewer::$topLevel[] = $item;
@ -909,12 +909,7 @@ class SSViewer {
$subtemplateViewer->includeRequirements(false);
$subtemplateViewer->setPartialCacheStore($this->getPartialCacheStore());
$underlay[$subtemplate] = DBField::create_field(
'HTMLText',
$subtemplateViewer->process($item, $arguments),
$subtemplate,
array('shortcodes' => false)
);
$underlay[$subtemplate] = $subtemplateViewer->process($item, $arguments);
}
}
@ -939,7 +934,7 @@ class SSViewer {
}
}
return $output;
return DBField::create_field('HTMLText', $output, null, array('shortcodes' => false));
}
/**

View File

@ -318,7 +318,7 @@ class ViewableData extends Object implements IteratorAggregate {
*
* @param string|array|SSViewer $template the template to render into
* @param array $customFields fields to customise() the object with before rendering
* @return string
* @return HTMLText
*/
public function renderWith($template, $customFields = null) {
if(!is_object($template)) {