use same way to check for hooks in hasHook() and callHook()

This commit is contained in:
Uwe Steinmann 2021-04-23 09:56:25 +02:00
parent a7e214b5a9
commit ffdc648292
2 changed files with 87 additions and 35 deletions

View File

@ -139,6 +139,46 @@ class SeedDMS_Controller_Common {
$this->errormsg = $msg;
} /* }}} */
/**
* Return a list of hook classes for the current class
*
* Hooks are associated to a controller class. Calling a hook with
* SeedDMS_View_Common::callHook() will run through a list of
* controller classes searching for the hook. This method returns this
* list.
*
* If a controller is implemented in SeedDMS_View_Example which inherits
* from SeedDMS_Theme_Style which again inherits from SeedDMS_View_Common,
* then this method will return an array with the elments:
* 'Example', 'Style', 'Common'. If SeedDMS_View_Example also sets
* the class property 'controllerAliasName', then this value will be added
* to the beginning of the list.
*
* When a hook is called, it will run through this list and checks
* if $GLOBALS['SEEDDMS_HOOKS']['controller'][<element>] exists and contains
* an instanciated class. This class must implement the hook.
*
* @return array list of controller class names.
*/
protected function getHookClassNames() { /* {{{ */
$tmps = array();
/* the controllerAliasName can be set in the controller to specify a different name
* than extracted from the class name.
*/
if(property_exists($this, 'controllerAliasName') && !empty($this->controllerAliasName)) {
$tmps[] = $this->controllerAliasName;
}
$tmp = explode('_', get_class($this));
$tmps[] = $tmp[2];
foreach(class_parents($this) as $pc) {
$tmp = explode('_', $pc);
$tmps[] = $tmp[2];
}
/* Run array_unique() in case the parent class has the same suffix */
$tmps = array_unique($tmps);
return $tmps;
} /* }}} */
/**
* Call all hooks registered for a controller
*
@ -190,17 +230,7 @@ class SeedDMS_Controller_Common {
* null if no hook was called
*/
function callHook($hook) { /* {{{ */
$tmps = array();
$tmp = explode('_', get_class($this));
$tmps[] = $tmp[2];
foreach(class_parents($this) as $pc) {
$tmp = explode('_', $pc);
$tmps[] = $tmp[2];
}
// $tmp = explode('_', get_parent_class($this));
// $tmps[] = $tmp[2];
/* Run array_unique() in case the parent class has the same suffix */
$tmps = array_unique($tmps);
$tmps = $this->getHookClassNames();
foreach($tmps as $tmp)
if(isset($GLOBALS['SEEDDMS_HOOKS']['controller'][lcfirst($tmp)])) {
$this->lasthookresult = null;
@ -242,7 +272,7 @@ class SeedDMS_Controller_Common {
* null if no hook was called
*/
function hasHook($hook) { /* {{{ */
$tmp = explode('_', get_class($this));
$tmps = $this->getHookClassNames();
if(isset($GLOBALS['SEEDDMS_HOOKS']['controller'][lcfirst($tmp[2])])) {
foreach($GLOBALS['SEEDDMS_HOOKS']['controller'][lcfirst($tmp[2])] as $hookObj) {
if (method_exists($hookObj, $hook)) {

View File

@ -95,6 +95,46 @@ class SeedDMS_View_Common {
public function show() {
}
/**
* Return a list of hook classes for the current class
*
* Hooks are associated to a view class. Calling a hook with
* SeedDMS_View_Common::callHook() will run through a list of
* view classes searching for the hook. This method returns this
* list.
*
* If a view is implemented in SeedDMS_View_Example which inherits
* from SeedDMS_Theme_Style which again inherits from SeedDMS_View_Common,
* then this method will return an array with the elments:
* 'Example', 'Style', 'Common'. If SeedDMS_View_Example also sets
* the class property 'viewAliasName', then this value will be added
* to the beginning of the list.
*
* When a hook is called, it will run through this list and checks
* if $GLOBALS['SEEDDMS_HOOKS']['view'][<element>] exists and contains
* an instanciated class. This class must implement the hook.
*
* @return array list of view class names.
*/
protected function getHookClassNames() { /* {{{ */
$tmps = array();
/* the viewAliasName can be set in the view to specify a different name
* than extracted from the class name.
*/
if(property_exists($this, 'viewAliasName') && !empty($this->viewAliasName)) {
$tmps[] = $this->viewAliasName;
}
$tmp = explode('_', get_class($this));
$tmps[] = $tmp[2];
foreach(class_parents($this) as $pc) {
$tmp = explode('_', $pc);
$tmps[] = $tmp[2];
}
/* Run array_unique() in case the parent class has the same suffix */
$tmps = array_unique($tmps);
return $tmps;
} /* }}} */
/**
* Call a hook with a given name
*
@ -113,21 +153,7 @@ class SeedDMS_View_Common {
* function returns
*/
public function callHook($hook) { /* {{{ */
$tmps = array();
/* the viewAliasName can be set in the view to specify a different name
* than extracted from the class name.
*/
if(property_exists($this, 'viewAliasName') && !empty($this->viewAliasName)) {
$tmps[] = $this->viewAliasName;
}
$tmp = explode('_', get_class($this));
$tmps[] = $tmp[2];
foreach(class_parents($this) as $pc) {
$tmp = explode('_', $pc);
$tmps[] = $tmp[2];
}
/* Run array_unique() in case the parent class has the same suffix */
$tmps = array_unique($tmps);
$tmps = $this->getHookClassNames();
$ret = null;
foreach($tmps as $tmp)
if(isset($GLOBALS['SEEDDMS_HOOKS']['view'][lcfirst($tmp)])) {
@ -180,6 +206,9 @@ class SeedDMS_View_Common {
* ?>
* </code>
*
* The method does not return hooks for parent classes nor does it
* evaluate the viewAliasName property.
*
* @params string $classname name of class (current class if left empty)
* @return array list of hook objects registered for the class
*/
@ -203,14 +232,7 @@ class SeedDMS_View_Common {
* null if no hook was called
*/
public function hasHook($hook) { /* {{{ */
$tmps = array();
$tmp = explode('_', get_class($this));
$tmps[] = $tmp[2];
$tmp = explode('_', get_parent_class($this));
$tmps[] = $tmp[2];
/* Run array_unique() in case the parent class has the same suffix */
$tmps = array_unique($tmps);
$ret = null;
$tmps = $this->getHookClassNames();
foreach($tmps as $tmp) {
if(isset($GLOBALS['SEEDDMS_HOOKS']['view'][lcfirst($tmp)])) {
foreach($GLOBALS['SEEDDMS_HOOKS']['view'][lcfirst($tmp)] as $hookObj) {