Location: PHPKode > projects > Test Suites Results Parser and Browser > tslogparser-v07/admin/modules.inc.php
<?php
/*
 * Copyright (c) 2005, Bull S.A..  All rights reserved.
 * Created by: Sebastien Decugis
 
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of version 2 of the GNU General Public License as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it would be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write the Free Software Foundation, Inc., 59
 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
 */

/* This file defines the base module class which will be used.

 A module consists in four functions closely related to a testsuite model ("type").
 These functions are:
  int TS_parse(ref parent, string TS_name, string TS_description, string TS_path);
  int TS_delete(ref parent, int TS_id);
  int RUN_parse(ref parent, string RUN_name, string RUN_description, int TS_id, string CONTENT);
  int RUN_delete(ref parent, int RUN_id);
 
 An additionnal function is added for informational purpose (no functionnal need)
  string module_info(void)
 
 TS_parse will check for the directory TS_path and analyse its content.
  In case a correct testsuite structure is found, the testsuite is parsed
  and put into the database with name and description as provided.
  The return value is $true if success and $false otherwise.
  
 TS_delete will search the database for a testsuite TS_id, then delete it.
  In case the testsuite contains runs, TS_delete will fail.
  In case TS_id is not of the correct type, TS_delete will fail.
  Otherwise, the testsuite is deleted from the database.
  The return value is $true if success and $false otherwise.
 
 RUN_parse will add a new run in the database.
  It starts with checking that TS_id exists and is of the correct testsuite type.
  Then it analyzes CONTENT and if a correct run format is found, the results
  are added into the database with RUN_name and RUN_description information.
  The return value is $true if success and $false otherwise.
 
 RUN_delete will delete RUN_id run from database.
  It starts with checking that RUN_id belongs to a testsuite of the correct type.
  Then it removes it from the database.
  The return value is $true if success and $false otherwise.

 All 4 previous functions have a 'parent' pointer to the testsuite class. This is
  used for error reporting directly into the parent's 'last_error' property. 
  
 module_info will return an HTML-formated text (enclosed in <p> and </p> tags)
  describing the module and the testsuite it supports.
  All information related to the module (version, known bugs, ...) are suitable 
  for this function (think of it as the only documentation for the module).
 
*/

/* This particular file mechanism is as follow:

 -> Include each .php file from the modules subdirectory.
   Each inclusion returns its module name, which is added into the base catalog.
 
 -> The function list_modules returns the catalog.
 -> The function select_module selects one of the modules as the working module.
     (provider of the modules functions as described earlier). It returns true on success,
     false if the requested module is not available.
 -> The function selected_module returns the currently selected module.
 -> The function deselect_module changes the selection back to none.
 
 -> The TS_parse, TS_delete, RUN_parse, RUN_delete and module_info functions
     are wrappers to the selected module functions, or return an error when no 
     module is selected.
 
*/

class testsuite
{
	var $modules_catalog;
	var $selected_module;
	var $last_error="";
	var $debug=0;
	
	/* Class constructor: initializes the modules catalog */
	function testsuite()
	{
		$this->modules_catalog=array();
		$this->selected_module="";
		
		/* Enumerate the modules files and include them */
		foreach (glob("modules/*.mod.php") as $filename) 
		{
			$tmp = require($filename);
			if (is_string($tmp) && class_exists($tmp))
			{
				$this->modules_catalog[]=$tmp;
			}
			else
			{
				die("File $filename has an incorrect format\n");
			}
		}
	}
	
	/* Functions for manipulating the selected module */
	function list_modules()
	{
		return $this->modules_catalog;
	}
	
	function selected_module()
	{
		return $this->selected_module;
	}
	
	function select_module($newmod)
	{
		if (in_array($newmod, $this->modules_catalog))
		{
			/* Set the module for this execution */
			$this->selected_module=$newmod;
			return true;
		}
		return false;
	}
	
	function deselect_module()
	{
		$this->selected_module="";
	}
	
	
	/* Wrapper functions to the selected module */
	function module_info($what="", $module="")
	{
		if (!$module)
		{
			/* Check the environment is correct */
			if ($this->selected_module == "")
			{
				die("No selected module");
			}
			$module=$this->selected_module;
		}
		if (!is_callable(array($module,'module_info')))
		{
			die("Module ".$module." is malformed.\n");
		}
		/* Call actually the module method */
		return call_user_func_array(
			array($module, 'module_info'),
			array($what));
	}
			
	function TS_parse($TS_name, $TS_description, $path)
	{
		/* Check the environment is correct */
		if ($this->selected_module == "")
		{
			die("No selected module");
		}
		if (!is_callable(array($this->selected_module,'TS_parse')))
		{
			die("Module ".$this->selected_module." is malformed.\n");
		}
		/* Call actually the module method */
		return call_user_func_array(
			array($this->selected_module, 'TS_parse'),
			array(&$this, $TS_name, $TS_description, $path));
	}
	function TS_delete($TS_ID)
	{
		/* Check the environment is correct */
		if ($this->selected_module == "")
		{
			die("No selected module");
		}
		if (!is_callable(array($this->selected_module,'TS_delete')))
		{
			die("Module ".$this->selected_module." is malformed.\n");
		}
		/* Call actually the module method */
		return call_user_func_array(
			array($this->selected_module, 'TS_delete'),
			array(&$this, $TS_ID));
	}
	function RUN_parse($RUN_name, $RUN_description, $TS_id, &$CONTENT)
	{
		/* Check the environment is correct */
		if ($this->selected_module == "")
		{
			die("No selected module");
		}
		if (!is_callable(array($this->selected_module,'RUN_parse')))
		{
			die("Module ".$this->selected_module." is malformed.\n");
		}
		/* Call actually the module method */
		return call_user_func_array(
			array($this->selected_module, 'RUN_parse'),
			array(&$this, $RUN_name, $RUN_description, $TS_id, $CONTENT));
	}
	function RUN_delete($RUN_id)
	{
		/* Check the environment is correct */
		if ($this->selected_module == "")
		{
			die("No selected module");
		}
		if (!is_callable(array($this->selected_module,'RUN_delete')))
		{
			die("Module ".$this->selected_module." is malformed.\n");
		}
		/* Call actually the module method */
		return call_user_func_array(
			array($this->selected_module, 'RUN_delete'),
			array(&$this, $RUN_id));
	}
}

?>
   
Return current item: Test Suites Results Parser and Browser