From 8fb76220618a181e7ee8face377761b08e19776f Mon Sep 17 00:00:00 2001 From: Josh Sherman Date: Sun, 14 Oct 2012 18:08:27 -0400 Subject: [PATCH] If the module doesn't return anything, pass the return array instead Now you can set variables in the module itself and have them returned to the template instead of needing to explicitly return the data. Will come in handy in scenarios where you've extended another module and want to retain it's return data. Previously you had to set a variable and then add to that and return it from the child class. --- classes/Controller.php | 12 +++++++++++- classes/Module.php | 19 +++++++++++++++++-- jar.php | 31 ++++++++++++++++++++++++++++--- 3 files changed, 56 insertions(+), 6 deletions(-) diff --git a/classes/Controller.php b/classes/Controller.php index ee81913..b906ebb 100644 --- a/classes/Controller.php +++ b/classes/Controller.php @@ -368,7 +368,17 @@ class Controller extends Object * module know to use the cache, either passing in a variable * or setting it on the object */ - $display->setModuleReturn($valid_request && $valid_security_hash ? $module->__default() : array('status' => 'error', 'message' => $error_message)); + if ($valid_request && $valid_security_hash) + { + $module_return = $module->__default(); + + if ($module_return === null) + { + $module_return = $module->return; + } + } + + $display->setModuleReturn(isset($module_return) ? $module_return : array('status' => 'error', 'message' => $error_message)); unset($error_message); diff --git a/classes/Module.php b/classes/Module.php index cd65d52..3fdf255 100644 --- a/classes/Module.php +++ b/classes/Module.php @@ -158,6 +158,19 @@ class Module extends Object */ protected $template = 'index'; + /** + * Return + * + * Array that is returned to the template in the case of the module not + * returning anything itself. This is somewhat of a one way trip as you + * cannot get the variable unless you reference the return array explicitly + * $this->return['variable'] + * + * @access protected + * @var array + */ + protected $return = array(); + /** * Constructor * @@ -198,14 +211,16 @@ class Module extends Object /** * Magic Setter Method * - * Prohibits the direct modification of module variables. + * Places the variables that are being modified in the return array that is + * returned if nothing is returned by the module itself. This also prohibits + * the direct modification of module variables which could cause issues. * * @param string $name name of the variable to be set * @param mixed $value value of the variable to be set */ public function __set($name, $value) { - throw new Exception('Cannot set module variables directly'); + $this->return[$name] = $value; } /** diff --git a/jar.php b/jar.php index e35993c..a9a1d98 100755 --- a/jar.php +++ b/jar.php @@ -1315,7 +1315,17 @@ class Controller extends Object * module know to use the cache, either passing in a variable * or setting it on the object */ - $display->setModuleReturn($valid_request && $valid_security_hash ? $module->__default() : array('status' => 'error', 'message' => $error_message)); + if ($valid_request && $valid_security_hash) + { + $module_return = $module->__default(); + + if ($module_return === null) + { + $module_return = $module->return; + } + } + + $display->setModuleReturn(isset($module_return) ? $module_return : array('status' => 'error', 'message' => $error_message)); unset($error_message); @@ -5552,6 +5562,19 @@ class Module extends Object */ protected $template = 'index'; + /** + * Return + * + * Array that is returned to the template in the case of the module not + * returning anything itself. This is somewhat of a one way trip as you + * cannot get the variable unless you reference the return array explicitly + * $this->return['variable'] + * + * @access protected + * @var array + */ + protected $return = array(); + /** * Constructor * @@ -5592,14 +5615,16 @@ class Module extends Object /** * Magic Setter Method * - * Prohibits the direct modification of module variables. + * Places the variables that are being modified in the return array that is + * returned if nothing is returned by the module itself. This also prohibits + * the direct modification of module variables which could cause issues. * * @param string $name name of the variable to be set * @param mixed $value value of the variable to be set */ public function __set($name, $value) { - throw new Exception('Cannot set module variables directly'); + $this->return[$name] = $value; } /**