[
class tree: tangra_lib
] [
index: tangra_lib
] [
all elements
]
tangra_lib
Packages:
tangra_lib
Source for file web_events_dispatcher.class.php
Documentation is available at
web_events_dispatcher.class.php
<?php
// *** Tangra (Application Framework and Tools for PHP)
// $Id$
//
/**
* Contains class Web_Events_Dispatcher
*
*
@package
tangra_lib
*
@subpackage
web_site
*/
/**
*
*/
require_once
(
TANGRA_MAIN_DIR
.
'exceptions/te_key_already_exists.class.php'
)
;
/**
*
*/
require_once
(
TANGRA_MAIN_DIR
.
'web_site/web_event_composite.class.php'
)
;
/**
* Web_Event_Dispatcher is responsible for monitoring occurence of
* Web_Events and dispatching of program flow
*
*
@package
tangra_lib
*
@subpackage
web_site
*/
class
Web_Event_Dispatcher
extends
Tangra_Class
{
/**
* Reference to Web_page object
*
*
@var
Web_page
*
@internal
*/
private
$page_pointer
;
/**
* Default event-action pair
*
*
@var
unknown_type
*
@internal
*/
private
$default_event_action_pair
;
/**
* Array containing event-action pairs
*
*
@var
array
*
@internal
*/
private
$event_action_pairs
=
array
(
)
;
/**
* Array that contains prioritetes of events
*
*
@var
array
*
@internal
*/
private
$order_arr
=
array
(
)
;
/**
* Constructor
*
*
@param
Web_Page
$page_pointer
Page that instantiates this object
*/
function
__construct
(
Web_Page
$page_pointer
)
{
$this
->
page_pointer
=
$page_pointer
;
}
/**
* Sets default event-action pair
*
*
@param
Web_Event
$we
*
@param
string
$target_method
Name of the Web_Page method that will be triggered if event occures
*/
function
set_default_event_action_pair
(
Web_Event
$we
,
$target_method
)
{
$this
->
default_event_action_pair
[
'event'
]
=
$we
;
$this
->
default_event_action_pair
[
'target'
]
=
$target_method
;
}
/**
* Adds new event-action pair
*
*
@param
Web_Event
$we
*
@param
unknown_type
$target_method
Name of the Web_Page method that will be triggered if event occures
*
@param
integer
$order
Order of this event. Events will be evaluated ordered by their $order.
*
@throws
Tangra_Exception, TE_Key_Already_Exists
*/
public
function
add_wed_event_action_pair
(
Web_Event
$we
,
$target_method
,
$order
=
10
)
{
tangra_if_not_int_throw_e
(
$order
)
;
if
(
!
array_key_exists
(
$we
->
get_name
(
)
,
$this
->
event_action_pairs
))
{
if
(
method_exists
(
$this
->
page_pointer
,
$target_method
))
{
$this
->
event_action_pairs
[
$we
->
get_name
(
)
]
=
array
(
'event'
=>
$we
,
'target'
=>
$target_method
)
;
$this
->
order_arr
[
$we
->
get_name
(
)
]
=
$order
;
asort
(
$this
->
order_arr
)
;
}
else
{
throw
new
Tangra_Exception
(
'Method not callable: '
.
$target_method
)
;
}
}
else
{
throw
new
TE_Key_Already_Exists
(
$we
->
get_name
(
))
;
}
}
/**
* Returns array dispatch direction
*
* Returns array:
* ['target'] - action of the event, method that will be triggered
* ['param'] - captured value/array of values
*
*
@param
Web_Context
$context
*
@return
array
*/
public
function
get_dispatch
(
Web_Context
$context
)
{
$ret
=
false
;
foreach
(
$this
->
order_arr
as
$ev_name
=>
$order
)
{
$ea_pair
=
$this
->
event_action_pairs
[
$ev_name
]
;
$param
=
$ea_pair
[
'event'
]
->
is_it_me
(
$context
)
;
if
(
$param
!==
false
)
{
$ret
[
'target'
]
=
$ea_pair
[
'target'
]
;
$ret
[
'param'
]
=
$param
;
break
;
}
}
if
(
!
$ret
)
{
$ret
[
'target'
]
=
$this
->
default_event_action_pair
[
'target'
]
;
$ret
[
'param'
]
=
NULL
;
}
return
$ret
;
}
/**
* Checks if there are duplicate captures
*
*
@return
string
Capture label
*/
public
function
check_for_duplicate_captures
(
)
{
$ret
=
false
;
$tmp_arr
=
array
(
)
;
foreach
(
$this
->
event_action_pairs
as
$eap
)
{
$tmp_capture
=
$eap
[
'event'
]
->
get_capture
(
)
;
if
(
is_array
(
$tmp_capture
))
{
//composite event
foreach
(
$tmp_capture
as
$capture
)
{
if
(
!
array_key_exists
(
$capture
,
$tmp_arr
))
{
$tmp_arr
[
$capture
]
=
$capture
;
}
else
{
$ret
=
$capture
;
break
2
;
}
}
}
else
{
//simple event
if
(
!
array_key_exists
(
$tmp_capture
,
$tmp_arr
))
{
$tmp_arr
[
$tmp_capture
]
=
$tmp_capture
;
}
else
{
$ret
=
$tmp_capture
;
break
;
}
}
}
return
$ret
;
}
/**
* Checks if event with name <var>$event_name</var> is already registered
*
*
@param
unknown_type
$event_name
*
@return
boolean
*/
public
function
is_event_registered
(
$event_name
)
{
return
array_key_exists
(
$event_name
,
$this
->
event_action_pairs
)
;
}
/**
* Returns event action pairs
*
*
@return
array
*/
public
function
get_event_action_pairs
(
)
{
return
$this
->
event_action_pairs
;
}
}