Tidy up CmsObject query implementation to reduce overhead

This commit is contained in:
Sam Georges 2014-05-24 13:47:34 +10:00
parent c97e352ea2
commit cd58891723
2 changed files with 21 additions and 13 deletions

View File

@ -62,6 +62,7 @@ class CmsObject implements ArrayAccess
/**
* Creates an instance of the object and associates it with a CMS theme.
* @param \Cms\Classes\Theme $theme Specifies the theme the object belongs to.
* If the theme is specified as NULL, then a query can be performed on the object.
*/
public function __construct(Theme $theme = null)
{
@ -491,7 +492,7 @@ class CmsObject implements ArrayAccess
*/
public function newQuery()
{
$query = new CmsObjectQuery($this, $this->theme);
$query = new CmsObjectQuery($this);
return $query;
}
@ -503,8 +504,15 @@ class CmsObject implements ArrayAccess
*/
public function __call($method, $parameters)
{
$query = $this->newQuery();
return call_user_func_array(array($query, $method), $parameters);
// If this object is populated with a theme, then a query
// cannot be performed on it to reduce overhead.
if (!$this->theme) {
$query = $this->newQuery();
return call_user_func_array(array($query, $method), $parameters);
}
$className = get_class($this);
throw new \BadMethodCallException("Call to undefined method {$className}::{$method}()");
}
/**

View File

@ -17,14 +17,9 @@ class CmsObjectQuery
protected $theme;
public function __construct($cmsObject, $theme)
public function __construct($cmsObject)
{
$this->cmsObject = $cmsObject;
if ($theme)
$this->theme = $theme;
else
$this->inEditTheme();
}
/**
@ -72,6 +67,9 @@ class CmsObjectQuery
*/
public function all()
{
if (!$this->theme)
$this->inEditTheme();
$collection = forward_static_call([$this->cmsObject, 'listInTheme'], $this->theme, !$this->useCache);
$collection = new CmsObjectCollection($collection);
return $collection;
@ -85,11 +83,13 @@ class CmsObjectQuery
*/
public function __call($method, $parameters)
{
if (!method_exists('Cms\Classes\CmsObjectCollection', $method))
return;
if (method_exists('Cms\Classes\CmsObjectCollection', $method)) {
$collection = $this->all();
return call_user_func_array(array($collection, $method), $parameters);
}
$collection = $this->all();
return call_user_func_array(array($collection, $method), $parameters);
$className = get_class($this);
throw new \BadMethodCallException("Call to undefined method {$className}::{$method}()");
}
}