PHP Class izzum\statemachine\persistence\Redis

Since redis has no schemas (as a nosql db), it should store data based on the way you want to retrieve data. There will be multiple views on the data which should make sense for a lot of use cases (counters, sets, sorted sets, lists, hashes etc) to retrieve the data. All keys that izzum uses in redis can be found in this classes' constants with a 'KEY_' prefix. This class uses the php redis module and your php build should be setup with this module loaded. An instance uses a redis key prefix of 'izzum:' by default. but this can be set to whatever you like. . You can even set the prefix key to something else temporarily for loading the configuration data and then set it to another prefix for writing state data. This allows you to store multiple machine configurations under different prefixes so you can load different machines from different places, which facilitates multiple dev teams working on configuration of different machines without them overwriting other teams' definitions. The configuration of statemachines is a JSON string. The specification of the JSON string can be found in izzum\statemachine\loader\JSON::getJSONSchema. see asset/json/json.schema Internally, this class uses the JSON loader to load the configuration. It can be set up to store multiple configurations under multiple keys. You can use the normal redis connection settings to connect to redis.
Inheritance: extends Adapter, implements izzum\statemachine\loader\Loader
Mostra file Open project: rolfvreijdenberger/izzum-statemachine Class Usage Examples

Public Methods

Method Description
__call ( string $name, mixed $arguments ) : mixed very very dumb proxy to redis connection. should only be used for testing.
__construct ( string $host = '127.0.0.1', integer $port = 6379, float $timeout, string $reserved = null, integer $retry = null ) The constructor accepts default connection parameters.
__destruct ( ) do some cleanup
__toString ( )
addHistory ( Identifier $identifier, $state, $message = null, $is_exception = false ) {@inheritDoc}
getConfigurationKey ( ) : string get the configuration key used for storing a json string of machine configurations.
getEntityIds ( $machine, $state = null ) {@inheritDoc}
getPrefix ( ) : string get the prefix for all keys used
getRedis ( ) : Redis Gets a lazy loaded \Redis instance that is connected and optionally authenticated.
insertState ( Identifier $identifier, $state, $message = null ) {@inheritDoc}
isPersisted ( Identifier $identifier ) {@inheritDoc}
load ( StateMachine $statemachine ) {@inheritDoc} Load the statemachine with data from a JSON string.
processGetState ( Identifier $identifier ) {@inheritDoc}
setConfigurationKey ( string $key ) set the configuration key to be used for storing a json string of machine configurations.
setConnection ( Redis $redis ) set the redis connection explicitely, useful if you want to share the redis instance when it is created outside this class.
setDatabase ( integer $database ) set the redis database. in case there is an active connection, it switches the database.
setPassword ( string $password ) set password to authenticate to the redis server
setPrefix ( string $prefix ) set the key prefix to be used for all redis keys
setUnixDomainSocket ( $socket )
toString ( )
updateState ( Identifier $identifier, $state, $message = null ) {@inheritDoc}

Protected Methods

Method Description
onConnect ( ) A hook to use in a subclass.

Method Details

__call() public method

calls methods on a https://github.com/phpredis/phpredis instance. some method calls will only accept an array as the second argument (like hmset) This makes it useful to test the redis commands or just use this class as an interface to redis.
public __call ( string $name, mixed $arguments ) : mixed
$name string name of the method to route to the active redis connection
$arguments mixed
return mixed

__construct() public method

You can also use an existing \Redis instance used by your application. Just construct without parameters and call 'setConnection($instance)' before doing anything else. You can also use a unix domain socket. Just construct without parameters and call 'setUnixDomainSocket' before doing anything else.
public __construct ( string $host = '127.0.0.1', integer $port = 6379, float $timeout, string $reserved = null, integer $retry = null )
$host string optional
$port integer optional
$timeout float value in seconds. default is 0 meaning unlimited
$reserved string should be NULL if $retry is specified
$retry integer value in milliseconds

__destruct() public method

do some cleanup
public __destruct ( )

__toString() public method

public __toString ( )

addHistory() public method

{@inheritDoc}
public addHistory ( Identifier $identifier, $state, $message = null, $is_exception = false )
$identifier izzum\statemachine\Identifier

getConfigurationKey() final public method

get the configuration key used for storing a json string of machine configurations.
final public getConfigurationKey ( ) : string
return string $key

getEntityIds() public method

{@inheritDoc}
public getEntityIds ( $machine, $state = null )

getPrefix() final public method

get the prefix for all keys used
final public getPrefix ( ) : string
return string

getRedis() public method

Gets a lazy loaded \Redis instance that is connected and optionally authenticated.
public getRedis ( ) : Redis
return Redis

insertState() public method

{@inheritDoc}
public insertState ( Identifier $identifier, $state, $message = null )
$identifier izzum\statemachine\Identifier

isPersisted() public method

{@inheritDoc}
public isPersisted ( Identifier $identifier )
$identifier izzum\statemachine\Identifier

load() public method

the JSON string is stored at the redis key 'configuration' by default. you can alter the configuration key by using Redis::setPrefix() and Redis::setConfigurationKey() First, the key ':configuration:' is checked for existence. If it exists, take the configuration from that key, else take the configuration form the ':configuration' key. This method can be overriden in a subclass to use another loader when the data is stored in redis in YAML or XML form for example. You could use the ReaderWriterDelegator to use another source to load the configuration from.
public load ( StateMachine $statemachine )
$statemachine izzum\statemachine\StateMachine

onConnect() protected method

you can do you initial setup here if you like.
protected onConnect ( )

processGetState() public method

{@inheritDoc}
public processGetState ( Identifier $identifier )
$identifier izzum\statemachine\Identifier

setConfigurationKey() final public method

set the configuration key to be used for storing a json string of machine configurations.
final public setConfigurationKey ( string $key )
$key string

setConnection() public method

set the redis connection explicitely, useful if you want to share the redis instance when it is created outside this class.
public setConnection ( Redis $redis )
$redis Redis a connected (and authenticated) redis instance

setDatabase() public method

set the redis database. in case there is an active connection, it switches the database.
public setDatabase ( integer $database )
$database integer a redis database is an integer starting from 0 (the default)

setPassword() public method

set password to authenticate to the redis server
public setPassword ( string $password )
$password string

setPrefix() final public method

set the key prefix to be used for all redis keys
final public setPrefix ( string $prefix )
$prefix string

setUnixDomainSocket() public method

public setUnixDomainSocket ( $socket )

toString() public method

public toString ( )

updateState() public method

{@inheritDoc}
public updateState ( Identifier $identifier, $state, $message = null )
$identifier izzum\statemachine\Identifier