From e3ab0da0ccdc15f222122c447020b8e223138e03 Mon Sep 17 00:00:00 2001 From: Abel Hoogeveen Date: Thu, 26 Feb 2015 14:09:08 +0100 Subject: [PATCH 1/3] Started working on Model Type loading based on Events --- .../{model => databasemodel}/class.model.php | 12 ++++++++++-- Core/Mods/databasemodel/moduleInfo.php | 16 ++++++++++++++++ Core/System/class.models.php | 7 ++++++- 3 files changed, 32 insertions(+), 3 deletions(-) rename Core/Mods/{model => databasemodel}/class.model.php (96%) create mode 100644 Core/Mods/databasemodel/moduleInfo.php diff --git a/Core/Mods/model/class.model.php b/Core/Mods/databasemodel/class.model.php similarity index 96% rename from Core/Mods/model/class.model.php rename to Core/Mods/databasemodel/class.model.php index aa727e0..40f6b38 100644 --- a/Core/Mods/model/class.model.php +++ b/Core/Mods/databasemodel/class.model.php @@ -1,6 +1,14 @@ events->addListener(array($this, 'eventRegisterBuild'), 'eventRegisterBuildEvent', EventPriority::NORMAL); + } + + public function eventRegisterBuild($event) { + $event->addEvent('databasemodel', 'loadModelsEvent'); + return $event; + } } class DatabaseModel extends Bus{ diff --git a/Core/Mods/databasemodel/moduleInfo.php b/Core/Mods/databasemodel/moduleInfo.php new file mode 100644 index 0000000..1557930 --- /dev/null +++ b/Core/Mods/databasemodel/moduleInfo.php @@ -0,0 +1,16 @@ + 'DatabaseModelManager', + 'module_file' => 'class.model.php', + 'module_name' => 'databasemodel', + + 'name' => 'DatabaseModel', + 'description' => 'Abstract type for easy database queries', + 'author' => 'TechFuze', + 'version' => '1.0.0', + 'website' => 'http://fuzeworks.techfuze.net/', + + 'date_created' => '26-02-2015', + 'date_updated' => '26-02-2015', +); diff --git a/Core/System/class.models.php b/Core/System/class.models.php index acadf22..4d4c5eb 100644 --- a/Core/System/class.models.php +++ b/Core/System/class.models.php @@ -7,13 +7,18 @@ class Models extends Bus{ private $models_array = array(); private $model_types = array(); + private $models_loaded = false; public function __construct(&$core){ parent::__construct($core); } public function loadModel($name, $directory = null){ - $this->core->loadMod('model'); + if ($this->models_loaded) { + $this->events->fireEvent('modelsLoadEvent'); + $this->models_loaded = true; + } + if($directory === null){ $directory = FUZEPATH . "/Application/Models"; } From 453afc0b31318d65fdd6f5d448a8eba75ece9c48 Mon Sep 17 00:00:00 2001 From: Abel Hoogeveen Date: Mon, 16 Mar 2015 12:38:46 +0100 Subject: [PATCH 2/3] Started working on new Abstract for models which allows the use of multiple types of models --- Core/Mods/databasemodel/moduleInfo.php | 2 ++ Core/System/class.abstract.model.php | 42 ++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 Core/System/class.abstract.model.php diff --git a/Core/Mods/databasemodel/moduleInfo.php b/Core/Mods/databasemodel/moduleInfo.php index 1557930..e705a41 100644 --- a/Core/Mods/databasemodel/moduleInfo.php +++ b/Core/Mods/databasemodel/moduleInfo.php @@ -5,6 +5,8 @@ return array( 'module_file' => 'class.model.php', 'module_name' => 'databasemodel', + 'abstract' => true, + 'name' => 'DatabaseModel', 'description' => 'Abstract type for easy database queries', 'author' => 'TechFuze', diff --git a/Core/System/class.abstract.model.php b/Core/System/class.abstract.model.php new file mode 100644 index 0000000..daec1f2 --- /dev/null +++ b/Core/System/class.abstract.model.php @@ -0,0 +1,42 @@ +$name; + } + + public function __set($name, $value) { + $parentClass->$name = $value; + } + + public function __call($name, $params) { + return call_user_func_array(array($this->parentClass, $name), $params); + } +} \ No newline at end of file From 9ce89a180e41a58a10f97ec5b564a5dfcf26b003 Mon Sep 17 00:00:00 2001 From: Abel Hoogeveen Date: Mon, 16 Mar 2015 13:29:03 +0100 Subject: [PATCH 3/3] It is now possible to have multiple types of models. Just add one call to a model like "$this->setType('techfuze/databasemodel', 'DatabaseModel');" and you load a FuzeWorks2 esque SQL model --- Application/Models/model.example.php | 3 +- Core/Mods/databasemodel/class.model.php | 10 ------ Core/Mods/databasemodel/moduleInfo.php | 2 +- ...odel-interpret.php => model.interpret.php} | 3 +- Core/System/class.abstract.model.php | 36 ++++++++++++++++--- Core/System/class.core.php | 11 +++++- Core/System/class.models.php | 25 +++---------- 7 files changed, 51 insertions(+), 39 deletions(-) rename Core/System/Models/{fz-model-interpret.php => model.interpret.php} (88%) diff --git a/Application/Models/model.example.php b/Application/Models/model.example.php index faa50eb..a9ac8ad 100644 --- a/Application/Models/model.example.php +++ b/Application/Models/model.example.php @@ -1,10 +1,11 @@ setType('techfuze/databasemodel', 'DatabaseModel'); $this->fields = array('id', 'key', 'value'); $this->table = 'example'; } diff --git a/Core/Mods/databasemodel/class.model.php b/Core/Mods/databasemodel/class.model.php index 40f6b38..06dd053 100644 --- a/Core/Mods/databasemodel/class.model.php +++ b/Core/Mods/databasemodel/class.model.php @@ -1,15 +1,5 @@ events->addListener(array($this, 'eventRegisterBuild'), 'eventRegisterBuildEvent', EventPriority::NORMAL); - } - - public function eventRegisterBuild($event) { - $event->addEvent('databasemodel', 'loadModelsEvent'); - return $event; - } -} class DatabaseModel extends Bus{ public $fields = array(); diff --git a/Core/Mods/databasemodel/moduleInfo.php b/Core/Mods/databasemodel/moduleInfo.php index e705a41..aafc5dc 100644 --- a/Core/Mods/databasemodel/moduleInfo.php +++ b/Core/Mods/databasemodel/moduleInfo.php @@ -1,7 +1,7 @@ 'DatabaseModelManager', + 'module_class' => 'DatabaseModel', 'module_file' => 'class.model.php', 'module_name' => 'databasemodel', diff --git a/Core/System/Models/fz-model-interpret.php b/Core/System/Models/model.interpret.php similarity index 88% rename from Core/System/Models/fz-model-interpret.php rename to Core/System/Models/model.interpret.php index 917d821..3dc6cbc 100644 --- a/Core/System/Models/fz-model-interpret.php +++ b/Core/System/Models/model.interpret.php @@ -1,10 +1,11 @@ setType('techfuze/databasemodel', 'DatabaseModel'); $this->fields = array(); $this->table = ''; } diff --git a/Core/System/class.abstract.model.php b/Core/System/class.abstract.model.php index daec1f2..6630c79 100644 --- a/Core/System/class.abstract.model.php +++ b/Core/System/class.abstract.model.php @@ -10,7 +10,7 @@ abstract class Model extends Bus{ /** * The parent class holder object * Requests get redirected to this class - * @access public + * @access private * @var Parent Object */ private $parentClass; @@ -24,18 +24,44 @@ abstract class Model extends Bus{ parent::__construct($core); } - protected function setType($type) { - + /** + * Set the type of this model. Eg, use techfuze/databasemodel and Databasemodel to get a SQL connected model + * @access protected + * @param String Module_name, the name of the module where the model can be found + * @param String class name, the class to load and connect to + */ + protected function setType($module_name, $class_name) { + $this->core->loadMod($module_name); + $this->parentClass = new $class_name($this->core); } + /** + * Retrieves a value from the model class + * @access public + * @param Any key + * @return Any value from the model class + */ public function __get($name) { - return $parentClass->$name; + return $this->parentClass->$name; } + /** + * Sets a value in the model class + * @access public + * @param Any key + * @param Any value + */ public function __set($name, $value) { - $parentClass->$name = $value; + $this->parentClass->$name = $value; } + /** + * Calls a function in the model class + * @access public + * @param String function_name + * @param Array values + * @return Function return + */ public function __call($name, $params) { return call_user_func_array(array($this->parentClass, $name), $params); } diff --git a/Core/System/class.core.php b/Core/System/class.core.php index 171452c..3eecd90 100644 --- a/Core/System/class.core.php +++ b/Core/System/class.core.php @@ -40,6 +40,7 @@ class Core { require_once(FUZESYSPATH . "/class.abstract.bus.php"); require_once(FUZESYSPATH . "/class.abstract.event.php"); require_once(FUZESYSPATH . "/class.abstract.module.php"); + require_once(FUZESYSPATH . "/class.abstract.model.php"); require_once(FUZESYSPATH . "/class.abstract.eventPriority.php"); // Load the core classes @@ -151,7 +152,15 @@ class Core { return false; } - // Create class object + // If it is an abstract module, return an StdClass for the memory address + if (isset($cfg->abstract)) { + if ($cfg->abstract) { + $c = new stdClass(); + return array($c, $cfg->module_name); + } + } + + // Otherwise create the class object $CLASS = new $class_name($this); if (method_exists($CLASS, 'setModulePath')) { $CLASS->setModulePath($cfg->directory); diff --git a/Core/System/class.models.php b/Core/System/class.models.php index 4d4c5eb..0abf1e0 100644 --- a/Core/System/class.models.php +++ b/Core/System/class.models.php @@ -14,40 +14,29 @@ class Models extends Bus{ } public function loadModel($name, $directory = null){ - if ($this->models_loaded) { - $this->events->fireEvent('modelsLoadEvent'); - $this->models_loaded = true; - } - if($directory === null){ $directory = FUZEPATH . "/Application/Models"; } $file = $directory.'/model.'.$name.'.php'; if (isset($this->model_types[$name])) { - $this->logger->logInfo('MODEL LOAD: '.get_class($this->model_types[$name]), get_class($this->model_types[$name]), __FILE__, __LINE__); + $this->logger->logInfo('Loading Model: '.get_class($this->model_types[$name]), get_class($this->model_types[$name])); $this->models_array[$name] = $this->model_types[$name]; } elseif (file_exists($file)){ require_once($file); $model = ucfirst($name); - $this->logger->logInfo('MODEL LOAD: '.$model, $model, __FILE__, __LINE__); + $this->logger->logInfo('Loading Model: '.$model, $model); $this->models_array[$name] = new $model($this->core); } else{ - $this->logger->logWarning('The requested model: \''.$name.'\' could not be found. Loading empty model', 'FuzeWorks->Model'); - require_once(FUZEPATH . "/Core/System/Models/fz-model-interpret.php"); - $this->logger->logInfo('MODEL LOAD: interprated model', 'FuzeWorks->Model', __FILE__, __LINE__); + $this->logger->logWarning('The requested model: \''.$name.'\' could not be found. Loading empty model', 'Models'); + require_once(FUZEPATH . "/Core/System/Models/model.interpret.php"); + $this->logger->logInfo('Loading Model: interprated databasemodel', 'Models'); $model = new Interpret($this->core); $model->table($name); $this->models_array[$name] = $model; } } - public function register($NAME, $MODEL_OBJECT) { - if (!isset($this->model_types[strtolower($NAME)])) { - $this->model_types[strtolower($NAME)] = $MODEL_OBJECT; - } - } - public function __get($name){ if (isset($this->models_array[strtolower($name)])) { return $this->models_array[strtolower($name)]; @@ -56,10 +45,6 @@ class Models extends Bus{ return $this->models_array[strtolower($name)]; } } - - public function getEmptyModel() { - return new \FuzeWorks\V100\DatabaseModel($this->core); - } } ?> \ No newline at end of file