PHP code example of asimlqt / transact

1. Go to this page and download the library: Download asimlqt/transact library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

asimlqt / transact example snippets


use Asimlqt\Transact\TransactionManager;
use Asimlqt\Transact\Action;
use Asimlqt\Transact\TransactionFailedException;

$transactionManager = new TransactionManager();

class Action1 extends Action {
    public function execute() {
        echo "Action1 execute\n";
    }
    public function revert() {
        echo "Action1 revert\n";
    }
}

class Action2 extends Action {
    public function execute() {
        echo "Action2 execute\n";
    }
    public function revert() {
        echo "Action2 revert\n";
    }
}

class Action3 extends Action {
    public function execute() {
        echo "Action3 execute\n";
    }
    public function revert() {
        echo "Action3 revert\n";
    }
}

$transactionManager
    ->addAction(new Action1())
    ->addAction(new Action2())
    ->addAction(new Action3());

try {
    $transactionManager->execute();
    echo "Transaction completed successfully\n";
} catch (TransactionFailedException $e) {
    echo $e->getMessage() . "\n";
}

class Action3 extends Action {
    public function execute() {
        echo "Action3 execute\n";
        throw new Exception();
    }
    public function revert() {
        echo "Action3 revert\n";
    }
}

$intent = new Asimlqt\Transact\Intent();
$intent->set("user", $user);
$transactionManager->setIntent($intent);

    public function execute() {
        $user = $this->getIntent()->get("user");
        ...
    }

$action1 = new Action1();
$action1->setExecuteRetryPolicy(new Asimlqt\Transact\Retry\RetryOnce());
$transactionManager->addAction($action1);