Implemented static methods for Modules.

Modules can now call default static methods by implementing the \FuzeWorks\Module trait.
The database also closes connection upon coreShutdownEvent
This commit is contained in:
Abel Hoogeveen 2016-01-17 14:42:39 +01:00
parent 727e8b3311
commit 81b86c61c9
9 changed files with 65 additions and 51 deletions

View File

@ -31,57 +31,57 @@
namespace FuzeWorks;
/**
* Class Module
* Trait Module
*
* Abstract Class for modules
* Contains all the methods modules should have
* @package net.techfuze.fuzeworks.core
* @author Abel Hoogeveen <abel@techfuze.net>
* @copyright Copyright (c) 2013 - 2015, Techfuze. (http://techfuze.net)
*/
class Module {
trait Module {
/**
* @var null|string Relative path to the module
*/
protected $modulePath = null;
protected static $modulePath = null;
/**
* @var string Internal name of the module
*/
protected $moduleName = 'placeholder';
protected static $moduleName = 'placeholder';
/**
* @var String name used in the mod array
*/
protected $linkName = 'placeholder';
protected static $linkName = 'placeholder';
/**
* @var moduleInfo object of the module
*/
protected $cfg;
protected static $cfg;
/**
* @var array Advertisements send from other modules
*/
protected $advertisements = array();
protected static $advertisements = array();
/**
* Returns the name of the module
*
* @return string Returns the name of the module
*/
public function getModuleName(){
public static function getModuleName(){
return $this->moduleName;
return self::$moduleName;
}
/**
* Returns the path to the module
* @return null|string
*/
public function getModulePath(){
public static function getModulePath(){
return $this->modulePath;
return self::$modulePath;
}
/**
@ -89,8 +89,8 @@ class Module {
* @access public
* @return stdClass module config
*/
public function getModuleConfig() {
return $this->cfg;
public static function getModuleConfig() {
return self::$cfg;
}
/**
@ -101,11 +101,10 @@ class Module {
*
* @param string $modulePath
*/
public function setModulePath($modulePath = null){
public static function setModulePath($modulePath = null){
// Only allow one change of this variable from outside
if($this->modulePath === null)
$this->modulePath = $modulePath;
if(self::$modulePath === null)
self::$modulePath = $modulePath;
}
/**
@ -113,8 +112,8 @@ class Module {
* @access public
* @param String link name
*/
public function setModuleLinkName($linkName) {
$this->linkName = $linkName;
public static function setModuleLinkName($linkName) {
self::$linkName = $linkName;
}
/**
@ -122,8 +121,8 @@ class Module {
* @access public
* @param String module name
*/
public function setModuleName($modName) {
$this->moduleName = $modName;
public static function setModuleName($modName) {
self::$moduleName = $modName;
}
/**
@ -131,8 +130,8 @@ class Module {
* @access public
* @param stdClass module config
*/
public function setModuleConfig($config) {
$this->cfg = $config;
public static function setModuleConfig($config) {
self::$cfg = $config;
}
/**
@ -141,12 +140,12 @@ class Module {
* @param Mixed config Key
* @param Mixed config value
*/
public function setConfigValue($key, $value) {
$file = $this->getModulePath() . "moduleInfo.php";
$this->cfg->$key = $value;
public static function setConfigValue($key, $value) {
$file = self::getModulePath() . "moduleInfo.php";
self::$cfg->$key = $value;
// Check if the module path is set yet
if ($this->getModulePath() == null) {
if (self::getModulePath() == null) {
Logger::logWarning("Could not write module config. ModulePath is not set", get_class($this));
return false;
}
@ -161,8 +160,8 @@ class Module {
* Set the advertisements send by other modules
* @param array $advertisements Advertisement data
*/
public function setAdvertisements($advertiseName, $advertiseData) {
$this->advertisements[$advertiseName] = $advertiseData;
public static function setAdvertisements($advertiseName, $advertiseData) {
self::$advertisements[$advertiseName] = $advertiseData;
}
/**
@ -170,8 +169,8 @@ class Module {
* @param String $advertiseName AdvertisementName
* @return array AdvertisementData
*/
public function getAdvertisements($advertiseName) {
return $this->advertisements[$advertiseName];
public static function getAdvertisements($advertiseName) {
return self::$advertisements[$advertiseName];
}
/**
@ -180,7 +179,7 @@ class Module {
* @param Mixed config Key
* @return Mixed config value
*/
public function getConfigValue($key) {
return $this->cfg->$key;
public static function getConfigValue($key) {
return self::$cfg->$key;
}
}

View File

@ -138,14 +138,15 @@ class Modules {
$CLASS = new $class_name();
// Apply default methods
if (method_exists($CLASS, 'setModulePath'))
$CLASS->setModulePath($cfg->directory);
if (method_exists($CLASS, 'setModulePath')) {
$CLASS::setModulePath($cfg->directory);
}
if (method_exists($CLASS, 'setModuleLinkName'))
$CLASS->setModuleLinkName(strtolower($cfg->module_name));
$CLASS::setModuleLinkName(strtolower($cfg->module_name));
if (method_exists($CLASS, 'setModuleName'))
$CLASS->setModuleName($name);
$CLASS::setModuleName($name);
// Send all advertisements
if (isset($cfg->listenFor)) {
@ -153,7 +154,7 @@ class Modules {
if (method_exists($CLASS, 'setAdvertisements')) {
foreach ($listenFor as $advertiseName) {
if (isset(self::$advertiseRegister[$advertiseName])) {
$CLASS->setAdvertisements($advertiseName, self::$advertiseRegister[$advertiseName]);
$CLASS::setAdvertisements($advertiseName, self::$advertiseRegister[$advertiseName]);
}
}
} else {
@ -171,7 +172,7 @@ class Modules {
}
}
$CLASS->setModuleConfig($cfg);
$CLASS::setModuleConfig($cfg);
}
// And finally check if it can be loaded

View File

@ -39,14 +39,14 @@ use \FuzeWorks\Module;
* @author Abel Hoogeveen <abel@techfuze.net>
* @copyright Copyright (c) 2013 - 2015, Techfuze. (http://techfuze.net)
*/
class Main extends Module {
class Main {
use Module;
/**
* Gets loaded upon module initialization
*
* Loads all the API types
*/
public function onLoad() {
require_once($this->getModulePath() . "/class.rest.php");
require_once(self::getModulePath() . "/class.rest.php");
}
}

View File

@ -32,6 +32,8 @@ namespace Module\Database;
use \FuzeWorks\Module;
use \FuzeWorks\Config;
use \FuzeWorks\Logger;
use \FuzeWorks\Events;
use \FuzeWorks\EventPriority;
use \PDO;
use \FuzeWorks\DatabaseException;
@ -44,7 +46,8 @@ use \FuzeWorks\DatabaseException;
* @author Abel Hoogeveen <abel@techfuze.net>
* @copyright Copyright (c) 2013 - 2015, Techfuze. (http://techfuze.net)
*/
class Main extends Module {
class Main {
use Module;
/**
* The default database connection
@ -56,6 +59,7 @@ class Main extends Module {
public function onLoad() {
Config::$dbActive = true;
Events::addListener(array($this, 'shutdown'), 'coreShutdownEvent', EventPriority::NORMAL);
}
/**
@ -100,6 +104,11 @@ class Main extends Module {
}
}
public function shutdown() {
Logger::log("Closing open database connections");
$this->DBH = null;
}
public function getPrefix() {
if (!$this->is_active()) {
$this->connect();

View File

@ -31,7 +31,7 @@
return array(
'module_class' => 'Module\Database\Main',
'module_file' => 'class.database.php',
'module_file' => 'class.main.php',
'module_name' => 'Database',
'abstract' => false,

View File

@ -42,14 +42,15 @@ use \FuzeWorks\DatabaseException;
* @author Abel Hoogeveen <abel@techfuze.net>
* @copyright Copyright (c) 2013 - 2015, Techfuze. (http://techfuze.net)
*/
class Main extends Module implements ModelServer {
class Main implements ModelServer {
use Module;
public $fields = array();
public $primary = 'id';
public $table = '';
public function onLoad() {
require_once($this->getModulePath() . '/class.query.php');
require_once(self::getModulePath() . '/class.query.php');
}
public function giveModel($type) {

View File

@ -47,7 +47,7 @@ use \FuzeWorks\Logger;
* @method $this or() or(string $field, string $arg2) OR $arg2 is the value of the field, or an operator in which case the value is pushed to the third argument
* @method $this and() and(string $field, string $arg2) AND $arg2 is the value of the field, or an operator in which case the value is pushed to the third argument
*/
class Query extends Module {
class Query {
/**
* @var array An array containing all the counted functions

View File

@ -43,7 +43,9 @@ use \FuzeWorks\Logger;
* @author Abel Hoogeveen <abel@techfuze.net>
* @copyright Copyright (c) 2013 - 2015, Techfuze. (http://techfuze.net)
*/
class Main extends Module {
class Main {
use Module;
/**
* Loads the module and registers the events

View File

@ -41,7 +41,9 @@ use \PHPMailer;
* @author Abel Hoogeveen <abel@techfuze.net>
* @copyright Copyright (c) 2013 - 2015, Techfuze. (http://techfuze.net)
*/
class Main extends Module {
class Main {
use Module;
/**
* Array of all the active PHPMailer instances
@ -70,7 +72,7 @@ use \PHPMailer;
$this->mailers[$name] = new PHPMailer();
// Set settings
$cfg = $this->cfg;
$cfg = self::$cfg;
// First check what is enabled
if ($cfg->sendmail_enabled && !$cfg->smtp_enabled) {