Location: PHPKode > scripts > NabiCI > CI/Loop.php
<?php
error_reporting(E_ALL);
require_once (realpath(dirname(__FILE__)).DIRECTORY_SEPARATOR.
	'Process.php');
require_once (realpath(dirname(__FILE__)).DIRECTORY_SEPARATOR.
	'website_builder.php');
require_once (realpath(dirname(__FILE__)).DIRECTORY_SEPARATOR.
	'Continuous_integration.php');
require_once (realpath(dirname(__FILE__)).DIRECTORY_SEPARATOR.
	'Task/Task.php');

/**
 * class Loop extends from Process and prepares necessary steps to execute the Tasks one after another
 *
 *
 * @author Derya Oez
 * @version 1.0
 * @package NabidooCI
 */

class Loop extends Process
{
	/**
	 * Objects of Task save as an Array
	 * 
	 * @access private
	 * @var Array
	 */
	
	private $task=array();
	
	/**
	 * all Task names of a running Continuous Integration Loop which are read out from the configuration file
	 * 
	 * @access private
	 * @var String
	 */
	
	private $allTaskNames="";
	
	/**
	 * The deadline is needed to set a time limit for the web page, if the web page is over the limit, then out of date
	 * The deadline in minutes is read out from the configuration file
	 * 
	 * @access private
	 * @var Integer
	 */
	
	private $deadline;
	
	/**
	 * Object of the Class WebsiteBuilder is used create the website
	 * 
	 * @access private
	 * @var Object of WebsiteBuilder
	 */
	
	private $webBuildObject; 

	/**
	 * function __construct($name)
	 * 
	 * @access public
	 * @param String $name 
	 * Instruction which Continuous Integration Loop should run
	 */

	public function __construct($name)
	{
		$this->name=$name;
	}

	/**
	 * function run($iniArray) is used to prepare information of the Loop,
	 * initialize to create web pages and to run the Tasks
	 *
	 * @access public
	 * @param Array $iniArray
	 * all settings from the configuration file
	 */

	public function run($iniArray)
	{
		//successful is true means that the loop plus task is running without errors
		$this->successful = true;
		$this->allTaskNames = $iniArray[$this->name]["tasks"];
		//deadline will be converted in minutes
		$this->deadline = $iniArray[$this->name]["deadline"]*60;
		//starttime of the Loop
		$this->startTime= time();
		// running is true means, that the Loop is running
		$this->running=true;
		//create an Object of WebsiteBuilder
		//create the web page for the current loop
		$this->webBuildObject= new WebsiteBuilder($this,"Current");
		//$tasks is an Array of all tasknames
		$tasks = explode(" ",$this->allTaskNames);

		//analyze the task mode for correct execution of the Task
		foreach($tasks as $taskName)
		{
			$taskAttributes = $iniArray[$taskName]; //array

			if (! isset($taskAttributes["expected_result"])){
				$taskAttributes["expected_result"]="";
			}
			//--------------------------------------

			if($taskAttributes["task_mode"]=="url")
			{
				$tempCom = "url";
				$this->executeTask($taskName,$taskAttributes["task_mode"],$taskAttributes["expected_result"],$taskAttributes[$tempCom]);
			}
			//-------------------------------------------------------------------------------------

			elseif ($taskAttributes["task_mode"]=="system" || $taskAttributes["task_mode"]=="system_linewise")
			{
				$tempCom = "call";
				$this->executeTask($taskName,$taskAttributes["task_mode"],$taskAttributes["expected_result"],$taskAttributes[$tempCom]);
			}
			//---------------------------------------------------------------

			elseif ($taskAttributes["task_mode"]=="system_list")
			{
				$this->handleSpecialTasks($taskName,$taskAttributes,"call_list");
			}
			//-------------------------------------------------------------------

			elseif ($taskAttributes["task_mode"]=="url_list")
			{
				$this->handleSpecialTasks($taskName,$taskAttributes,"url_list");
			}
		}
		//fix the endtime of the Loop
		$this->endTime= time();
		//finish of Loop
		$this->running=false;
		$this->webBuildObject->startBuildWebsite();
		//create the web pages for the result of the Continuous Integration Loop
		new WebsiteBuilder($this,"Result");
	}

	/**
	 * function executeTask($taskName,$taskmode,$expectedRes,$com) is used
	 * to create and run Object of Task, also create the web page
	 *
	 * @access private
	 * @param String $taskName
	 * name of the Task
	 * @param String $taskmode
	 * the mode of the task
	 * @param String $expectedRes
	 * the expected result after run the task
	 * @param String $com
	 * a command to execute, depends on the task mode
	 */
	private function executeTask($taskName,$taskmode,$expectedRes,$com)
	{
		//create an Object of Task
		array_push($this->task,new Task($taskName,$taskmode,$expectedRes,$com));
		$this->webBuildObject->startBuildWebsite();
		//$a=$this->task[count($this->task)-1];
		//$b=count($this->task)-1;
		$this->task[count($this->task)-1]->run();
		$this->webBuildObject->startBuildWebsite();

		if($this->successful==true)
		{
			if ($this->task[count($this->task)-1]->getSuccessful()==false)
			{
				$this->successful=false;
				$this->webBuildObject->startBuildWebsite();
			}
		}
	}

	/**
	 * function handleSpecialTasks($taskName,$taskAttributes,$command) is used to to handle special Tasks, 
	 * which means it will be called a list contains commands which will be executed one after another till 
	 * finish, for each command it will be created an Object of Task to run
	 * 
	 * @access private
	 * @param String $taskName
	 * 	the name of the Task
	 * @param String $taskAttributes
	 * attributes of the Task
	 * @param String $command
	 * the command of this Task in this case it is e.g. "url_list", not the value of this
	 */

	private function handleSpecialTasks($taskName,$taskAttributes,$command)
	{
		//$listPath is the path to the "list"-file
		$listPath = $taskAttributes[$command];
		//if the "listpath" exists
		if (easy_file::exists($listPath))
		{
			//$fileContent contains the content from the "list-file"
			$fileContent= trim(file_get_contents($listPath));
			//$listCommand is an Array of the file content
			$listCommand = explode("\n",$fileContent);
			//prepare values of the taskattributes and initialize execution Task one after another till finish
			for ($y=0;$y<count($listCommand);$y++)
			{
				$listTaskName = $taskName."_".($y+1);
				$listTaskMode = $taskAttributes["task_mode"];
				$listExpResult = $taskAttributes["expected_result"];
				$listCom = $listCommand[$y];
				$this->executeTask($listTaskName,$listTaskMode,$listExpResult,$listCom);
			}
			// in case of the "listpath" does not exist, it will be warned
		}else {
			$listTaskName = $taskName;
			$listTaskMode = $taskAttributes["task_mode"];
			$listExpResult = $taskAttributes["expected_result"];
			$listCom="Warning: No such file or directory in ".$listPath;
			$this->executeTask($listTaskName,$listTaskMode,$listExpResult,$listCom);
		}
	}

	/**
	 * function getTasks() is used to get the Taskobjects
	 *
	 * @access public
	 * @return Array
	 * Object of Task
	 */

	public function getTasks()
	{
		return $this->task;
	}

	/**
	 * function getAllTaskNames() is used give an overview of the Tasks at the web page
	 *
	 * @access public
	 * @return String
	 * all names of the Tasks
	 */

	public function getAllTaskNames()
	{
		return $this->allTaskNames;
	}

	/**
	 * function getDeadline()is used to get a time period,
	 * the value is needed to set a time limit for the web page, 
	 * if the web page is over the limit, then out of date 
	 *
	 * @access public
	 * @return Integer
	 * deadline of the Loop for the web page
	 */

	public function getDeadline()
	{
		return $this->deadline;
	}
}
?>
Return current item: NabiCI