PHPackages                             johmanx10/transaction - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. johmanx10/transaction

ActiveLibrary[Database &amp; ORM](/categories/database)

johmanx10/transaction
=====================

Handles operations with automatic rollback mechanisms.

1.2.0(6y ago)1943.3k2MITPHPPHP ^7.2

Since Dec 2Pushed 4y ago4 watchersCompare

[ Source](https://github.com/johmanx10/transaction)[ Packagist](https://packagist.org/packages/johmanx10/transaction)[ RSS](/packages/johmanx10-transaction/feed)WikiDiscussions master Synced 2w ago

READMEChangelog (6)Dependencies (2)Versions (9)Used By (0)

[![Build Status](https://camo.githubusercontent.com/acb3d3b76a63fb5ad45f61b82b19b4a8f60e7a1d5482e6586482b84de0eb6862/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6a6f686d616e7831302f7472616e73616374696f6e2f6261646765732f6275696c642e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/johmanx10/transaction/build-status/master)[![Packagist](https://camo.githubusercontent.com/4092ef1b5de2fb20e3b3a980dabaf87e64a928bdebd41582fb84dfd3ce626839/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6a6f686d616e7831302f7472616e73616374696f6e2e706e67)](https://packagist.org/packages/johmanx10/transaction/stats)[![Packagist](https://camo.githubusercontent.com/4a90fe37b949f88fb4a09aae991a3d3a9ac2cb2236df59504bfe6ae64df6935e/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6a6f686d616e7831302f7472616e73616374696f6e2e706e67)](https://packagist.org/packages/johmanx10/transaction)[![PHP from Packagist](https://camo.githubusercontent.com/b112ec9cad171f6a09f52cf9d189a566c504478fe76d0522a1eb38d0e519e98a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f6a6f686d616e7831302f7472616e73616374696f6e2e737667)](https://camo.githubusercontent.com/b112ec9cad171f6a09f52cf9d189a566c504478fe76d0522a1eb38d0e519e98a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f6a6f686d616e7831302f7472616e73616374696f6e2e737667)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/617aee05bd41ecf36537b49404cec5187a87865c53e825cc997d59599f92d370/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6a6f686d616e7831302f7472616e73616374696f6e2f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/johmanx10/transaction/?branch=master)[![Code Coverage](https://camo.githubusercontent.com/6d3aed9bdacafdb84791ae9959a18e64f094607959fab6f28cfa773928c9d1f9/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6a6f686d616e7831302f7472616e73616374696f6e2f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/johmanx10/transaction/?branch=master)[![Packagist](https://camo.githubusercontent.com/dcf37bad828d9b76c4a84ca64b9ae55f87d9925b9b90284ce95979e13ae31746/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6a6f686d616e7831302f7472616e73616374696f6e2e737667)](https://camo.githubusercontent.com/dcf37bad828d9b76c4a84ca64b9ae55f87d9925b9b90284ce95979e13ae31746/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6a6f686d616e7831302f7472616e73616374696f6e2e737667)

Introduction
============

[](#introduction)

Transaction handles operations with automatic rollback mechanisms.

A transaction consists of operations. When an operation fails, it traverses back up the chain, rolling back all previous operations in reverse order.

Assume a situation where filesystem operations need to be automated. If a part of the operations fail, the filesystem needs to be restored to the state before all operations were applied. Given the following operations:

1. Create directory `my-app`
2. Copy file `dist/console` to `my-app/bin/console`
3. Add executable rights to `my-app/bin/console`

This will be handled as follows:

1. ✔ Create directory `my-app`.
2. ∴ Copy file `dist/console` to `my-app/bin/console` - Directory `my-app/bin`does not exist.
3. ✔ Rollback: if `my-app/bin/console` exists, remove it.
4. ✔ Rollback: if `my-app` exists, remove it.

An [example of the above](examples/file-operations) can be tested locally by running `examples/file-operations` from a command line terminal.

Every operation is responsible for defining their own rollback mechanism. That way, complex nested structures to check and roll back operations can be constructed vertically.

Installation
============

[](#installation)

```
composer require johmanx10/transaction

```

Processing operations
=====================

[](#processing-operations)

To process a list of ordered operations, either use a transaction, or a handler.

Transaction
-----------

[](#transaction)

A transaction is more straight-forward and better suited to less complicated transactional scripts.

```
