<?php
/* Please see the README and LICENSE files. */
/**
* The system maintains a reference to each of the started components and is
* thus able to stop them if required.
*/
class System {
/**
* The components that got started
* @var System_Component[]
*/
protected $components;
/**
* The running timmer
* @var System_Timer
*/
protected $timer;
/**
* Start a new system
* @param String[] $component_array
*/
public function __construct($component_array){
$this->start_components($component_array);
$this->timer = new System_Timer();
}
/**
* Destroy the systme
*/
public function __destruct() {
if(!System_Shutdown::$triggered){
echo("System shutdown");
System_Shutdown::shutdown();
}
//echo(sprintf("<span style=\"font-size:12px;color:#000\">Debug Diagnostics<br/>Pageload %s s<br/>Max Mem. %s b</span>", number_format($this->timer->stop(),3), memory_get_peak_usage()));
}
/**
* Start each component
*
* @param String[] $component_array
* @return Boolean false if array is corrupt
*/
protected function start_components($component_array){
if(!is_array($component_array) || count($component_array)<1){
trigger_error("Bad input for component array");
return false;
}
foreach($component_array as $component_name){
$this->components[] = new $component_name();
end($this->components)->start();
}
return true;
}
/**
* Stop all of the components
*/
public function stop_components(){
foreach($this->components as $component){
$this->stop_component($component);
}
}
/**
* Stop a specific component
* @param System_Component $class
*/
public function stop_component($class){
if($class instanceof System_Component){
return $class->stop();
} else if(strlen($class)>0) {
foreach($this->components as $component){
if(get_class($component)==$class){
return $component->stop();
}
}
}
return false;
}
/**
* tock
*/
public function tick($text){
$this->timer->tick($text);
}
public function get_time(){
$this->timer->stop();
return $this->timer->get_time();
}
}
?>