From cd58891723a3ff57c2f1f4dcd157321d89ca5d26 Mon Sep 17 00:00:00 2001 From: Sam Georges Date: Sat, 24 May 2014 13:47:34 +1000 Subject: [PATCH] Tidy up CmsObject query implementation to reduce overhead --- modules/cms/classes/CmsObject.php | 14 +++++++++++--- modules/cms/classes/CmsObjectQuery.php | 20 ++++++++++---------- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/modules/cms/classes/CmsObject.php b/modules/cms/classes/CmsObject.php index b8dbe87ae..c575910b3 100644 --- a/modules/cms/classes/CmsObject.php +++ b/modules/cms/classes/CmsObject.php @@ -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}()"); } /** diff --git a/modules/cms/classes/CmsObjectQuery.php b/modules/cms/classes/CmsObjectQuery.php index 83e2c2824..40f49a4a9 100644 --- a/modules/cms/classes/CmsObjectQuery.php +++ b/modules/cms/classes/CmsObjectQuery.php @@ -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}()"); } } \ No newline at end of file