From: <gem...@li...> - 2012-05-07 12:53:52
|
Revision: 673 http://gemstracker.svn.sourceforge.net/gemstracker/?rev=673&view=rev Author: mennodekker Date: 2012-05-07 11:51:02 +0000 (Mon, 07 May 2012) Log Message: ----------- Add Gems_Model_ModelAbstract, that runs init() method after registry requests have been answered. This allows cleanup of existing models and later on the createmodel method of controllers can be simplified as common information can be stored in the model Modified Paths: -------------- trunk/library/classes/Gems/Model/OrganizationModel.php trunk/library/classes/Gems/Model/StaffModel.php trunk/library/classes/Gems/Model.php Added Paths: ----------- trunk/library/classes/Gems/Model/ModelAbstract.php Added: trunk/library/classes/Gems/Model/ModelAbstract.php =================================================================== --- trunk/library/classes/Gems/Model/ModelAbstract.php (rev 0) +++ trunk/library/classes/Gems/Model/ModelAbstract.php 2012-05-07 11:51:02 UTC (rev 673) @@ -0,0 +1,111 @@ +<?php +/** + * Copyright (c) 2011, Erasmus MC + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of Erasmus MC nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * Short description of file + * + * @package Gems + * @subpackage + * @copyright Copyright (c) 2011 Erasmus MC + * @license New BSD License + * @version $Id: Sample.php 215 2011-07-12 08:52:54Z michiel $ + */ + +/** + * Short description for ModelAbstract + * + * Long description for class ModelAbstract (if any)... + * + * @package Gems + * @subpackage Sample + * @copyright Copyright (c) 2011 Erasmus MC + * @license New BSD License + * @since Class available since version 1.0 + * @deprecated Class deprecated since version 2.0 + */ +class Gems_Model_ModelAbstract extends Gems_Model_JoinModel implements MUtil_Registry_TargetInterface +{ + /** + * Allows the loader to set resources. + * + * @param string $name Name of resource to set + * @param mixed $resource The resource. + * @return boolean True if $resource was OK + */ + public function answerRegistryRequest($name, $resource) + { + $this->$name = $resource; + + return true; + } + + /** + * Should be called after answering the request to allow the Target + * to check if all required registry values have been set correctly. + * + * @return boolean False if required are missing. + */ + public function checkRegistryRequestsAnswers() + { + return true; + } + + /** + * Filters the names that should not be requested. + * + * Can be overriden. + * + * @param string $name + * @return boolean + */ + protected function filterRequestNames($name) + { + return '_' !== $name[0]; + } + + /** + * Allows the loader to know the resources to set. + * + * Returns those object variables defined by the subclass but not at the level of this definition. + * + * Can be overruled. + * + * @return array of string names + */ + public function getRegistryRequests() + { + // Filter using the $this->filterRequestNames() callback + return array_filter(array_keys(get_object_vars($this)), array($this, 'filterRequestNames')); + } + + /** + * This is the place to put code to run after object initialization has finished and when all + * registry requests have been answered + */ + public function init() + { + } +} \ No newline at end of file Modified: trunk/library/classes/Gems/Model/OrganizationModel.php =================================================================== --- trunk/library/classes/Gems/Model/OrganizationModel.php 2012-05-07 09:27:28 UTC (rev 672) +++ trunk/library/classes/Gems/Model/OrganizationModel.php 2012-05-07 11:51:02 UTC (rev 673) @@ -45,7 +45,7 @@ * @license New BSD License * @since Class available since version 1.5 */ -class Gems_Model_OrganizationModel extends Gems_Model_JoinModel implements MUtil_Registry_TargetInterface +class Gems_Model_OrganizationModel extends Gems_Model_ModelAbstract { /** * @var Gems_Loader @@ -58,58 +58,6 @@ } /** - * Allows the loader to set resources. - * - * @param string $name Name of resource to set - * @param mixed $resource The resource. - * @return boolean True if $resource was OK - */ - public function answerRegistryRequest($name, $resource) - { - $this->$name = $resource; - - return true; - } - - /** - * Should be called after answering the request to allow the Target - * to check if all required registry values have been set correctly. - * - * @return boolean False if required are missing. - */ - public function checkRegistryRequestsAnswers() - { - return true; - } - - /** - * Filters the names that should not be requested. - * - * Can be overriden. - * - * @param string $name - * @return boolean - */ - protected function filterRequestNames($name) - { - return '_' !== $name[0]; - } - - /** - * Allows the loader to know the resources to set. - * - * Returns those object variables defined by the subclass but not at the level of this definition. - * - * Can be overruled. - * - * @return array of string names - */ - public function getRegistryRequests() - { - return array_filter(array_keys(get_object_vars($this)), array($this, 'filterRequestNames')); - } - - /** * Save a single model item. * * Makes sure the password is saved too using the userclass Modified: trunk/library/classes/Gems/Model/StaffModel.php =================================================================== --- trunk/library/classes/Gems/Model/StaffModel.php 2012-05-07 09:27:28 UTC (rev 672) +++ trunk/library/classes/Gems/Model/StaffModel.php 2012-05-07 11:51:02 UTC (rev 673) @@ -45,7 +45,7 @@ * @license New BSD License * @since Class available since version 1.5 */ -class Gems_Model_StaffModel extends Gems_Model_JoinModel implements MUtil_Registry_TargetInterface +class Gems_Model_StaffModel extends Gems_Model_ModelAbstract { /** * @var Gems_Loader @@ -58,58 +58,6 @@ } /** - * Allows the loader to set resources. - * - * @param string $name Name of resource to set - * @param mixed $resource The resource. - * @return boolean True if $resource was OK - */ - public function answerRegistryRequest($name, $resource) - { - $this->$name = $resource; - - return true; - } - - /** - * Should be called after answering the request to allow the Target - * to check if all required registry values have been set correctly. - * - * @return boolean False if required are missing. - */ - public function checkRegistryRequestsAnswers() - { - return true; - } - - /** - * Filters the names that should not be requested. - * - * Can be overriden. - * - * @param string $name - * @return boolean - */ - protected function filterRequestNames($name) - { - return '_' !== $name[0]; - } - - /** - * Allows the loader to know the resources to set. - * - * Returns those object variables defined by the subclass but not at the level of this definition. - * - * Can be overruled. - * - * @return array of string names - */ - public function getRegistryRequests() - { - return array_filter(array_keys(get_object_vars($this)), array($this, 'filterRequestNames')); - } - - /** * Save a single model item. * * Makes sure the password is saved too using the userclass Modified: trunk/library/classes/Gems/Model.php =================================================================== --- trunk/library/classes/Gems/Model.php 2012-05-07 09:27:28 UTC (rev 672) +++ trunk/library/classes/Gems/Model.php 2012-05-07 11:51:02 UTC (rev 673) @@ -96,6 +96,30 @@ protected $util; /** + * Create or loads the class. When only loading, this function returns a StaticCall object that + * can be invoked lazely. + * + * @see MUtil_Lazy_StaticCall + * @see MUtil_Registry_TargetInterface + * + * @param type $name + * @param type $create + * @param array $arguments + * @return Gems_Model_ModelAbstract + */ + protected function _loadClass($name, $create = false, array $arguments = array()) + { + $obj = parent::_loadClass($name, $create, $arguments); + + // If it as a Gems_Model_ModelAbstract, run the init method now + if ($obj instanceof Gems_Model_ModelAbstract) { + $obj->init(); + } + + return $obj; + } + + /** * Link the model to the user_logins table. * * @param Gems_Model_JoinModel $model @@ -111,6 +135,7 @@ * Link the model to the user_passwords table. * * @param Gems_Model_JoinModel $model + * @deprecated since version 1.5.4 */ public static function addUserPassword(Gems_Model_JoinModel $model) { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |