Download the PHP package kang-babi/transactions without Composer

On this page you can find all versions of the php package kang-babi/transactions. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package transactions

Transactions

A small PHP library for composing a main callback, exception handlers, and optional cleanup through a fluent API. run() returns the main callback's result or the result of the matching exception handler. PHPDoc generics preserve those return types for PHPStan.

The package works independently of Laravel. Database transaction management, exception reporting, and response formatting belong to your callbacks.

Installation

Requires PHP 8.0 or later.

Quick start

Start with Transaction::start(), configure the main callback with try(), then call run() to execute it.

Configuring callbacks does not execute them. Each call to run() executes the workflow again.

Handling exceptions

Register a handler with the exception's class name and a closure. The handler receives the original exception instance, and its return value becomes the result.

You can register multiple handlers, including handlers for concrete Error classes:

Exact class matching

Handlers match the thrown exception's exact class, rather than using PHP's native catch inheritance rules. For example, a handler for Exception::class does not handle RuntimeException, and Throwable::class does not act as a catch-all handler. Register each concrete exception class you intend to handle.

An exception without an exact handler propagates to the caller. Exceptions thrown inside a handler also propagate; they are not passed to other registered handlers. Registering a handler for the same class again replaces the previous handler.

Optional cleanup

Use finally() for a callback that runs after the main callback succeeds or an exception is handled successfully. It receives no arguments, and its return value is ignored.

Unlike PHP's native finally block, this callback does not always run.

Outcome Cleanup runs? Result of run()
Main callback succeeds Yes, if configured Main callback's return value
Matching handler succeeds Yes, if configured Handler's return value
No matching handler No Original exception propagates
Handler throws No Handler's exception propagates
Cleanup throws It starts but fails Cleanup's exception propagates

Exceptions from cleanup are not passed to registered handlers. If cleanup must run even when an exception propagates, use a native PHP try/finally block around run().

Return types and PHPStan

run() has a native mixed return type because callbacks can return any value. Its PHPDoc return type is TTryResult|TCatchResult, which lets PHPStan infer the union of the main callback's return type and all registered handler return types.

For example, a main callback returning int and a handler returning null produce an inferred int|null result. When both paths return compatible array shapes, the workflow can be returned directly from a method with an explicit array-shape contract:

PHPStan infers the array shapes from these closure bodies. When a callback delegates to another method, document that method's return shape so the information remains available for inference. PHP enforces the native return type at runtime; PHPStan checks the more specific PHPDoc contract.

API reference

Method Behavior
Transaction::start() Creates a new workflow.
try(Closure $try) Sets the main callback. A later call replaces it. Required before run().
catch(string $exception, Closure $catch) Registers a handler for an exact Throwable class. A later registration for the same class replaces it.
finally(Closure $finally) Sets optional cleanup. A later call replaces it.
run() Executes the workflow and returns the main callback's or matching handler's result.

All configuration methods return the same instance for chaining. Calling run() before configuring try() raises an Error because the main callback is unset.

Development

Install development dependencies and run the test suite:

Check formatting and static analysis:

Changelog

See CHANGELOG.md for release notes.

License

Released under the MIT license.


All versions of transactions with dependencies

PHP Build Version
Package Version
Requires php Version ^8.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package kang-babi/transactions contains the following files

Loading the files please wait ...