MAJOR ENHANCEMENT VirtualPages now pass all unknown method calls, and property gets on to the original item if the virtual page doesn't have it/them.

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@83160 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Tom Rix 2009-07-29 23:21:59 +00:00
parent 7115d327d4
commit 1eb80b7498
2 changed files with 68 additions and 2 deletions

View File

@ -554,7 +554,8 @@ abstract class Object {
); );
} }
} else { } else {
throw new Exception("Object->__call(): the method '$method' does not exist on '$this->class'"); // Please do not change the exception code number below.
throw new Exception("Object->__call(): the method '$method' does not exist on '$this->class'", 2175);
} }
} }

View File

@ -138,8 +138,39 @@ class VirtualPage extends Page {
$return = $this->copyContentFrom()->getField($field); $return = $this->copyContentFrom()->getField($field);
} }
} }
return $return; return $return;
} }
/**
* Pass unrecognized method calls on to the original data object
*
* @param string $method
* @param string $args
*/
function __call($method, $args) {
try {
return parent::__call($method, $args);
} catch (Exception $e) {
// Hack... detect exception type. We really should use exception subclasses.
// if the exception isn't a 'no method' error, rethrow it
if ($e->getCode() !== 2175) throw $e;
$original = $this->copyContentFrom();
return call_user_func_array(array($original, $method), $args);
}
}
/**
* Overwrite to also check for method on the original data object
*
* @param string $method
* @return bool
*/
function hasMethod($method) {
$haveIt = parent::hasMethod($method);
if (!$haveIt) $haveIt = $this->copyContentFrom()->hasMethod($method);
return $haveIt;
}
} }
/** /**
@ -147,7 +178,7 @@ class VirtualPage extends Page {
* @package cms * @package cms
*/ */
class VirtualPage_Controller extends Page_Controller { class VirtualPage_Controller extends Page_Controller {
static $allowed_actions = array( static $allowed_actions = array(
'loadcontentall' => 'ADMIN', 'loadcontentall' => 'ADMIN',
); );
@ -192,6 +223,40 @@ class VirtualPage_Controller extends Page_Controller {
echo "<li>Published $page->URLSegment"; echo "<li>Published $page->URLSegment";
} }
} }
/**
* Also check the original objects' original controller for the method
*
* @param string $method
* @return bool
*/
function hasMethod($method) {
$haveIt = parent::hasMethod($method);
if (!$haveIt) {
$name = get_class($this->CopyContentFrom())."_Controller";
$controller = new $name($this->dataRecord->copyContentFrom());
$haveIt = $controller->hasMethod($method);
}
return $haveIt;
}
/**
* Pass unrecognized method calls on to the original controller
*
* @param string $method
* @param string $args
*/
function __call($method, $args) {
try {
return parent::__call($method, $args);
} catch (Exception $e) {
// Hack... detect exception type. We really should use exception subclasses.
// if the exception isn't a 'no method' error, rethrow it
if ($e->getCode() !== 2175) throw $e;
$original = $this->copyContentFrom();
return call_user_func_array(array($original, $method), $args);
}
}
} }
?> ?>