very simple controller pattern

basically used for adding hooks in op/op.* files
This commit is contained in:
Uwe Steinmann 2013-07-21 11:47:49 +02:00
parent 90b205232a
commit 5ee03ef693
2 changed files with 107 additions and 0 deletions

View File

@ -0,0 +1,56 @@
<?php
// SeedDMS. Document Management System
// Copyright (C) 2013 Uwe Steinmann
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
require_once('inc.ClassControllerCommon.php');
class Controller {
/**
* Create a controller from a class
*
* This method will check for a class file in the controller directory
* and returns an instance of it.
*
* @param string $class name of controller class
* @param array $params parameter passed to constructor of controller class
* @return object an object of a class implementing the view
*/
static function factory($class, $params=array()) { /* {{{ */
global $settings, $session, $dms, $user;
if(!$class) {
return null;
}
$classname = "SeedDMS_Controller_".$class;
$filename = "../controllers/class.".$class.".php";
if(file_exists($filename)) {
require($filename);
$controller = new $classname($params);
/* Set some configuration parameters */
$controller->setParam('dms', $dms);
$controller->setParam('user', $user);
$controller->setParam('postVars', $_POST);
$controller->setParam('getVars', $_GET);
$controller->setParam('requestVars', $_REQUEST);
$controller->setParam('session', $session);
$controller->setParam('settings', $settings);
return $controller;
}
return null;
} /* }}} */
}

View File

@ -0,0 +1,51 @@
<?php
// SeedDMS. Document Management System
// Copyright (C) 2013 Uwe Steinmann
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
class SeedDMS_Controller_Common {
function __construct($params) {
$this->params = $params;
}
function setParams($params) {
$this->params = $params;
}
function setParam($name, $value) {
$this->params[$name] = $value;
}
function unsetParam($name) {
if(isset($this->params[$name]))
unset($this->params[$name]);
}
function run() {
}
function callHook($hook) {
$tmp = explode('_', get_class($this));
if(isset($GLOBALS['SEEDDMS_HOOKS']['controller'][lcfirst($tmp[2])])) {
foreach($GLOBALS['SEEDDMS_HOOKS']['controller'][lcfirst($tmp[2])] as $hookObj) {
if (method_exists($hookObj, $hook)) {
return $hookObj->$hook($this);
}
}
}
return false;
}
}