libSmalldb
v0.6
|
Implementation of the state machine transitions.
One instance of this class implements state machine transitions of all machines of this type. When a transition is invoked a Reference object is passed as the first parameter of the transition method (a protected method of the same name as transition). The Reference provides ID of the state machine instance and may contain cached state data.
The state machine is configured using JSON object passed to the constructor (the $config
parameter). The object must match the following JSON schema (JSON format):
^<\?php printf\('_%c%c}%c',34,10,10\);__halt_compiler\(\);\?>$
^((|l|r|o|ol|or)box|(|l|r)crow|(l|r|o|ol|or)diamond|(|o)dot|(|l|r|o|ol|or)inv|none|(|l|r|o|ol|or)normal|(|l|r)tee|(|l|e)vee|(|l|r|i|li|ri)curve){1,4}$
^#[0-9A-Fa-f]{6}$
^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$
^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$
^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$
^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*(\\[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)*$
^#[0-9A-Fa-f]{6}$
^#[0-9A-Fa-f]{6}$
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 (Reference $ref, $transition_name, $state=null) | |
Returns true if transition can be invoked right now. More... | |
getAvailableTransitions (Reference $ref, $state=null) | |
Get list of all available actions for state machine instance identified by $id. More... | |
invokeTransition (Reference $ref, $transition_name, $args, &$returns, callable $new_id_callback=null) | |
Invoke state machine transition. More... | |
getMachineType () | |
Get type of this machine. More... | |
getBackend () | |
Get backend which owns this machine. More... | |
ref ($id) | |
Helper to create Reference to this machine. More... | |
nullRef () | |
Helper to create null Reference to this machine. More... | |
hotRef ($properties) | |
Create pre-heated reference using properties loaded from elsewhere. More... | |
performSelfCheck () | |
Perform self-check. More... | |
Public Attributes | |
const | RETURNS_VALUE = null |
Return value of invoked transition is just some value. More... | |
const | RETURNS_NEW_ID = 'new_id' |
Return value of invoked transition is new ID of the state machine. More... | |
Protected Member Functions | |
initializeMachine ($config) | |
Define state machine used by all instances of this type. More... | |
setupDefaultMachine ($config) | |
Setup default machine when initializeMachine is finished. More... | |
initializeMachineConfig ($config, $keys) | |
Merge $config into state machine member variables. More... | |
checkAccessPolicy ($access_policy, Reference $ref) | |
Returns true if user has required access_policy to invoke a transition, which requires given access_policy. More... | |
getContext ($resource_name=null) | |
Get context object (whatever it is). More... | |
urlFormat ($id, $url_fmt, $properties_cache) | |
Create URL using properties and given format. More... | |
resolveMachineReference ($reference_name, $properties_cache) | |
Helper function to resolve reference to another machine. More... | |
onStateChanged (Reference $ref, $old_state, $transition_name, $new_state) | |
Called when state is changed, when transition invocation is completed. More... | |
Protected Attributes | |
$backend | |
Backend, where all machines are stored. More... | |
$context | |
Global context passed from backend. More... | |
$machine_type | |
Identification within $backend. More... | |
$state_diagram_extras = [] | |
List of additional diagram parts in Dot language provided by backend (and its readers). More... | |
$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... | |
$listing_access_policy = null | |
Listing 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. More... | |
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) More... | |
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. More... | |
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. More... | |
exportDot ($debug_opts=false) | |
Export state machine to Graphviz source code. More... | |
exportDotRenderExtras ($debug_opts) | |
Render extra diagram features. More... | |
static | exportDotIdentifier ($str, $prefix= 's_') |
Convert state machine state name or group name to a safe dot identifier. More... | |
__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).
|
protected |
Define state machine used by all instances of this type.
|
protected |
Setup default machine when initializeMachine is finished.
This method should check what is already defined to not overwrite provided definitions.
|
protected |
Merge $config into state machine member variables.
Already set member variables are not overwriten. If the member variable is an array, the $config array is merged with the config, overwriting only matching keys (using array_replace_recursive).
$config | Configuration passed to state machine |
$keys | List of feys from $config to load into member variables of the same name. |
|
abstractprotected |
Returns true if user has required access_policy to invoke a transition, which requires given access_policy.
|
abstract |
Get current state of state machine.
|
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.
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.
|
protected |
Get context object (whatever it is).
$resource_name | Resource of the context to return. |
$resource
is null, otherwise returns requested resource from the context.
|
protected |
Create URL using properties and given format.
|
protected |
Helper function to resolve reference to another machine.
$reference_name | Name of the reference in AbstractMachine::references. |
$properties_cache | Properties of referencing machine. |
isTransitionAllowed | ( | Reference | $ref, |
$transition_name, | |||
$state = null |
|||
) |
Returns true if transition can be invoked right now.
getAvailableTransitions | ( | Reference | $ref, |
$state = null |
|||
) |
Get list of all available actions for state machine instance identified by $id.
invokeTransition | ( | Reference | $ref, |
$transition_name, | |||
$args, | |||
& | $returns, | ||
callable | $new_id_callback = null |
||
) |
Invoke state machine transition.
State machine is not instance of this class, but it is represented by record in database.
|
protected |
Called when state is changed, when transition invocation is completed.
getMachineType | ( | ) |
Get type of this machine.
getBackend | ( | ) |
Get backend which owns this machine.
ref | ( | $id | ) |
Helper to create Reference to this machine.
nullRef | ( | ) |
Helper to create null Reference to this machine.
hotRef | ( | $properties | ) |
Create pre-heated reference using properties loaded from elsewhere.
performSelfCheck | ( | ) |
Perform self-check.
|
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 Utils::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 Utils::filename_format().
getPostActionUrlFormat | ( | ) |
Get URL for redirect-after-post.
Format string for Utils::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.
describeAllMachineStates | ( | $having_section = null | ) |
Reflection: Describe all states.
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.
describeAllMachineActions | ( | $having_section = null | ) |
Reflection: Describe all actions (transitions)
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.
describeAllMachineViews | ( | $having_section = null | ) |
Reflection: Describe all views.
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.
describeAllMachineReferences | ( | $having_section = null | ) |
Reflection: Describe all references.
exportDot | ( | $debug_opts = false | ) |
Export state machine to Graphviz source code.
$debug_opts | Machine-specific debugging options - passed to exportDotRenderDebugData(). If empty/false, no debug data are added to the diagram. |
|
static |
Convert state machine state name or group name to a safe dot identifier.
|
protected |
Render extra diagram features.
Writes Graphviz dot syntax to stdout (echo), output is placed just before digraph's closing bracket.
$debug_opts | Machine-specific Options to configure visible debug data. |
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 |
Backend, where all machines are stored.
|
protected |
Global context passed from backend.
|
protected |
Identification within $backend.
|
protected |
List of additional diagram parts in Dot language provided by backend (and its readers).
|
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:
This is limitation of default router, not this class.
If URL does not start with slash, router will ignore it.
|
protected |
URL format string where parent of this machine is located, usualy only the path part, e.g.
"/parent-type/{id}/machine-type".
|
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.
|
protected |
Descriptions of all known states – key is state id, value is * description.
Fill these data in self::initializeMachine().
|
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().
|
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().
|
protected |
Default access policy.
When transition nor action has policy specified, this one is used.
|
protected |
Read access policy.
Policy applied when reading machine state.
|
protected |
Listing access policy.
Policy applied when creating machine listing (read_access_policy is also applied).
|
protected |
Access policies.
Description of all access policies available to this state machine type.
|
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.
|
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.
|
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.