sadekbaroudi/operation-state

Handle Operation States for executable actions, with the ability to undo. i.e. transactions and rollbacks for PHP

dev-master 2013-12-31 07:59 UTC

This package is not auto-updated.

Last update: 2024-05-20 13:58:23 UTC


README

Simple PHP classes to handle Operation States for executable actions, with the ability to undo. In other words, helper for transactions and rollbacks within PHP.

Its purpose is to provide a reliable method for executing actions, and being able to undo those actions when applicable. The functionality will allow you create multiple groups of executable actions and their respective undo actions.

Usage

use Sadekbaroudi\OperationState\OperationStateManager;
use Sadekbaroudi\OperationState\OperationState;

$yourClass = new YourClassName();

// Instantiate the manager
$osm = new OperationStateManager();

// Note that the setExecute and setUndo require PHP is_callable compliant parameters, as OperationState uses those methods
$os = new OperationState();
$os->setExecute(array($yourClass, 'yourMethod'), array('param1', $param2, array('foo' => 'bar')));
$os->setUndo(array($yourClass, 'undoMethod'), array('param1'));
$osm->add($os);

try {
    $osm->execute($os);
} catch ( OperationStateException $e ) {
    $osm->undo($os);
    throw $e;
}

Installation

Operation State can be installed with Composer by adding the library as a dependency to your composer.json file.

{
    "require": {
        "sadekbaroudi/operation-state": "*@dev"
    }
}

Please refer to the Composer's documentation for installation and usage instructions.