Hooks =& $this->Application->makeClass('kHookManager');
$this->Agents =& $this->Application->makeClass('kAgentManager');
$this->Request =& $this->Application->makeClass('kRequestManager');
}
/**
* Sets data from cache to object
*
* @param Array $data
* @access public
*/
public function setFromCache(&$data)
{
$this->Hooks->setFromCache($data);
$this->Agents->setFromCache($data);
$this->buildEvents = $data['EventManager.buildEvents'];
}
/**
* Gets object data for caching
*
* @return Array
* @access public
*/
public function getToCache()
{
return array_merge(
$this->Hooks->getToCache(),
$this->Agents->getToCache(),
Array (
'EventManager.buildEvents' => $this->buildEvents,
)
);
}
/**
* Returns information about registered agents
*
* @param bool $from_cache
* @return Array
* @access public
*/
public function getAgents($from_cache = false)
{
return $this->Agents->getAll($from_cache);
}
/**
* Add new agent
*
* @param string $short_name name to be used to store last maintenace run info
* @param string $event_name
* @param int $run_interval run interval in seconds
* @param int $type before or after agent
* @param int $status
* @access public
*/
public function registerAgent($short_name, $event_name, $run_interval, $type = reBEFORE, $status = STATUS_ACTIVE)
{
$this->Agents->add($short_name, $event_name, $run_interval, $type, $status);
}
/**
* Run registred agents with specified event type
*
* @param int $event_type
* @param bool $from_cron
* @access public
*/
public function runAgents($event_type = reBEFORE, $from_cron = false)
{
$this->Agents->runAgents($event_type, $from_cron);
}
/**
* Runs agent based on given data
*
* @param Array $agent_data
* @return bool
* @access public
*/
public function runAgent($agent_data)
{
return $this->Agents->runAgent($agent_data);
}
/**
* Registers Hook from subprefix event to master prefix event
*
* Pattern: Observer
*
* @param string $hook_event
* @param string $do_event
* @param int $mode
* @param bool $conditional
* @access public
*/
public function registerHook($hook_event, $do_event, $mode = hAFTER, $conditional = false)
{
$this->Hooks->registerHook($hook_event, $do_event, $mode, $conditional);
}
/**
* Registers build event for given pseudo class
*
* @param string $pseudo_class
* @param string $event_name
* @access public
*/
public function registerBuildEvent($pseudo_class, $event_name)
{
$this->buildEvents[$pseudo_class] = $event_name;
}
/**
* Runs build event for given $pseudo_class instance, when defined
*
* @param string $prefix_special
* @param string $pseudo_class
* @param Array $event_params
* @access public
*/
public function runBuildEvent($prefix_special, $pseudo_class, $event_params)
{
if ( !isset($this->buildEvents[$pseudo_class]) ) {
return ;
}
$event = new kEvent($prefix_special . ':' . $this->buildEvents[$pseudo_class], $event_params);
$this->HandleEvent($event);
}
/**
* Check if event is called twice, that causes recursion
*
* @param kEvent $event
* @return bool
* @access protected
*/
protected function isRecursion(&$event)
{
$event_key = $event->getPrefixSpecial() . ':' . $event->Name;
return in_array($event_key, $this->recursionStack);
}
/**
* Adds event to recursion stack
*
* @param kEvent $event
* @access protected
*/
protected function pushEvent(&$event)
{
$event_key = $event->getPrefixSpecial() . ':' . $event->Name;
array_push($this->recursionStack, $event_key);
}
/**
* Removes event from recursion stack
*
* @access protected
*/
protected function popEvent()
{
array_pop($this->recursionStack);
}
/**
* Allows to process any type of event
*
* @param kEvent $event
* @access public
*/
public function HandleEvent(&$event)
{
if ( $this->isRecursion($event) || !$this->verifyEventPrefix($event) ) {
return ;
}
$this->pushEvent($event);
if (!$event->SkipBeforeHooks) {
$this->Hooks->runHooks($event, hBEFORE);
if ($event->status == kEvent::erFATAL) {
return ;
}
}
$event_handler =& $this->Application->recallObject($event->Prefix . '_EventHandler');
/* @var $event_handler kEventHandler */
$event_handler->processEvent($event);
if ($event->status == kEvent::erFATAL) {
return ;
}
if (!$event->SkipAfterHooks) {
$this->Hooks->runHooks($event, hAFTER);
}
$this->popEvent();
}
/**
* Checks if event prefix is valid
*
* @param kEvent $event
* @param bool $is_fatal
* @return string
* @access public
*/
public function verifyEventPrefix(&$event, $is_fatal = false)
{
if ( !$this->Application->prefixRegistred($event->Prefix) ) {
$this->Application->UnitConfigReader->loadConfig($event->Prefix);
if ( !$this->Application->prefixRegistred($event->Prefix) ) {
$error_msg = 'Prefix ' . $event->Prefix . ' not registred (requested event ' . $event->Name . ')';
if ($is_fatal) {
throw new Exception($error_msg);
}
else {
trigger_error($error_msg, E_USER_WARNING);
}
return false;
}
}
return true;
}
/**
* Processes request
*
* @access public
*/
public function ProcessRequest()
{
$this->Request->ProcessRequest();
}
/**
* Allows to add new element to opener stack
*
* @param string $template
* @param Array $params
* @param string $pass
* @access public
*/
public function openerStackPush($template, $params, $pass = 'all', $wid = null)
{
$this->Request->openerStackPush($template, $params, $pass, $wid);
}
/**
* Allows to change last element in opener stack
*
* @param string $template
* @param Array $params
* @param string $pass
* @access public
*/
public function openerStackChange($params = Array(), $pass_events = true, $wid = null)
{
$this->Request->openerStackChange($params, $pass_events, $wid);
}
/**
* Set's new event for $prefix_special
* passed
*
* @param string $prefix_special
* @param string $event_name
* @access public
*/
public function setEvent($prefix_special,$event_name)
{
$actions =& $this->Application->recallObject('kActions');
$actions->Set('events['.$prefix_special.']',$event_name);
}
/**
* Allows to determine, that required event is beeing processed right now
*
* @param string $event_key Event name in format prefix[.special]:event_name
* @return bool
* @access public
*/
public function eventRunning($event_key)
{
return array_search($event_key, $this->recursionStack) !== false;
}
}