Smalldb  v0.0-177-g1a83180
 All Classes Namespaces Functions Variables Pages
Public Member Functions | Public Attributes | Protected Member Functions | Protected Attributes | List of all members
AbstractMachine Class Reference

Implementation of the state machine. More...

Inheritance diagram for AbstractMachine:
Inheritance graph

Public Member Functions

 __construct (AbstractBackend $backend, $type, $config, $context)
 Constructor. More...
 
 getState ($id)
 Get current state of state machine. More...
 
 getProperties ($id, &$state_cache=null)
 Get properties of state machine, including it's state. More...
 
 getView ($id, $view, &$properties_cache=null, &$view_cache=null, &$persistent_view_cache=null)
 Get properties in given view. More...
 
 isTransitionAllowed ($id, $transition_name, $state=null)
 Returns true if transition can be invoked right now. More...
 
 getAvailableTransitions ($id, $state=null)
 Get list of all available actions for state machine instance identified by $id.
 
 invokeTransition ($id, $transition_name, $args, &$returns)
 Invoke state machine transition. More...
 
 flushCache ()
 If machine properties are cached, flush all cached data.
 
 getMachineType ()
 Get type of this machine.
 
 getBackend ()
 Get backend which owns this machine.
 

Public Attributes

const RETURNS_VALUE = null
 Return value of invoked transition is just some value.
 
const RETURNS_NEW_ID = 'new_id'
 Return value of invoked transition is new ID of the state machine.
 

Protected Member Functions

 initializeMachine ($config)
 Define state machine used by all instances of this type.
 
 checkAccessPolicy ($access_policy, $id)
 Returns true if user has required access_policy to invoke a transition, which requires given access_policy.
 
 resolveMachineReference ($reference_name, $properties_cache, &$ref_machine_type, &$ref_machine_id)
 Helper function to resolve reference to another machine. More...
 
 onStateChanged ($id, $old_state, $transition_name, $new_state)
 Called when state is changed, when transition invocation is completed.
 

Protected Attributes

 $backend
 Backend, where all machines are stored.
 
 $context
 Global context passed from backend.
 
 $machine_type
 Identification within $backend.
 
 $url_fmt
 URL format string where machine is located, usualy only the path part, e.g. More...
 
 $parent_url_fmt
 URL format string where parent of this machine is located, usualy only the path part, e.g. More...
 
 $post_action_url_fmt
 URL format string for redirect-after-post. More...
 
 $states
 Descriptions of all known states – key is state id, value is * description. More...
 
 $state_groups
 State groups. More...
 
 $actions
 Description of all known actions – key is action name. More...
 
 $default_access_policy = null
 Default access policy. More...
 
 $read_access_policy = null
 Read access policy. More...
 
 $access_policies
 Access policies. More...
 
 $properties
 Description of machine properties – key is property name. More...
 
 $views
 Description of machine views – key is view name. More...
 
 $references
 Description of machine references. More...
 

Reflection API

 describeId ()
 Reflection: Describe ID (primary key). More...
 
 getUrlFormat ()
 Get URL format. More...
 
 getParentUrlFormat ()
 Get prent URL format. More...
 
 getPostActionUrlFormat ()
 Get URL for redirect-after-post. More...
 
 getMachineImplementationMTime ()
 Get mtime of machine implementation. More...
 
 getAllMachineStates ($having_section=null)
 Reflection: Get all states. More...
 
 describeMachineState ($state, $field=null)
 Reflection: Describe given machine state. More...
 
 describeAllMachineStates ($having_section=null)
 Reflection: Describe all states.
 
 getAllMachineActions ($having_section=null)
 Reflection: Get all actions (transitions) More...
 
 describeMachineAction ($action, $field=null)
 Reflection: Describe given machine action (transition) More...
 
 describeAllMachineActions ($having_section=null)
 Reflection: Describe all actions (transitions)
 
 getAllMachineProperties ($having_section=null)
 Reflection: Get all properties. More...
 
 describeMachineProperty ($property, $field=null)
 Reflection: Describe given property. More...
 
 describeAllMachineProperties ($having_section=null)
 Reflection: Describe all properties. More...
 
 getAllMachineViews ($having_section=null)
 Reflection: Get all views. More...
 
 describeMachineView ($view, $field=null)
 Reflection: Describe given view. More...
 
 describeAllMachineViews ($having_section=null)
 Reflection: Describe all views.
 
 getAllMachineReferences ($having_section=null)
 Reflection: Get all references. More...
 
 describeMachineReference ($reference, $field=null)
 Reflection: Describe given reference. More...
 
 describeAllMachineReferences ($having_section=null)
 Reflection: Describe all references.
 
 exportDot ()
 Export state machine to Graphviz source code.
 

Detailed Description

Implementation of the state machine.

One instance of this class represents all machines of this type.

State machine always work with ID, never with Reference. References are decoded within backend.

Transition method should return FALSE when it has failed.

Constructor & Destructor Documentation

__construct ( AbstractBackend  $backend,
  $type,
  $config,
  $context 
)

Constructor.

Machine gets reference to owning backend, name of its type (under which is this machine registered in backend) and optional array of additional configuration (passed directly to initializeMachine method).

Here is the call graph for this function:

Member Function Documentation

getState (   $id)
abstract

Get current state of state machine.

Note
getState() does not check read access policy.

Here is the caller graph for this function:

getProperties (   $id,
$state_cache = null 
)
abstract

Get properties of state machine, including it's state.

If state machine uses property views, not all properties may be returned by this method. Some of them may be computed or too big.

Some implementations may store current state to $state_cache, so it does not have to be retireved in the second query.

Note
getProperties() does not check read access policy.

Here is the caller graph for this function:

getView (   $id,
  $view,
$properties_cache = null,
$view_cache = null,
$persistent_view_cache = null 
)

Get properties in given view.

Just like SQL view, the property view is transformed set of properties. These views are useful when some properties require heavy calculations.

Keep in mind, that view name must not interfere with Reference properties, otherwise the view is inaccessible directly.

Array $properties_cache is supplied by Reference class to make some calculations faster and without duplicate database queries. Make sure these cached properties are up to date or null.

Array $view_cache is writable cache inside the Reference class, flushed together with $properties_cache. If cache is empty, but available, an empty array is supplied.

Array $persistent_view_cache is kept untouched for entire Reference lifetime. If cache is empty, but available, an empty array is supplied.

/

Todo:
Override or call handlers?

Here is the call graph for this function:

resolveMachineReference (   $reference_name,
  $properties_cache,
$ref_machine_type,
$ref_machine_id 
)
protected

Helper function to resolve reference to another machine.

Parameters
[in]$reference_nameName of the reference in AbstractMachine::references.
[in]$properties_cacheProperties of referencing machine.
[out]$ref_machine_typeResolved machine type (string).
[out]$ref_machine_idResolved machine ID.

Here is the caller graph for this function:

isTransitionAllowed (   $id,
  $transition_name,
  $state = null 
)

Returns true if transition can be invoked right now.

/

Todo:
Transition should not have full definition of the policy, only its name. Definitions should be in common place.

Here is the call graph for this function:

Here is the caller graph for this function:

invokeTransition (   $id,
  $transition_name,
  $args,
$returns 
)

Invoke state machine transition.

State machine is not instance of this class, but it is represented by record in database.

Here is the call graph for this function:

describeId ( )
abstract

Reflection: Describe ID (primary key).

Returns array of all parts of the primary key and its types (as strings). If primary key is not compound, something like array('id' => 'string') is returned.

Order of the parts may be mandatory.

getUrlFormat ( )

Get URL format.

Format string for filename_format().

getParentUrlFormat ( )

Get prent URL format.

Parent URL is URL of collection or something of which is this machine part of.

Format string for filename_format().

getPostActionUrlFormat ( )

Get URL for redirect-after-post.

Format string for filename_format().

getMachineImplementationMTime ( )

Get mtime of machine implementation.

Useful to detect outdated cache entry in generated documentation.

No need to override this method, it handles inherited classes correctly. However if machine is loaded from database, a new implementation is needed.

This does not include filemtime(FILE) intentionaly.

getAllMachineStates (   $having_section = null)

Reflection: Get all states.

List of can be filtered by section, just like getAllMachineActions method does.

describeMachineState (   $state,
  $field = null 
)

Reflection: Describe given machine state.

Returns state description in array or null. If field is specified, only given field is returned.

getAllMachineActions (   $having_section = null)

Reflection: Get all actions (transitions)

List of actions can be filtered by section defined in action configuration. For example $this->getAllMachineStates('block') will return only actions which have 'block' configuration defined. Requested section must contain non-empty() value.

describeMachineAction (   $action,
  $field = null 
)

Reflection: Describe given machine action (transition)

Returns action description in array or null. If field is specified, only given field is returned.

getAllMachineProperties (   $having_section = null)

Reflection: Get all properties.

List of can be filtered by section, just like getAllMachineActions method does.

describeMachineProperty (   $property,
  $field = null 
)

Reflection: Describe given property.

Returns property description in array or null. If field is specified, only given field is returned.

describeAllMachineProperties (   $having_section = null)

Reflection: Describe all properties.

Returns array of all properties and their descriptions. See describeMachineProperty and getAllMachineProperties.

getAllMachineViews (   $having_section = null)

Reflection: Get all views.

List of can be filtered by section, just like getAllMachineActions method does.

describeMachineView (   $view,
  $field = null 
)

Reflection: Describe given view.

Returns view description in array or null. If field is specified, only given field is returned.

getAllMachineReferences (   $having_section = null)

Reflection: Get all references.

List of can be filtered by section, just like getAllMachineActions method does.

describeMachineReference (   $reference,
  $field = null 
)

Reflection: Describe given reference.

Returns reference description in array or null. If field is specified, only given field is returned.

Member Data Documentation

$url_fmt
protected

URL format string where machine is located, usualy only the path part, e.g.

"/machine-type/{id}".

To make reverse routes work, only entire path fragment can be replaced by symbol:

  • Good: /machine-type/foo/{id}/bar
  • Bad: /machine-type/foo-{id}/bar

This is limitation of default router, not this class.

If URL does not start with slash, router will ignore it.

$parent_url_fmt
protected

URL format string where parent of this machine is located, usualy only the path part, e.g.

"/parent-type/{id}/machine-type".

See also
$url_fmt
$post_action_url_fmt
protected

URL format string for redirect-after-post.

When machine is part of another entity, it may be reasonable to redirect to such entity instead of showing this machine alone. If not set, url_fmt is used.

See also
$url_fmt
$states
protected

Descriptions of all known states – key is state id, value is * description.

Fill these data in self::initializeMachine().

$state_groups
protected

State groups.

This state machine is flat – no sub-states. To make diagrams easier to read, this allows to group relevant states together. This has no influence on the behaviour.

Fill these data in self::initializeMachine().

$actions
protected

Description of all known actions – key is action name.

Each action has transitions (transition function) and each transition can end in various different states (assertion function).

Fill these data in self::initializeMachine().

$default_access_policy = null
protected

Default access policy.

When transition nor action has policy specified, this one is used.

Note
This does not affect read_access_policy.
$read_access_policy = null
protected

Read access policy.

Policy applied when reading machine state.

Note
Not affected by default_access_policy.
$access_policies
protected

Access policies.

Description of all access policies available to this state machine type.

$properties
protected

Description of machine properties – key is property name.

Each property has some metadata available, so it is possible to generate simple forms or present data to user without writing per-machine specific templates. These metadata should be as little implementation specific as possible.

$views
protected

Description of machine views – key is view name.

Some properties may require heavy computations and may not be used as often as other properties, so it is reasonable to put them into separate view instead.

These views are just like SQL views. They are somehow transformed view on the machine and its properties. There is no explicit definition of structure of the view – it can be single value, subset of properties, or something completely different.

View names must not collide with reference names, since references are special cases of views.

$references
protected

Description of machine references.

Often it is useful to reference one state machine from another, just like foreign keys in SQL.

Smalldb is limited to reference using primary keys only.

References are added to views.