Download the PHP package asimlqt/transact without Composer
On this page you can find all versions of the php package asimlqt/transact. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download asimlqt/transact
More information about asimlqt/transact
Files in asimlqt/transact
Package transact
Short Description A transaction library for PHP
License Apache-2.0
Homepage https://github.com/asimlqt/transact
Informations about the package transact
Transact
Transact is a transaction library for PHP similar to the way database transactions work but for PHP code.
The purpose is to be able to execute multiple actions which might depend on the previous action completing successfully before eexecuting the next one and in case of failure you want to revert back to the original state.
Installation
Use the following composer command to install
Example
The following example creates 3 simple actions that echo some text during the execute
and revert
methods. This example will be referenced throughout the rest of the README and only changes will be listed.
The output of the program is
Order of execution
The actions are executed in the order they are added. When there is an error i.e. when an exception is thrown the revert
methods will be called in reverse order. The revert
method of the last successful action will be called first then the one before that etc. all the way to the first action.
For example if the third action threw an exception then the revert
method of Action2
will be called then the revert
method of Action1
:
The output of will be:
You've probably noticed that the Action3
execute method through an exception so why wasn't the revert method of Action3
called? That's becasue each action should perform only one task, so if an action threw an exception becasue it couldn't complete the task then there is nothing to revert!
Passing data to actions
Usually the actions will need some data to perform their tasks so to do this use the Intent
object which is a simple wrapper around an array. It only has 2 methods get
and set
. This will automatically be injected into the actions before the execute method is called.
Then in the Action you can retrieve the user using:
Note that the same intent object will be forwarded to all actions so it is possible to overwrite data. It can also be useful if you need to pass data from one action to another.
Retry Policies
If an action fails to complete it's task due to some external factor you might want to try the action again e.g. making an API request. Retry policies can be specified for both, execute
and revert
. They are action specific so you can only specify a retry policy for only one action or different policies for different actions. The following policies are currently available:
RetryNone
This is the default policy if you don't explicitly specify one. this does not perform any retries.
RetryOnce
This will immediately try the action again before marking it as failed.
RetryNumTimes
This will try the action repeatedly for the specified number of times.
RetryAfter
This will retry the request once more after a delay of specified microseconds.
You will need to set these on the Action objects before adding them to the transaction manager: